From ff306fa5d97a09d69332c76a33596bb053222855 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Fri, 19 Jun 2009 16:24:10 +0200 Subject: 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. --- src/lombok/eclipse/handlers/HandleData.java | 49 +++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/lombok/eclipse/handlers/HandleData.java (limited to 'src/lombok/eclipse/handlers/HandleData.java') diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java new file mode 100644 index 00000000..64540e95 --- /dev/null +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -0,0 +1,49 @@ +package lombok.eclipse.handlers; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.jdt.internal.compiler.ast.Annotation; +import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; +import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; +import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; +import org.mangosdk.spi.ProviderFor; + +import lombok.Data; +import lombok.core.AnnotationValues; +import lombok.core.AST.Kind; +import lombok.eclipse.EclipseAnnotationHandler; +import lombok.eclipse.EclipseAST.Node; + +@ProviderFor(EclipseAnnotationHandler.class) +public class HandleData implements EclipseAnnotationHandler { + @Override public boolean handle(AnnotationValues annotation, Annotation ast, Node annotationNode) { + Node typeNode = annotationNode.up(); + + TypeDeclaration typeDecl = null; + if ( typeNode.get() instanceof TypeDeclaration ) typeDecl = (TypeDeclaration) typeNode.get(); + int modifiers = typeDecl == null ? 0 : typeDecl.modifiers; + boolean notAClass = (modifiers & + (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation | ClassFileConstants.AccEnum)) != 0; + + if ( typeDecl == null || notAClass ) { + annotationNode.addError("@Data is only supported on a class."); + return false; + } + + List nodesForEquality = new ArrayList(); + for ( Node child : typeNode.down() ) { + if ( child.getKind() != Kind.FIELD ) continue; + FieldDeclaration fieldDecl = (FieldDeclaration) child.get(); + //Skip static fields. + if ( (fieldDecl.modifiers & ClassFileConstants.AccStatic) != 0 ) continue; + if ( (fieldDecl.modifiers & ClassFileConstants.AccTransient) == 0 ) nodesForEquality.add(child); + new HandleGetter().generateGetterForField(child, annotationNode.get()); + if ( (fieldDecl.modifiers & ClassFileConstants.AccFinal) == 0 ) + new HandleSetter().generateSetterForField(child, annotationNode.get()); + } + + //TODO generate constructor, hashCode, equals, toString. + return true; + } +} -- cgit