diff options
Diffstat (limited to 'src/core/lombok/javac/handlers')
-rw-r--r-- | src/core/lombok/javac/handlers/HandleData.java | 7 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleGetter.java | 45 | ||||
-rw-r--r-- | src/core/lombok/javac/handlers/HandleSetter.java | 44 |
3 files changed, 82 insertions, 14 deletions
diff --git a/src/core/lombok/javac/handlers/HandleData.java b/src/core/lombok/javac/handlers/HandleData.java index 88c0553c..efd86bba 100644 --- a/src/core/lombok/javac/handlers/HandleData.java +++ b/src/core/lombok/javac/handlers/HandleData.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -32,6 +32,7 @@ import java.lang.reflect.Modifier; import java.util.LinkedHashMap; import java.util.Map; +import lombok.AccessLevel; import lombok.Data; import lombok.core.AnnotationValues; import lombok.core.AST.Kind; @@ -107,8 +108,8 @@ public class HandleData implements JavacAnnotationHandler<Data> { } for (Map.Entry<JavacNode, Boolean> field : gettersAndSetters.entrySet()) { - new HandleGetter().generateGetterForField(field.getKey(), annotationNode.get()); - if (field.getValue()) new HandleSetter().generateSetterForField(field.getKey(), annotationNode.get()); + new HandleGetter().generateGetterForField(field.getKey(), annotationNode.get(), AccessLevel.PUBLIC, true); + if (field.getValue()) new HandleSetter().generateSetterForField(field.getKey(), annotationNode.get(), AccessLevel.PUBLIC, true); } new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode); diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 14570187..5af05c7c 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -37,6 +37,7 @@ 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; +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.JCStatement; @@ -66,7 +67,7 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { * @param fieldNode The node representing the field you want a getter for. * @param pos The node responsible for generating the getter (the {@code @Data} or {@code @Getter} annotation). */ - public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos) { + public void generateGetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, boolean checkForTypeLevelGetter) { for (JavacNode child : fieldNode.down()) { if (child.getKind() == Kind.ANNOTATION) { if (Javac.annotationTypeMatches(Getter.class, child)) { @@ -76,17 +77,49 @@ public class HandleGetter implements JavacAnnotationHandler<Getter> { } } - createGetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, false); + if (checkForTypeLevelGetter) { + JavacNode containingType = fieldNode.up(); + if (containingType != null) for (JavacNode child : containingType.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(Getter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; + } + } + } + } + + createGetterForField(level, fieldNode, fieldNode, false); } @Override public boolean handle(AnnotationValues<Getter> annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, Getter.class); - JavacNode fieldNode = annotationNode.up(); + JavacNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); - if (level == AccessLevel.NONE) return true; - return createGetterForField(level, fieldNode, annotationNode, true); + if (node == null) return false; + if (node.getKind() == Kind.FIELD) { + return createGetterForField(level, node, annotationNode, true); + } + if (node.getKind() == Kind.TYPE) { + JCClassDecl typeDecl = null; + if (node.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) node.get(); + long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags; + boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; + + if (typeDecl == null || notAClass) { + annotationNode.addError("@Getter is only supported on a class."); + return false; + } + + for (JavacNode field : node.down()) { + if (field.getKind() != Kind.FIELD) continue; + generateGetterForField(field, ast, level, false); + } + return true; + } + return false; } private boolean createGetterForField(AccessLevel level, diff --git a/src/core/lombok/javac/handlers/HandleSetter.java b/src/core/lombok/javac/handlers/HandleSetter.java index e7f20f6c..a3822daa 100644 --- a/src/core/lombok/javac/handlers/HandleSetter.java +++ b/src/core/lombok/javac/handlers/HandleSetter.java @@ -1,5 +1,5 @@ /* - * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * Copyright © 2009-2010 Reinier Zwitserloot and Roel Spilker. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -46,6 +46,7 @@ import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCAssign; 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.JCFieldAccess; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; @@ -76,7 +77,7 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { * @param fieldNode The node representing the field you want a setter for. * @param pos The node responsible for generating the setter (the {@code @Data} or {@code @Setter} annotation). */ - public void generateSetterForField(JavacNode fieldNode, DiagnosticPosition pos) { + public void generateSetterForField(JavacNode fieldNode, DiagnosticPosition pos, AccessLevel level, boolean checkForTypeLevelSetter) { for (JavacNode child : fieldNode.down()) { if (child.getKind() == Kind.ANNOTATION) { if (Javac.annotationTypeMatches(Setter.class, child)) { @@ -86,17 +87,50 @@ public class HandleSetter implements JavacAnnotationHandler<Setter> { } } - createSetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, false); + if (checkForTypeLevelSetter) { + JavacNode containingType = fieldNode.up(); + if (containingType != null) for (JavacNode child : containingType.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (Javac.annotationTypeMatches(Setter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; + } + } + } + } + + createSetterForField(level, fieldNode, fieldNode, false); } @Override public boolean handle(AnnotationValues<Setter> annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, Setter.class); - JavacNode fieldNode = annotationNode.up(); + JavacNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE) return true; - return createSetterForField(level, fieldNode, annotationNode, true); + if (node == null) return false; + if (node.getKind() == Kind.FIELD) { + return createSetterForField(level, node, annotationNode, true); + } + if (node.getKind() == Kind.TYPE) { + JCClassDecl typeDecl = null; + if (node.get() instanceof JCClassDecl) typeDecl = (JCClassDecl) node.get(); + long modifiers = typeDecl == null ? 0 : typeDecl.mods.flags; + boolean notAClass = (modifiers & (Flags.INTERFACE | Flags.ANNOTATION | Flags.ENUM)) != 0; + + if (typeDecl == null || notAClass) { + annotationNode.addError("@Setter is only supported on a class."); + return false; + } + + for (JavacNode field : node.down()) { + if (field.getKind() != Kind.FIELD) continue; + generateSetterForField(field, ast, level, false); + } + return true; + } + return false; } private boolean createSetterForField(AccessLevel level, |