From 105c32f4b69f00f47fdc05990d47d4e851058e0a Mon Sep 17 00:00:00 2001 From: Bulgakov Alexander Date: Sun, 13 Nov 2016 22:41:18 +0300 Subject: fix of the issue 1230. @Tolerate not working for @Builder methods? --- .../eclipse/handlers/EclipseHandlerUtil.java | 16 +++--- .../lombok/eclipse/handlers/HandleBuilder.java | 2 +- src/core/lombok/javac/handlers/HandleBuilder.java | 5 +- .../lombok/javac/handlers/JavacHandlerUtil.java | 18 ++++--- .../after-delombok/BuilderWithTolerate.java | 59 ++++++++++++++++++++++ .../resource/after-ecj/BuilderWithTolerate.java | 34 +++++++++++++ .../resource/before/BuilderWithTolerate.java | 18 +++++++ 7 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 test/transform/resource/after-delombok/BuilderWithTolerate.java create mode 100644 test/transform/resource/after-ecj/BuilderWithTolerate.java create mode 100644 test/transform/resource/before/BuilderWithTolerate.java diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 0ff5a7f6..59d8f587 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1168,9 +1168,8 @@ public class EclipseHandlerUtil { if (params < minArgs || params > maxArgs) continue; } - if (def.annotations != null) for (Annotation anno : def.annotations) { - if (typeMatches(Tolerate.class, node, anno.type)) continue top; - } + + if (isTolerate(node, def)) continue top; return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } @@ -1181,6 +1180,13 @@ public class EclipseHandlerUtil { return MemberExistsResult.NOT_EXISTS; } + public static boolean isTolerate(EclipseNode node, AbstractMethodDeclaration def) { + if (def.annotations != null) for (Annotation anno : def.annotations) { + if (typeMatches(Tolerate.class, node, anno.type)) return true; + } + return false; + } + /** * Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only * the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned. @@ -1198,9 +1204,7 @@ public class EclipseHandlerUtil { if (def instanceof ConstructorDeclaration) { if ((def.bits & ASTNode.IsDefaultConstructor) != 0) continue; - if (def.annotations != null) for (Annotation anno : def.annotations) { - if (typeMatches(Tolerate.class, node, anno.type)) continue top; - } + if (isTolerate(node, def)) continue; return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } diff --git a/src/core/lombok/eclipse/handlers/HandleBuilder.java b/src/core/lombok/eclipse/handlers/HandleBuilder.java index 4e0c4218..afa03538 100644 --- a/src/core/lombok/eclipse/handlers/HandleBuilder.java +++ b/src/core/lombok/eclipse/handlers/HandleBuilder.java @@ -652,7 +652,7 @@ public class HandleBuilder extends EclipseAnnotationHandler { for (int i = 0; i < len; i++) { if (!(existing[i] instanceof MethodDeclaration)) continue; char[] existingName = existing[i].selector; - if (Arrays.equals(name, existingName)) return; + if (Arrays.equals(name, existingName) && !isTolerate(fieldNode, existing[i])) return; } String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName()); diff --git a/src/core/lombok/javac/handlers/HandleBuilder.java b/src/core/lombok/javac/handlers/HandleBuilder.java index b83e3d5f..9c3c9d03 100644 --- a/src/core/lombok/javac/handlers/HandleBuilder.java +++ b/src/core/lombok/javac/handlers/HandleBuilder.java @@ -608,8 +608,9 @@ public class HandleBuilder extends JavacAnnotationHandler { for (JavacNode child : builderType.down()) { if (child.getKind() != Kind.METHOD) continue; - Name existingName = ((JCMethodDecl) child.get()).name; - if (existingName.equals(fieldName)) return; + JCMethodDecl methodDecl = (JCMethodDecl) child.get(); + Name existingName = methodDecl.name; + if (existingName.equals(fieldName) && !isTolerate(fieldNode, methodDecl)) return; } String setterName = fluent ? fieldNode.getName() : HandlerUtil.buildAccessorName("set", fieldNode.getName()); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index fdc8a262..efa67604 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -605,10 +605,7 @@ public class JavacHandlerUtil { if (params < minArgs || params > maxArgs) continue; } - List annotations = md.getModifiers().getAnnotations(); - if (annotations != null) for (JCAnnotation anno : annotations) { - if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) continue top; - } + if (isTolerate(node, md)) continue top; return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } @@ -619,6 +616,14 @@ public class JavacHandlerUtil { return MemberExistsResult.NOT_EXISTS; } + public static boolean isTolerate(JavacNode node, JCTree.JCMethodDecl md) { + List annotations = md.getModifiers().getAnnotations(); + if (annotations != null) for (JCTree.JCAnnotation anno : annotations) { + if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) return true; + } + return false; + } + /** * Checks if there is a (non-default) constructor. In case of multiple constructors (overloading), only * the first constructor decides if EXISTS_BY_USER or EXISTS_BY_LOMBOK is returned. @@ -634,10 +639,7 @@ public class JavacHandlerUtil { JCMethodDecl md = (JCMethodDecl) def; if (md.name.contentEquals("")) { if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) continue; - List annotations = md.getModifiers().getAnnotations(); - if (annotations != null) for (JCAnnotation anno : annotations) { - if (typeMatches(Tolerate.class, node, anno.getAnnotationType())) continue top; - } + if (isTolerate(node, md)) continue; return getGeneratedBy(def) == null ? MemberExistsResult.EXISTS_BY_USER : MemberExistsResult.EXISTS_BY_LOMBOK; } } diff --git a/test/transform/resource/after-delombok/BuilderWithTolerate.java b/test/transform/resource/after-delombok/BuilderWithTolerate.java new file mode 100644 index 00000000..6dc7e248 --- /dev/null +++ b/test/transform/resource/after-delombok/BuilderWithTolerate.java @@ -0,0 +1,59 @@ + +import lombok.experimental.Tolerate; + +public class BuilderWithTolerate { + private final int value; + + public static void main(String[] args) { + BuilderWithTolerate.builder().value("42").build(); + } + + + public static class BuilderWithTolerateBuilder { + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + private int value; + + @Tolerate + public BuilderWithTolerateBuilder value(String s) { + return this.value(Integer.parseInt(s)); + } + + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + BuilderWithTolerateBuilder() { + } + + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithTolerateBuilder value(final int value) { + this.value = value; + return this; + } + + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public BuilderWithTolerate build() { + return new BuilderWithTolerate(value); + } + + @java.lang.Override + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public java.lang.String toString() { + return "BuilderWithTolerate.BuilderWithTolerateBuilder(value=" + this.value + ")"; + } + } + + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + BuilderWithTolerate(final int value) { + this.value = value; + } + + @java.lang.SuppressWarnings("all") + @javax.annotation.Generated("lombok") + public static BuilderWithTolerateBuilder builder() { + return new BuilderWithTolerateBuilder(); + } +} \ No newline at end of file diff --git a/test/transform/resource/after-ecj/BuilderWithTolerate.java b/test/transform/resource/after-ecj/BuilderWithTolerate.java new file mode 100644 index 00000000..0b9b46a0 --- /dev/null +++ b/test/transform/resource/after-ecj/BuilderWithTolerate.java @@ -0,0 +1,34 @@ +import lombok.Builder; +import lombok.experimental.Tolerate; +public @Builder class BuilderWithTolerate { + public static class BuilderWithTolerateBuilder { + private @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") int value; + public @Tolerate BuilderWithTolerateBuilder value(String s) { + return this.value(Integer.parseInt(s)); + } + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder() { + super(); + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder value(final int value) { + this.value = value; + return this; + } + public @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerate build() { + return new BuilderWithTolerate(value); + } + public @java.lang.Override @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") java.lang.String toString() { + return (("BuilderWithTolerate.BuilderWithTolerateBuilder(value=" + this.value) + ")"); + } + } + private final int value; + public static void main(String[] args) { + BuilderWithTolerate.builder().value("42").build(); + } + @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerate(final int value) { + super(); + this.value = value; + } + public static @java.lang.SuppressWarnings("all") @javax.annotation.Generated("lombok") BuilderWithTolerateBuilder builder() { + return new BuilderWithTolerateBuilder(); + } +} \ No newline at end of file diff --git a/test/transform/resource/before/BuilderWithTolerate.java b/test/transform/resource/before/BuilderWithTolerate.java new file mode 100644 index 00000000..48fefce5 --- /dev/null +++ b/test/transform/resource/before/BuilderWithTolerate.java @@ -0,0 +1,18 @@ +import lombok.Builder; +import lombok.experimental.Tolerate; + +@Builder +public class BuilderWithTolerate { + private final int value; + + public static void main(String[] args) { + BuilderWithTolerate.builder().value("42").build(); + } + + public static class BuilderWithTolerateBuilder { + @Tolerate + public BuilderWithTolerateBuilder value(String s) { + return this.value(Integer.parseInt(s)); + } + } +} \ No newline at end of file -- cgit