diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-19 16:24:10 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-19 16:24:10 +0200 |
commit | ff306fa5d97a09d69332c76a33596bb053222855 (patch) | |
tree | 00124b2190b36f13381d9513717e558aacbad973 /src/lombok/javac/handlers/HandleGetter.java | |
parent | e56ab6c2457531c0ce8556097f945b6e7946f6f2 (diff) | |
download | lombok-ff306fa5d97a09d69332c76a33596bb053222855.tar.gz lombok-ff306fa5d97a09d69332c76a33596bb053222855.tar.bz2 lombok-ff306fa5d97a09d69332c76a33596bb053222855.zip |
Added initial support for the @Data annotation. Currently produces getters and setters only,
not yet a constructor, toString, hashCode, or equals.
HandleGetter and HandleSetter have been updated to handle static (theoretic; you can't put annotations on static fields normally).
You can now make AnnotationValue objects using just an annotationNode and a target type, as well as check if a given annotationNode is likely to represent a target annotation type. This is in Javac and Eclipse classes.
HandleGetter and HandleSetter can now be asked to make a getter/setter, and will grab access level off of a Getter/Setter annotation, if present.
Diffstat (limited to 'src/lombok/javac/handlers/HandleGetter.java')
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 53 |
1 files changed, 40 insertions, 13 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 5cc2c108..651bc018 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -2,14 +2,17 @@ package lombok.javac.handlers; import static lombok.javac.handlers.PKG.*; +import lombok.AccessLevel; import lombok.Getter; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; +import lombok.javac.Javac; import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacAST; +import lombok.javac.JavacAST.Node; import org.mangosdk.spi.ProviderFor; +import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBlock; @@ -21,35 +24,59 @@ import com.sun.tools.javac.tree.JCTree.JCTypeParameter; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.Name; +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; @ProviderFor(JavacAnnotationHandler.class) public class HandleGetter implements JavacAnnotationHandler<Getter> { - @Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacAST.Node annotationNode) { - if ( annotationNode.up().getKind() != Kind.FIELD ) { - annotationNode.addError("@Getter is only supported on a field."); + public void generateGetterForField(Node fieldNode, DiagnosticPosition pos) { + AccessLevel level = Getter.DEFAULT_ACCESS_LEVEL; + Node errorNode = fieldNode; + + for ( Node child : fieldNode.down() ) { + if ( child.getKind() == Kind.ANNOTATION ) { + if ( Javac.annotationTypeMatches(Getter.class, child) ) { + level = Javac.createAnnotation(Getter.class, child).getInstance().value(); + errorNode = child; + pos = child.get(); + break; + } + } + } + + createGetterForField(level, fieldNode, errorNode, pos); + } + + @Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, Node annotationNode) { + Node fieldNode = annotationNode.up(); + AccessLevel level = annotation.getInstance().value(); + return createGetterForField(level, fieldNode, annotationNode, annotationNode.get()); + } + + private boolean createGetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos) { + if ( fieldNode.getKind() != Kind.FIELD ) { + errorNode.addError("@Getter is only supported on a field."); return false; } - String methodName = toGetterName((JCVariableDecl) annotationNode.up().get()); + JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); + String methodName = toGetterName(fieldDecl); - if ( methodExists(methodName, annotationNode.up()) ) { - annotationNode.addWarning( + if ( methodExists(methodName, fieldNode) ) { + errorNode.addWarning( String.format("Not generating %s(): A method with that name already exists", methodName)); return false; } - Getter getter = annotation.getInstance(); - - JCClassDecl javacClassTree = (JCClassDecl) annotationNode.up().up().get(); + JCClassDecl javacClassTree = (JCClassDecl) fieldNode.up().get(); - int access = toJavacModifier(getter.value()); + long access = toJavacModifier(level) | (fieldDecl.mods.flags & Flags.STATIC); - JCMethodDecl getterMethod = createGetter(access, annotationNode.up(), annotationNode.getTreeMaker()); + JCMethodDecl getterMethod = createGetter(access, fieldNode, fieldNode.getTreeMaker()); javacClassTree.defs = javacClassTree.defs.append(getterMethod); return true; } - private JCMethodDecl createGetter(int access, JavacAST.Node field, TreeMaker treeMaker) { + private JCMethodDecl createGetter(long access, Node field, TreeMaker treeMaker) { JCVariableDecl fieldNode = (JCVariableDecl) field.get(); JCStatement returnStatement = treeMaker.Return(treeMaker.Ident(fieldNode.getName())); |