aboutsummaryrefslogtreecommitdiff
path: root/src/core/lombok
diff options
context:
space:
mode:
authorRoel Spilker <r.spilker@gmail.com>2009-12-02 04:21:31 +0100
committerRoel Spilker <r.spilker@gmail.com>2009-12-02 04:21:31 +0100
commit9f69893ce2efd6ca0a6ef64022e48e49fc2b33f0 (patch)
tree46586ab9cbef48f10b06f841c5234ffb03f6c98a /src/core/lombok
parente3d9e116af1c573b27ba35ac07f0111aaa1882ac (diff)
downloadlombok-9f69893ce2efd6ca0a6ef64022e48e49fc2b33f0.tar.gz
lombok-9f69893ce2efd6ca0a6ef64022e48e49fc2b33f0.tar.bz2
lombok-9f69893ce2efd6ca0a6ef64022e48e49fc2b33f0.zip
Changed the order of the generated methods in for @Data
- Constructor(s) - Getters/Setters per field - equals - hashCode - toString Added first test for @Data
Diffstat (limited to 'src/core/lombok')
-rw-r--r--src/core/lombok/eclipse/handlers/HandleData.java27
-rw-r--r--src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java16
-rw-r--r--src/core/lombok/javac/handlers/HandleData.java24
3 files changed, 46 insertions, 21 deletions
diff --git a/src/core/lombok/eclipse/handlers/HandleData.java b/src/core/lombok/eclipse/handlers/HandleData.java
index 37b1e6f1..4e23bbf4 100644
--- a/src/core/lombok/eclipse/handlers/HandleData.java
+++ b/src/core/lombok/eclipse/handlers/HandleData.java
@@ -21,13 +21,21 @@
*/
package lombok.eclipse.handlers;
-import static lombok.eclipse.Eclipse.*;
-import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
+import static lombok.eclipse.Eclipse.copyAnnotations;
+import static lombok.eclipse.Eclipse.copyType;
+import static lombok.eclipse.Eclipse.copyTypeParams;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.constructorExists;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.findAnnotations;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.generateNullCheck;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.injectMethod;
+import static lombok.eclipse.handlers.EclipseHandlerUtil.methodExists;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import lombok.AccessLevel;
import lombok.Data;
@@ -84,6 +92,7 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
}
List<EclipseNode> nodesForConstructor = new ArrayList<EclipseNode>();
+ Map<EclipseNode, Boolean> gettersAndSetters = new LinkedHashMap<EclipseNode, Boolean>();
for (EclipseNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
@@ -94,13 +103,9 @@ public class HandleData implements EclipseAnnotationHandler<Data> {
boolean isFinal = (fieldDecl.modifiers & ClassFileConstants.AccFinal) != 0;
boolean isNonNull = findAnnotations(fieldDecl, TransformationsUtil.NON_NULL_PATTERN).length != 0;
if ((isFinal || isNonNull) && fieldDecl.initialization == null) nodesForConstructor.add(child);
- new HandleGetter().generateGetterForField(child, annotationNode.get());
- if (!isFinal) new HandleSetter().generateSetterForField(child, annotationNode.get());
+ gettersAndSetters.put(child, !isFinal);
}
- new HandleToString().generateToStringForType(typeNode, annotationNode);
- new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
-
//Careful: Generate the public static constructor (if there is one) LAST, so that any attempt to
//'find callers' on the annotation node will find callers of the constructor, which is by far the
//most useful of the many methods built by @Data. This trick won't work for the non-static constructor,
@@ -121,6 +126,14 @@ 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 HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
+ new HandleToString().generateToStringForType(typeNode, annotationNode);
+
return false;
}
diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
index 7c0980c8..2c636916 100644
--- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
+++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java
@@ -204,32 +204,32 @@ public class HandleEqualsAndHashCode implements EclipseAnnotationHandler<EqualsA
}
}
- switch (methodExists("hashCode", typeNode)) {
+ switch (methodExists("equals", typeNode)) {
case NOT_EXISTS:
- MethodDeclaration hashCode = createHashCode(typeNode, nodesForEquality, callSuper, errorNode.get());
- injectMethod(typeNode, hashCode);
+ MethodDeclaration equals = createEquals(typeNode, nodesForEquality, callSuper, errorNode.get());
+ injectMethod(typeNode, equals);
break;
case EXISTS_BY_LOMBOK:
break;
default:
case EXISTS_BY_USER:
if (whineIfExists) {
- errorNode.addWarning("Not generating hashCode(): A method with that name already exists");
+ errorNode.addWarning("Not generating equals(Object other): A method with that name already exists");
}
break;
}
- switch (methodExists("equals", typeNode)) {
+ switch (methodExists("hashCode", typeNode)) {
case NOT_EXISTS:
- MethodDeclaration equals = createEquals(typeNode, nodesForEquality, callSuper, errorNode.get());
- injectMethod(typeNode, equals);
+ MethodDeclaration hashCode = createHashCode(typeNode, nodesForEquality, callSuper, errorNode.get());
+ injectMethod(typeNode, hashCode);
break;
case EXISTS_BY_LOMBOK:
break;
default:
case EXISTS_BY_USER:
if (whineIfExists) {
- errorNode.addWarning("Not generating equals(Object other): A method with that name already exists");
+ errorNode.addWarning("Not generating hashCode(): A method with that name already exists");
}
break;
}
diff --git a/src/core/lombok/javac/handlers/HandleData.java b/src/core/lombok/javac/handlers/HandleData.java
index 54dd8dd2..128db8b0 100644
--- a/src/core/lombok/javac/handlers/HandleData.java
+++ b/src/core/lombok/javac/handlers/HandleData.java
@@ -21,9 +21,16 @@
*/
package lombok.javac.handlers;
-import static lombok.javac.handlers.JavacHandlerUtil.*;
+import static lombok.javac.handlers.JavacHandlerUtil.constructorExists;
+import static lombok.javac.handlers.JavacHandlerUtil.findAnnotations;
+import static lombok.javac.handlers.JavacHandlerUtil.generateNullCheck;
+import static lombok.javac.handlers.JavacHandlerUtil.injectMethod;
+import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed;
+import static lombok.javac.handlers.JavacHandlerUtil.methodExists;
import java.lang.reflect.Modifier;
+import java.util.LinkedHashMap;
+import java.util.Map;
import lombok.Data;
import lombok.core.AnnotationValues;
@@ -72,6 +79,7 @@ public class HandleData implements JavacAnnotationHandler<Data> {
}
List<JavacNode> nodesForConstructor = List.nil();
+ Map<JavacNode, Boolean> gettersAndSetters = new LinkedHashMap<JavacNode, Boolean>();
for (JavacNode child : typeNode.down()) {
if (child.getKind() != Kind.FIELD) continue;
JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
@@ -83,13 +91,9 @@ public class HandleData implements JavacAnnotationHandler<Data> {
boolean isFinal = (fieldFlags & Flags.FINAL) != 0;
boolean isNonNull = !findAnnotations(child, TransformationsUtil.NON_NULL_PATTERN).isEmpty();
if ((isFinal || isNonNull) && fieldDecl.init == null) nodesForConstructor = nodesForConstructor.append(child);
- new HandleGetter().generateGetterForField(child, annotationNode.get());
- if (!isFinal) new HandleSetter().generateSetterForField(child, annotationNode.get());
+ gettersAndSetters.put(child, !isFinal);
}
- new HandleToString().generateToStringForType(typeNode, annotationNode);
- new HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
-
String staticConstructorName = annotation.getInstance().staticConstructor();
if (constructorExists(typeNode) == MemberExistsResult.NOT_EXISTS) {
@@ -102,6 +106,14 @@ public class HandleData implements JavacAnnotationHandler<Data> {
injectMethod(typeNode, staticConstructor);
}
+ 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 HandleEqualsAndHashCode().generateEqualsAndHashCodeForType(typeNode, annotationNode);
+ new HandleToString().generateToStringForType(typeNode, annotationNode);
+
return true;
}