From 12d44e59c57e4654a7c61f83baf606a39fbb8448 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sat, 11 Jul 2009 11:39:45 +0200 Subject: 'fixed' data up a bit by including only the final fields for the constructor. Also fixed a bug in javac's toString() generation for the @Data constructor. It did not include the transient fields. --- src/lombok/Data.java | 4 ++++ src/lombok/eclipse/handlers/HandleData.java | 16 +++++++++------- src/lombok/javac/handlers/HandleData.java | 16 +++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/lombok/Data.java b/src/lombok/Data.java index 7d010e81..fa424c24 100644 --- a/src/lombok/Data.java +++ b/src/lombok/Data.java @@ -33,6 +33,10 @@ import java.lang.annotation.Target; * If any method to be generated already exists (in name - the return type or parameters are not relevant), then * that method will not be generated by the Data annotation. *

+ * The generated constructor will have 1 parameter for each final field. The generated toString will print all fields, + * while the generated hashCode and equals take into account all non-transient fields.
+ * Static fields are skipped (no getter or setter, and they are not included in toString, equals, hashCode, or the constructor). + *

* toString, equals, and hashCode use the deepX variants in the * java.util.Arrays utility class. Therefore, if your class has arrays that contain themselves, * these methods will just loop endlessly until the inevitable StackOverflowError. This behaviour diff --git a/src/lombok/eclipse/handlers/HandleData.java b/src/lombok/eclipse/handlers/HandleData.java index c7f053e6..066361d2 100644 --- a/src/lombok/eclipse/handlers/HandleData.java +++ b/src/lombok/eclipse/handlers/HandleData.java @@ -109,34 +109,36 @@ public class HandleData implements EclipseAnnotationHandler { } List nodesForEquality = new ArrayList(); - List nodesForConstructorAndToString = new ArrayList(); + List nodesForConstructor = new ArrayList(); + List nodesForToString = 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); - nodesForConstructorAndToString.add(child); + boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0; + nodesForToString.add(child); + if ( isFinal ) nodesForConstructor.add(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); - if ( (fieldDecl.modifiers & ClassFileConstants.AccFinal) == 0 ) - new HandleSetter().generateSetterForField(child, annotationNode.get()); + if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); } if ( methodExists("toString", typeNode) == MemberExistsResult.NOT_EXISTS ) { - MethodDeclaration toString = createToString(typeNode, nodesForConstructorAndToString, ast); + MethodDeclaration toString = createToString(typeNode, nodesForToString, ast); injectMethod(typeNode, toString); } if ( constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS ) { ConstructorDeclaration constructor = createConstructor( - ann.staticConstructor().length() == 0, typeNode, nodesForConstructorAndToString, ast); + ann.staticConstructor().length() == 0, typeNode, nodesForConstructor, ast); injectMethod(typeNode, constructor); } if ( ann.staticConstructor().length() > 0 ) { if ( methodExists("of", typeNode) == MemberExistsResult.NOT_EXISTS ) { MethodDeclaration staticConstructor = createStaticConstructor( - ann.staticConstructor(), typeNode, nodesForConstructorAndToString, ast); + ann.staticConstructor(), typeNode, nodesForConstructor, ast); injectMethod(typeNode, staticConstructor); } } diff --git a/src/lombok/javac/handlers/HandleData.java b/src/lombok/javac/handlers/HandleData.java index d3974c58..41165600 100644 --- a/src/lombok/javac/handlers/HandleData.java +++ b/src/lombok/javac/handlers/HandleData.java @@ -76,7 +76,8 @@ public class HandleData implements JavacAnnotationHandler { } List nodesForEquality = List.nil(); - List nodesForConstructorAndToString = List.nil(); + List nodesForConstructor = List.nil(); + List nodesForToString = List.nil(); for ( Node child : typeNode.down() ) { if ( child.getKind() != Kind.FIELD ) continue; JCVariableDecl fieldDecl = (JCVariableDecl) child.get(); @@ -84,21 +85,22 @@ public class HandleData implements JavacAnnotationHandler { //Skip static fields. if ( (fieldFlags & Flags.STATIC) != 0 ) continue; if ( (fieldFlags & Flags.TRANSIENT) == 0 ) nodesForEquality = nodesForEquality.append(child); - nodesForConstructorAndToString = nodesForConstructorAndToString.append(child); + boolean isFinal = (fieldFlags & Flags.FINAL) != 0; + nodesForToString = nodesForToString.append(child); + if ( isFinal ) nodesForConstructor = nodesForConstructor.append(child); new HandleGetter().generateGetterForField(child, annotationNode.get()); - if ( (fieldFlags & Flags.FINAL) == 0 ) - new HandleSetter().generateSetterForField(child, annotationNode.get()); + if ( !isFinal ) new HandleSetter().generateSetterForField(child, annotationNode.get()); } String staticConstructorName = annotation.getInstance().staticConstructor(); if ( constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS ) { - JCMethodDecl constructor = createConstructor(staticConstructorName.equals(""), typeNode, nodesForConstructorAndToString); + JCMethodDecl constructor = createConstructor(staticConstructorName.equals(""), typeNode, nodesForConstructor); injectMethod(typeNode, constructor); } if ( !staticConstructorName.isEmpty() && methodExists("of", typeNode) == MemberExistsResult.NOT_EXISTS ) { - JCMethodDecl staticConstructor = createStaticConstructor(staticConstructorName, typeNode, nodesForConstructorAndToString); + JCMethodDecl staticConstructor = createStaticConstructor(staticConstructorName, typeNode, nodesForConstructor); injectMethod(typeNode, staticConstructor); } @@ -113,7 +115,7 @@ public class HandleData implements JavacAnnotationHandler { } if ( methodExists("toString", typeNode) == MemberExistsResult.NOT_EXISTS ) { - JCMethodDecl method = createToString(typeNode, nodesForEquality); + JCMethodDecl method = createToString(typeNode, nodesForToString); injectMethod(typeNode, method); } -- cgit