diff options
author | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-17 06:54:33 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@zwitserloot.com> | 2010-07-17 06:54:33 +0200 |
commit | 32a23d77866093c5ba97eefdd5482868cf505de5 (patch) | |
tree | f97477bdc0cc3547a90e6d2798a1f34337f30798 /src/core/lombok/eclipse | |
parent | 99f763a7294065333bee1d269dc28a744e072bcc (diff) | |
download | lombok-32a23d77866093c5ba97eefdd5482868cf505de5.tar.gz lombok-32a23d77866093c5ba97eefdd5482868cf505de5.tar.bz2 lombok-32a23d77866093c5ba97eefdd5482868cf505de5.zip |
Implements issue #129: @Getter and @Setter are now legal on entire types.
Diffstat (limited to 'src/core/lombok/eclipse')
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleData.java | 6 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleGetter.java | 44 | ||||
-rw-r--r-- | src/core/lombok/eclipse/handlers/HandleSetter.java | 45 |
3 files changed, 81 insertions, 14 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleData.java b/src/core/lombok/eclipse/handlers/HandleData.java index bbf1822e..68b4682c 100644 --- a/src/core/lombok/eclipse/handlers/HandleData.java +++ b/src/core/lombok/eclipse/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 @@ -127,8 +127,8 @@ public class HandleData implements EclipseAnnotationHandler<Data> { } for (Map.Entry<EclipseNode, 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/eclipse/handlers/HandleGetter.java b/src/core/lombok/eclipse/handlers/HandleGetter.java index d786f20f..43197ae4 100644 --- a/src/core/lombok/eclipse/handlers/HandleGetter.java +++ b/src/core/lombok/eclipse/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 @@ -62,7 +62,7 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> { * If not, the getter is still generated if it isn't already there, though there will not * be a warning if its already there. The default access level is used. */ - public void generateGetterForField(EclipseNode fieldNode, ASTNode pos) { + public void generateGetterForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, boolean checkForTypeLevelGetter) { for (EclipseNode child : fieldNode.down()) { if (child.getKind() == Kind.ANNOTATION) { if (annotationTypeMatches(Getter.class, child)) { @@ -72,15 +72,49 @@ public class HandleGetter implements EclipseAnnotationHandler<Getter> { } } - createGetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, pos, false); + if (checkForTypeLevelGetter) { + EclipseNode containingType = fieldNode.up(); + if (containingType != null) for (EclipseNode child : containingType.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (annotationTypeMatches(Getter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; + } + } + } + } + + createGetterForField(level, fieldNode, fieldNode, pos, false); } public boolean handle(AnnotationValues<Getter> annotation, Annotation ast, EclipseNode annotationNode) { - EclipseNode fieldNode = annotationNode.up(); + EclipseNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE) return true; - return createGetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); + if (node == null) return false; + if (node.getKind() == Kind.FIELD) { + return createGetterForField(level, node, annotationNode, annotationNode.get(), true); + } + if (node.getKind() == Kind.TYPE) { + TypeDeclaration typeDecl = null; + if (node.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) node.get(); + int modifiers = typeDecl == null ? 0 : typeDecl.modifiers; + boolean notAClass = (modifiers & + (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0; + + if (typeDecl == null || notAClass) { + annotationNode.addError("@Getter is only supported on a class."); + return false; + } + + for (EclipseNode 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/eclipse/handlers/HandleSetter.java b/src/core/lombok/eclipse/handlers/HandleSetter.java index 7760acd9..3dbbb2b5 100644 --- a/src/core/lombok/eclipse/handlers/HandleSetter.java +++ b/src/core/lombok/eclipse/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 @@ -69,7 +69,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { * If not, the setter is still generated if it isn't already there, though there will not * be a warning if its already there. The default access level is used. */ - public void generateSetterForField(EclipseNode fieldNode, ASTNode pos) { + public void generateSetterForField(EclipseNode fieldNode, ASTNode pos, AccessLevel level, boolean checkForTypeLevelSetter) { for (EclipseNode child : fieldNode.down()) { if (child.getKind() == Kind.ANNOTATION) { if (annotationTypeMatches(Setter.class, child)) { @@ -79,16 +79,49 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { } } - createSetterForField(AccessLevel.PUBLIC, fieldNode, fieldNode, pos, false); + if (checkForTypeLevelSetter) { + EclipseNode containingType = fieldNode.up(); + if (containingType != null) for (EclipseNode child : containingType.down()) { + if (child.getKind() == Kind.ANNOTATION) { + if (annotationTypeMatches(Setter.class, child)) { + //The annotation will make it happen, so we can skip it. + return; + } + } + } + } + + createSetterForField(level, fieldNode, fieldNode, pos, false); } public boolean handle(AnnotationValues<Setter> annotation, Annotation ast, EclipseNode annotationNode) { - EclipseNode fieldNode = annotationNode.up(); - if (fieldNode.getKind() != Kind.FIELD) return false; + EclipseNode node = annotationNode.up(); AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE) return true; - return createSetterForField(level, fieldNode, annotationNode, annotationNode.get(), true); + if (node == null) return false; + if (node.getKind() == Kind.FIELD) { + return createSetterForField(level, node, annotationNode, annotationNode.get(), true); + } + if (node.getKind() == Kind.TYPE) { + TypeDeclaration typeDecl = null; + if (node.get() instanceof TypeDeclaration) typeDecl = (TypeDeclaration) node.get(); + int modifiers = typeDecl == null ? 0 : typeDecl.modifiers; + boolean notAClass = (modifiers & + (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0; + + if (typeDecl == null || notAClass) { + annotationNode.addError("@Setter is only supported on a class."); + return false; + } + + for (EclipseNode field : node.down()) { + if (field.getKind() != Kind.FIELD) continue; + generateSetterForField(field, ast, level, false); + } + return true; + } + return false; } private boolean createSetterForField(AccessLevel level, |