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/eclipse/handlers/HandleSetter.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/eclipse/handlers/HandleSetter.java')
-rw-r--r-- | src/lombok/eclipse/handlers/HandleSetter.java | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/src/lombok/eclipse/handlers/HandleSetter.java b/src/lombok/eclipse/handlers/HandleSetter.java index 692061e4..d0d0d902 100644 --- a/src/lombok/eclipse/handlers/HandleSetter.java +++ b/src/lombok/eclipse/handlers/HandleSetter.java @@ -15,36 +15,65 @@ 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.TypeReference; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; import org.eclipse.jdt.internal.compiler.lookup.MethodScope; import org.eclipse.jdt.internal.compiler.lookup.TypeIds; import org.mangosdk.spi.ProviderFor; +import lombok.AccessLevel; import lombok.Setter; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; +import lombok.core.AST.Kind; +import lombok.eclipse.Eclipse; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseAST.Node; @ProviderFor(EclipseAnnotationHandler.class) public class HandleSetter implements EclipseAnnotationHandler<Setter> { + public void generateSetterForField(Node fieldNode, ASTNode pos) { + AccessLevel level = Setter.DEFAULT_ACCESS_LEVEL; + Node errorNode = fieldNode; + + for ( Node child : fieldNode.down() ) { + if ( child.getKind() == Kind.ANNOTATION ) { + if ( Eclipse.annotationTypeMatches(Setter.class, child) ) { + level = Eclipse.createAnnotation(Setter.class, child).getInstance().value(); + errorNode = child; + pos = child.get(); + break; + } + } + } + + createSetterForField(level, fieldNode, errorNode, pos); + } + @Override public boolean handle(AnnotationValues<Setter> annotation, Annotation ast, Node annotationNode) { - if ( !(annotationNode.up().get() instanceof FieldDeclaration) ) return false; - FieldDeclaration field = (FieldDeclaration) annotationNode.up().get(); + Node fieldNode = annotationNode.up(); + if ( fieldNode.getKind() != Kind.FIELD ) return false; + AccessLevel level = annotation.getInstance().value(); + return createSetterForField(level, fieldNode, annotationNode, annotationNode.get()); + } + + private boolean createSetterForField(AccessLevel level, Node fieldNode, Node errorNode, ASTNode pos) { + if ( fieldNode.getKind() != Kind.FIELD ) return false; + FieldDeclaration field = (FieldDeclaration) fieldNode.get(); String setterName = TransformationsUtil.toSetterName(new String(field.name)); - TypeDeclaration parent = (TypeDeclaration) annotationNode.up().up().get(); + TypeDeclaration parent = (TypeDeclaration) fieldNode.up().get(); if ( parent.methods != null ) for ( AbstractMethodDeclaration method : parent.methods ) { if ( method.selector != null && new String(method.selector).equals(setterName) ) { - annotationNode.addWarning(String.format( + errorNode.addWarning(String.format( "Not generating %s(%s %s): A method with that name already exists", setterName, field.type, new String(field.name))); return false; } } - int modifier = toModifier(annotation.getInstance().value()); + int modifier = toModifier(level) | (field.modifiers & ClassFileConstants.AccStatic); - MethodDeclaration method = generateSetter(parent, field, setterName, modifier, ast); + MethodDeclaration method = generateSetter(parent, field, setterName, modifier, pos); if ( parent.methods == null ) { parent.methods = new AbstractMethodDeclaration[1]; @@ -60,7 +89,7 @@ public class HandleSetter implements EclipseAnnotationHandler<Setter> { } private MethodDeclaration generateSetter(TypeDeclaration parent, FieldDeclaration field, String name, - int modifier, Annotation ast) { + int modifier, ASTNode ast) { long pos = (((long)ast.sourceStart) << 32) | ast.sourceEnd; MethodDeclaration method = new MethodDeclaration(parent.compilationResult); method.modifiers = modifier; |