use of org.eclipse.jdt.internal.compiler.ast.FieldDeclaration in project lombok by rzwitserloot.
the class HandleSetter method generateSetterForType.
public boolean generateSetterForType(EclipseNode typeNode, EclipseNode pos, AccessLevel level, boolean checkForTypeLevelSetter) {
if (checkForTypeLevelSetter) {
if (hasAnnotation(Setter.class, typeNode)) {
// The annotation will make it happen, so we can skip it.
return true;
}
}
TypeDeclaration typeDecl = null;
if (typeNode.get() instanceof TypeDeclaration)
typeDecl = (TypeDeclaration) typeNode.get();
int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0;
if (typeDecl == null || notAClass) {
pos.addError("@Setter is only supported on a class or a field.");
return false;
}
for (EclipseNode field : typeNode.down()) {
if (field.getKind() != Kind.FIELD)
continue;
FieldDeclaration fieldDecl = (FieldDeclaration) field.get();
if (!filterField(fieldDecl))
continue;
// Skip final fields.
if ((fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0)
continue;
generateSetterForField(field, pos, level);
}
return true;
}
use of org.eclipse.jdt.internal.compiler.ast.FieldDeclaration in project lombok by rzwitserloot.
the class HandleSetter method createSetterForField.
public void createSetterForField(AccessLevel level, EclipseNode fieldNode, EclipseNode sourceNode, boolean whineIfExists, List<Annotation> onMethod, List<Annotation> onParam) {
ASTNode source = sourceNode.get();
if (fieldNode.getKind() != Kind.FIELD) {
sourceNode.addError("@Setter is only supported on a class or a field.");
return;
}
FieldDeclaration field = (FieldDeclaration) fieldNode.get();
TypeReference fieldType = copyType(field.type, source);
boolean isBoolean = isBoolean(fieldType);
String setterName = toSetterName(fieldNode, isBoolean);
boolean shouldReturnThis = shouldReturnThis(fieldNode);
if (setterName == null) {
fieldNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list.");
return;
}
int modifier = toEclipseModifier(level) | (field.modifiers & ClassFileConstants.AccStatic);
for (String altName : toAllSetterNames(fieldNode, isBoolean)) {
switch(methodExists(altName, fieldNode, false, 1)) {
case EXISTS_BY_LOMBOK:
return;
case EXISTS_BY_USER:
if (whineIfExists) {
String altNameExpl = "";
if (!altName.equals(setterName))
altNameExpl = String.format(" (%s)", altName);
fieldNode.addWarning(String.format("Not generating %s(): A method with that name already exists%s", setterName, altNameExpl));
}
return;
default:
case NOT_EXISTS:
}
}
MethodDeclaration method = createSetter((TypeDeclaration) fieldNode.up().get(), false, fieldNode, setterName, null, shouldReturnThis, modifier, sourceNode, onMethod, onParam);
injectMethod(fieldNode.up(), method);
}
use of org.eclipse.jdt.internal.compiler.ast.FieldDeclaration in project lombok by rzwitserloot.
the class EclipseHandlerUtil method createListOfNonExistentFields.
/**
* Given a list of field names and a node referring to a type, finds each name in the list that does not match a field within the type.
*/
public static List<Integer> createListOfNonExistentFields(List<String> list, EclipseNode type, boolean excludeStandard, boolean excludeTransient) {
boolean[] matched = new boolean[list.size()];
for (EclipseNode child : type.down()) {
if (list.isEmpty())
break;
if (child.getKind() != Kind.FIELD)
continue;
if (excludeStandard) {
if ((((FieldDeclaration) child.get()).modifiers & ClassFileConstants.AccStatic) != 0)
continue;
if (child.getName().startsWith("$"))
continue;
}
if (excludeTransient && (((FieldDeclaration) child.get()).modifiers & ClassFileConstants.AccTransient) != 0)
continue;
int idx = list.indexOf(child.getName());
if (idx > -1)
matched[idx] = true;
}
List<Integer> problematic = new ArrayList<Integer>();
for (int i = 0; i < list.size(); i++) {
if (!matched[i])
problematic.add(i);
}
return problematic;
}
use of org.eclipse.jdt.internal.compiler.ast.FieldDeclaration in project lombok by rzwitserloot.
the class HandleBuilder method makeSimpleSetterMethodForBuilder.
private void makeSimpleSetterMethodForBuilder(EclipseNode builderType, boolean deprecate, EclipseNode fieldNode, char[] nameOfSetFlag, EclipseNode sourceNode, boolean fluent, boolean chain) {
TypeDeclaration td = (TypeDeclaration) builderType.get();
AbstractMethodDeclaration[] existing = td.methods;
if (existing == null)
existing = EMPTY;
int len = existing.length;
FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
char[] name = fd.name;
for (int i = 0; i < len; i++) {
if (!(existing[i] instanceof MethodDeclaration))
continue;
char[] existingName = existing[i].selector;
if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i]))
return;
}
String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName());
MethodDeclaration setter = HandleSetter.createSetter(td, deprecate, fieldNode, setterName, nameOfSetFlag, chain, ClassFileConstants.AccPublic, sourceNode, Collections.<Annotation>emptyList(), Collections.<Annotation>emptyList());
injectMethod(builderType, setter);
}
use of org.eclipse.jdt.internal.compiler.ast.FieldDeclaration in project lombok by rzwitserloot.
the class HandleBuilder method generateDefaultProvider.
public MethodDeclaration generateDefaultProvider(char[] methodName, TypeParameter[] typeParameters, EclipseNode fieldNode, ASTNode source) {
int pS = source.sourceStart, pE = source.sourceEnd;
MethodDeclaration out = new MethodDeclaration(((CompilationUnitDeclaration) fieldNode.top().get()).compilationResult);
out.typeParameters = copyTypeParams(typeParameters, source);
out.selector = methodName;
out.modifiers = ClassFileConstants.AccPrivate | ClassFileConstants.AccStatic;
out.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
FieldDeclaration fd = (FieldDeclaration) fieldNode.get();
out.returnType = copyType(fd.type, source);
out.statements = new Statement[] { new ReturnStatement(fd.initialization, pS, pE) };
fd.initialization = null;
out.traverse(new SetGeneratedByVisitor(source), ((TypeDeclaration) fieldNode.up().get()).scope);
return out;
}
Aggregations