diff options
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java | 16 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSetter.java | 57 |
2 files changed, 63 insertions, 10 deletions
diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index ff743601..a056c396 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -937,7 +937,7 @@ public class EclipseHandlerUtil { */ public static List<String> toAllGetterNames(EclipseNode field, boolean isBoolean) { String fieldName = field.getName(); - AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + AnnotationValues<Accessors> accessors = getAccessorsForField(field); return TransformationsUtil.toAllGetterNames(accessors, fieldName, isBoolean); } @@ -978,6 +978,18 @@ public class EclipseHandlerUtil { } /** + * When generating a setter, the setter either returns void (beanspec) or Self (fluent). + * This method scans for the {@code Accessors} annotation to figure that out. + */ + public static boolean shouldReturnThis(EclipseNode field) { + if ((((FieldDeclaration) field.get()).modifiers & ClassFileConstants.AccStatic) != 0) return false; + AnnotationValues<Accessors> accessors = EclipseHandlerUtil.getAccessorsForField(field); + boolean forced = (accessors.getActualExpression("chain") != null); + Accessors instance = accessors.getInstance(); + return instance.chain() || (instance.fluent() && !forced); + } + + /** * Checks if the field should be included in operations that work on 'all' fields: * If the field is static, or starts with a '$', or is actually an enum constant, 'false' is returned, indicating you should skip it. */ @@ -1006,7 +1018,7 @@ public class EclipseHandlerUtil { EclipseNode current = field.up(); while (current != null) { - for (EclipseNode node : field.down()) { + for (EclipseNode node : current.down()) { if (annotationTypeMatches(Accessors.class, node)) { return createAnnotation(Accessors.class, node); } diff --git a/src/core/lombok/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 3fa872f9..996ac14a 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/handlers/HandleSetter.java @@ -25,15 +25,18 @@ import static lombok.eclipse.Eclipse.*; import static lombok.eclipse.handlers.EclipseHandlerUtil.*; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; import lombok.AccessLevel; import lombok.Setter; +import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; -import lombok.core.AST.Kind; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; +import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.Annotation; @@ -43,9 +46,14 @@ import org.eclipse.jdt.internal.compiler.ast.Expression; import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.NameReference; +import org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference; +import org.eclipse.jdt.internal.compiler.ast.ReturnStatement; import org.eclipse.jdt.internal.compiler.ast.SingleNameReference; +import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference; import org.eclipse.jdt.internal.compiler.ast.Statement; +import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; +import org.eclipse.jdt.internal.compiler.ast.TypeParameter; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; @@ -149,6 +157,7 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { TypeReference fieldType = copyType(field.type, source); boolean isBoolean = nameEquals(fieldType.getTypeName(), "boolean") && fieldType.dimensions() == 0; String setterName = toSetterName(fieldNode, isBoolean); + boolean shouldReturnThis = shouldReturnThis(fieldNode); if (setterName == null) { errorNode.addWarning("Not generating setter for this field: It does not fit your @Accessors prefix list."); return; @@ -174,19 +183,40 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { } } - MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, modifier, source); + MethodDeclaration method = generateSetter((TypeDeclaration) fieldNode.up().get(), fieldNode, setterName, shouldReturnThis, modifier, source); injectMethod(fieldNode.up(), method); } - private MethodDeclaration generateSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, int modifier, ASTNode source) { + private MethodDeclaration generateSetter(TypeDeclaration parent, EclipseNode fieldNode, String name, boolean shouldReturnThis, int modifier, ASTNode source) { FieldDeclaration field = (FieldDeclaration) fieldNode.get(); int pS = source.sourceStart, pE = source.sourceEnd; long p = (long)pS << 32 | pE; MethodDeclaration method = new MethodDeclaration(parent.compilationResult); setGeneratedBy(method, source); method.modifiers = modifier; - method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); - method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; + if (shouldReturnThis) { + EclipseNode type = fieldNode; + while (type != null && type.getKind() != Kind.TYPE) type = type.up(); + if (type != null && type.get() instanceof TypeDeclaration) { + TypeDeclaration typeDecl = (TypeDeclaration) type.get(); + if (typeDecl.typeParameters != null && typeDecl.typeParameters.length > 0) { + TypeReference[] refs = new TypeReference[typeDecl.typeParameters.length]; + int idx = 0; + for (TypeParameter param : typeDecl.typeParameters) { + TypeReference typeRef = new SingleTypeReference(param.name, (long)param.sourceStart << 32 | param.sourceEnd); + setGeneratedBy(typeRef, source); + refs[idx++] = typeRef; + } + method.returnType = new ParameterizedSingleTypeReference(typeDecl.name, refs, 0, p); + } else method.returnType = new SingleTypeReference(((TypeDeclaration)type.get()).name, p); + } + } + + if (method.returnType == null) { + method.returnType = TypeReference.baseTypeReference(TypeIds.T_void, 0); + method.returnType.sourceStart = pS; method.returnType.sourceEnd = pE; + shouldReturnThis = false; + } setGeneratedBy(method.returnType, source); if (isFieldDeprecated(fieldNode)) { method.annotations = new Annotation[] { generateDeprecatedAnnotation(source) }; @@ -211,13 +241,24 @@ public class HandleSetter extends EclipseAnnotationHandler<Setter> { Annotation[] nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); Annotation[] nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + List<Statement> statements = new ArrayList<Statement>(5); if (nonNulls.length == 0) { - method.statements = new Statement[] { assignment }; + statements.add(assignment); } else { Statement nullCheck = generateNullCheck(field, source); - if (nullCheck != null) method.statements = new Statement[] { nullCheck, assignment }; - else method.statements = new Statement[] { assignment }; + if (nullCheck != null) statements.add(nullCheck); + statements.add(assignment); } + + if (shouldReturnThis) { + ThisReference thisRef = new ThisReference(pS, pE); + setGeneratedBy(thisRef, source); + ReturnStatement returnThis = new ReturnStatement(thisRef, pS, pE); + setGeneratedBy(returnThis, source); + statements.add(returnThis); + } + method.statements = statements.toArray(new Statement[0]); + Annotation[] copiedAnnotations = copyAnnotations(source, nonNulls, nullables); if (copiedAnnotations.length != 0) param.annotations = copiedAnnotations; return method; |