diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-02 10:27:31 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-02 10:27:31 +0100 |
commit | db212a2fa08bf43b782cb422526cb3b4f809e295 (patch) | |
tree | 22f7fc606ec8b6b13c0ed0dd2e1470383724f8f7 /src/core | |
parent | bc88c1f99b1d0972f5a209a6de81739b6e00e61e (diff) | |
parent | 9f69893ce2efd6ca0a6ef64022e48e49fc2b33f0 (diff) | |
download | lombok-db212a2fa08bf43b782cb422526cb3b4f809e295.tar.gz lombok-db212a2fa08bf43b782cb422526cb3b4f809e295.tar.bz2 lombok-db212a2fa08bf43b782cb422526cb3b4f809e295.zip |
Merge branch 'master' of git@github.com:rzwitserloot/lombok
Diffstat (limited to 'src/core')
6 files changed, 57 insertions, 32 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/HandlerLibrary.java b/src/core/lombok/javac/HandlerLibrary.java index 48218513..76fbb8ab 100644 --- a/src/core/lombok/javac/HandlerLibrary.java +++ b/src/core/lombok/javac/HandlerLibrary.java @@ -165,7 +165,6 @@ public class HandlerLibrary { if (container == null) continue; try { - System.out.println("Calling handle on: "+ container.handler.getClass().getName()); handled |= container.handle(node); } catch (AnnotationValueDecodeFail fail) { fail.owner.setError(fail.getMessage(), fail.idx); 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; } diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index f388336d..4ee24391 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -167,9 +167,9 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd } } - switch (methodExists("hashCode", typeNode)) { + switch (methodExists("equals", typeNode)) { case NOT_EXISTS: - JCMethodDecl method = createHashCode(typeNode, nodesForEquality, callSuper); + JCMethodDecl method = createEquals(typeNode, nodesForEquality, callSuper); injectMethod(typeNode, method); break; case EXISTS_BY_LOMBOK: @@ -177,14 +177,14 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd 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: - JCMethodDecl method = createEquals(typeNode, nodesForEquality, callSuper); + JCMethodDecl method = createHashCode(typeNode, nodesForEquality, callSuper); injectMethod(typeNode, method); break; case EXISTS_BY_LOMBOK: @@ -192,7 +192,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd 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; } @@ -326,7 +326,7 @@ public class HandleEqualsAndHashCode implements JavacAnnotationHandler<EqualsAnd JCAnnotation overrideAnnotation = maker.Annotation(chainDots(maker, typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); - JCExpression objectType = maker.Type(typeNode.getSymbolTable().objectType); + JCExpression objectType = chainDots(maker, typeNode, "java", "lang", "Object"); JCExpression returnType = maker.TypeIdent(TypeTags.BOOLEAN); List<JCStatement> statements = List.nil(); diff --git a/src/core/lombok/javac/handlers/HandleSneakyThrows.java b/src/core/lombok/javac/handlers/HandleSneakyThrows.java index 8a185e87..fda8805c 100644 --- a/src/core/lombok/javac/handlers/HandleSneakyThrows.java +++ b/src/core/lombok/javac/handlers/HandleSneakyThrows.java @@ -26,6 +26,7 @@ import static lombok.javac.handlers.JavacHandlerUtil.markAnnotationAsProcessed; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import lombok.SneakyThrows; import lombok.core.AnnotationValues; @@ -52,9 +53,9 @@ public class HandleSneakyThrows implements JavacAnnotationHandler<SneakyThrows> @Override public boolean handle(AnnotationValues<SneakyThrows> annotation, JCAnnotation ast, JavacNode annotationNode) { markAnnotationAsProcessed(annotationNode, SneakyThrows.class); Collection<String> exceptionNames = annotation.getRawExpressions("value"); - - List<JCExpression> memberValuePairs = ast.getArguments(); - if (memberValuePairs == null || memberValuePairs.size() == 0) return false; + if (exceptionNames.isEmpty()) { + exceptionNames = Collections.singleton("java.lang.Throwable"); + } java.util.List<String> exceptions = new ArrayList<String>(); for (String exception : exceptionNames) { |