diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 21:57:24 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2012-03-26 21:57:24 +0200 |
commit | eb3250a2fa4a3a141cd8a1e552b8f3bbd8ea1726 (patch) | |
tree | a17c502fb3eea1e99dea1c55cb3a41359464028e /src/core | |
parent | a514af4dcdd87cdae64e87b9d8a8d1a489a8e474 (diff) | |
download | lombok-eb3250a2fa4a3a141cd8a1e552b8f3bbd8ea1726.tar.gz lombok-eb3250a2fa4a3a141cd8a1e552b8f3bbd8ea1726.tar.bz2 lombok-eb3250a2fa4a3a141cd8a1e552b8f3bbd8ea1726.zip |
Finished work on @Accessors
Diffstat (limited to 'src/core')
4 files changed, 118 insertions, 21 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; diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index 2a91fb37..02591736 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -50,12 +50,14 @@ import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; +import com.sun.tools.javac.tree.JCTree.JCReturn; import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCTypeParameter; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; import com.sun.tools.javac.util.List; +import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; /** @@ -195,6 +197,7 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { private JCMethodDecl createSetter(long access, JavacNode field, TreeMaker treeMaker, JCTree source) { String setterName = toSetterName(field); + boolean returnThis = shouldReturnThis(field); if (setterName == null) return null; JCVariableDecl fieldDecl = (JCVariableDecl) field.get(); @@ -202,26 +205,53 @@ public class HandleSetter extends JavacAnnotationHandler<Setter> { JCExpression fieldRef = createFieldAccessor(treeMaker, field, FieldAccess.ALWAYS_FIELD); JCAssign assign = treeMaker.Assign(fieldRef, treeMaker.Ident(fieldDecl.name)); - List<JCStatement> statements; + ListBuffer<JCStatement> statements = ListBuffer.lb(); List<JCAnnotation> nonNulls = findAnnotations(field, TransformationsUtil.NON_NULL_PATTERN); List<JCAnnotation> nullables = findAnnotations(field, TransformationsUtil.NULLABLE_PATTERN); + Name methodName = field.toName(setterName); + List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables); + + JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); + if (nonNulls.isEmpty()) { - statements = List.<JCStatement>of(treeMaker.Exec(assign)); + statements.append(treeMaker.Exec(assign)); } else { JCStatement nullCheck = generateNullCheck(treeMaker, field); - if (nullCheck != null) statements = List.<JCStatement>of(nullCheck, treeMaker.Exec(assign)); - else statements = List.<JCStatement>of(treeMaker.Exec(assign)); + if (nullCheck != null) statements.append(nullCheck); + statements.append(treeMaker.Exec(assign)); } - JCBlock methodBody = treeMaker.Block(0, statements); - Name methodName = field.toName(setterName); - List<JCAnnotation> annsOnParam = nonNulls.appendList(nullables); + JCExpression methodType = null; + if (returnThis) { + JavacNode typeNode = field; + while (typeNode != null && typeNode.getKind() != Kind.TYPE) typeNode = typeNode.up(); + if (typeNode != null && typeNode.get() instanceof JCClassDecl) { + JCClassDecl type = (JCClassDecl) typeNode.get(); + ListBuffer<JCExpression> typeArgs = ListBuffer.lb(); + if (!type.typarams.isEmpty()) { + for (JCTypeParameter tp : type.typarams) { + typeArgs.append(treeMaker.Ident(tp.name)); + } + methodType = treeMaker.TypeApply(treeMaker.Ident(type.name), typeArgs.toList()); + } else { + methodType = treeMaker.Ident(type.name); + } + } + } - JCVariableDecl param = treeMaker.VarDef(treeMaker.Modifiers(Flags.FINAL, annsOnParam), fieldDecl.name, fieldDecl.vartype, null); - //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. - JCExpression methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID"))); + if (methodType == null) { + //WARNING: Do not use field.getSymbolTable().voidType - that field has gone through non-backwards compatible API changes within javac1.6. + methodType = treeMaker.Type(new JCNoType(getCtcInt(TypeTags.class, "VOID"))); + returnThis = false; + } + + if (returnThis) { + JCReturn returnStatement = treeMaker.Return(treeMaker.Ident(field.toName("this"))); + statements.append(returnStatement); + } + JCBlock methodBody = treeMaker.Block(0, statements.toList()); List<JCTypeParameter> methodGenericParams = List.nil(); List<JCVariableDecl> parameters = List.of(param); List<JCExpression> throwsClauses = List.nil(); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 92629a3c..8803ca3d 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -341,6 +341,20 @@ public class JavacHandlerUtil { return TransformationsUtil.toSetterName(accessors, fieldName, isBoolean); } + /** + * 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(JavacNode field) { + if ((((JCVariableDecl) field.get()).mods.flags & Flags.STATIC) != 0) return false; + + AnnotationValues<Accessors> accessors = JavacHandlerUtil.getAccessorsForField(field); + + boolean forced = (accessors.getActualExpression("chain") != null); + Accessors instance = accessors.getInstance(); + return instance.chain() || (instance.fluent() && !forced); + } + private static boolean isBoolean(JavacNode field) { JCExpression varType = ((JCVariableDecl) field.get()).vartype; return varType != null && varType.toString().equals("boolean"); @@ -355,7 +369,7 @@ public class JavacHandlerUtil { JavacNode current = field.up(); while (current != null) { - for (JavacNode node : field.down()) { + for (JavacNode node : current.down()) { if (annotationTypeMatches(Accessors.class, node)) { return createAnnotation(Accessors.class, node); } |