diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-20 23:46:17 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-06-21 00:10:58 +0200 |
commit | 255bd5907176cb5d2c4fb1cf6a9b48b14af0b4ba (patch) | |
tree | 347c4b1cf5b34b3975cc00e26a42c75ee1295b76 /src/lombok/javac/handlers | |
parent | 2e8e43a12e21151ff470a2729373b4af4980d113 (diff) | |
download | lombok-255bd5907176cb5d2c4fb1cf6a9b48b14af0b4ba.tar.gz lombok-255bd5907176cb5d2c4fb1cf6a9b48b14af0b4ba.tar.bz2 lombok-255bd5907176cb5d2c4fb1cf6a9b48b14af0b4ba.zip |
Due to a java bug, constants in enums don't work, so instead the default access level for @Getter and @Setter have now just been hardcoded in GetterHandler and SetterHandler.
Added ability to look up the Node object for any given AST object on Node itself, as you don't usually have the AST object.
Added toString() method generating to @Data, and this required some fancy footwork in finding if we've already generated methods, and editing a generated method to fill in binding and type resolutions. HandleGetter and HandleSetter have been updated to use these features.
Exceptions caused by lombok handlers show up in the eclipse error log, but now, if they are related to a CompilationUnit, also as a problem (error) on the CUD - those error log entries are easy to miss!
Our ASTs can now be appended to. When you generate a new AST node, you should add it to the AST, obviously. Getter/Setter have been updated to use this.
Diffstat (limited to 'src/lombok/javac/handlers')
-rw-r--r-- | src/lombok/javac/handlers/HandleGetter.java | 23 | ||||
-rw-r--r-- | src/lombok/javac/handlers/HandleSetter.java | 25 | ||||
-rw-r--r-- | src/lombok/javac/handlers/PKG.java | 12 |
3 files changed, 44 insertions, 16 deletions
diff --git a/src/lombok/javac/handlers/HandleGetter.java b/src/lombok/javac/handlers/HandleGetter.java index 651bc018..3405810b 100644 --- a/src/lombok/javac/handlers/HandleGetter.java +++ b/src/lombok/javac/handlers/HandleGetter.java @@ -29,8 +29,9 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; @ProviderFor(JavacAnnotationHandler.class) public class HandleGetter implements JavacAnnotationHandler<Getter> { public void generateGetterForField(Node fieldNode, DiagnosticPosition pos) { - AccessLevel level = Getter.DEFAULT_ACCESS_LEVEL; + AccessLevel level = AccessLevel.PUBLIC; Node errorNode = fieldNode; + boolean whineIfExists = false; for ( Node child : fieldNode.down() ) { if ( child.getKind() == Kind.ANNOTATION ) { @@ -38,21 +39,22 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { level = Javac.createAnnotation(Getter.class, child).getInstance().value(); errorNode = child; pos = child.get(); + whineIfExists = true; break; } } } - createGetterForField(level, fieldNode, errorNode, pos); + createGetterForField(level, fieldNode, errorNode, pos, whineIfExists); } @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()); + return createGetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); } - private boolean createGetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos) { + private boolean createGetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos, boolean whineIfExists) { if ( fieldNode.getKind() != Kind.FIELD ) { errorNode.addError("@Getter is only supported on a field."); return false; @@ -61,10 +63,16 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); String methodName = toGetterName(fieldDecl); - if ( methodExists(methodName, fieldNode) ) { - errorNode.addWarning( + switch ( methodExists(methodName, fieldNode) ) { + case EXISTS_BY_LOMBOK: + return true; + case EXISTS_BY_USER: + if ( whineIfExists ) errorNode.addWarning( String.format("Not generating %s(): A method with that name already exists", methodName)); return false; + default: + case NOT_EXISTS: + //continue with creating the getter } JCClassDecl javacClassTree = (JCClassDecl) fieldNode.up().get(); @@ -73,6 +81,9 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { JCMethodDecl getterMethod = createGetter(access, fieldNode, fieldNode.getTreeMaker()); javacClassTree.defs = javacClassTree.defs.append(getterMethod); + + fieldNode.up().add(getterMethod, Kind.METHOD).recursiveSetHandled(); + return true; } diff --git a/src/lombok/javac/handlers/HandleSetter.java b/src/lombok/javac/handlers/HandleSetter.java index f1e73489..4999b5d8 100644 --- a/src/lombok/javac/handlers/HandleSetter.java +++ b/src/lombok/javac/handlers/HandleSetter.java @@ -31,8 +31,9 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; @ProviderFor(JavacAnnotationHandler.class) public class HandleSetter implements JavacAnnotationHandler<Setter> { public void generateSetterForField(Node fieldNode, DiagnosticPosition pos) { - AccessLevel level = Setter.DEFAULT_ACCESS_LEVEL; + AccessLevel level = AccessLevel.PUBLIC; Node errorNode = fieldNode; + boolean whineIfExists = false; for ( Node child : fieldNode.down() ) { if ( child.getKind() == Kind.ANNOTATION ) { @@ -40,21 +41,22 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { level = Javac.createAnnotation(Setter.class, child).getInstance().value(); errorNode = child; pos = child.get(); + whineIfExists = true; break; } } } - createSetterForField(level, fieldNode, errorNode, pos); + createSetterForField(level, fieldNode, errorNode, pos, whineIfExists); } @Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, Node annotationNode) { Node fieldNode = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); - return createSetterForField(level, fieldNode, annotationNode, annotationNode.get()); + return createSetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); } - private boolean createSetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos) { + private boolean createSetterForField(AccessLevel level, Node fieldNode, Node errorNode, DiagnosticPosition pos, boolean whineIfExists) { if ( fieldNode.getKind() != Kind.FIELD ) { fieldNode.addError("@Setter is only supported on a field."); return false; @@ -63,11 +65,17 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCVariableDecl fieldDecl = (JCVariableDecl)fieldNode.get(); String methodName = toSetterName(fieldDecl); - if ( methodExists(methodName, fieldNode) ) { - errorNode.addWarning( + switch ( methodExists(methodName, fieldNode) ) { + case EXISTS_BY_LOMBOK: + return true; + case EXISTS_BY_USER: + if ( whineIfExists ) errorNode.addWarning( String.format("Not generating %s(%s %s): A method with that name already exists", - methodName, fieldDecl.vartype, fieldDecl.name)); + methodName, fieldDecl.vartype, fieldDecl.name)); return false; + default: + case NOT_EXISTS: + //continue with creating the setter } JCClassDecl javacClassTree = (JCClassDecl) fieldNode.up().get(); @@ -76,6 +84,9 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { JCMethodDecl setterMethod = createSetter(access, fieldNode, fieldNode.getTreeMaker()); javacClassTree.defs = javacClassTree.defs.append(setterMethod); + + fieldNode.up().add(setterMethod, Kind.METHOD).recursiveSetHandled(); + return true; } diff --git a/src/lombok/javac/handlers/PKG.java b/src/lombok/javac/handlers/PKG.java index 2c038b2d..138559a9 100644 --- a/src/lombok/javac/handlers/PKG.java +++ b/src/lombok/javac/handlers/PKG.java @@ -28,7 +28,11 @@ class PKG { return TransformationsUtil.toSetterName(fieldName); } - static boolean methodExists(String methodName, JavacAST.Node node) { + enum MethodExistsResult { + NOT_EXISTS, EXISTS_BY_USER, EXISTS_BY_LOMBOK; + } + + static MethodExistsResult methodExists(String methodName, JavacAST.Node node) { while ( node != null && !(node.get() instanceof JCClassDecl) ) { node = node.up(); } @@ -37,13 +41,15 @@ class PKG { for ( JCTree def : ((JCClassDecl)node.get()).defs ) { if ( def instanceof JCMethodDecl ) { if ( ((JCMethodDecl)def).name.contentEquals(methodName) ) { - return true; + JavacAST.Node existing = node.getNodeFor(def); + if ( existing == null || !existing.isHandled() ) return MethodExistsResult.EXISTS_BY_USER; + return MethodExistsResult.EXISTS_BY_LOMBOK; } } } } - return false; + return MethodExistsResult.NOT_EXISTS; } static int toJavacModifier(AccessLevel accessLevel) { |