From fb5a5530148614e8d0c423077d9043e2d58f453b Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Mon, 4 Jun 2018 22:32:44 +0200 Subject: Do not generate a private no-args constructor if: - The class has a parent class - There is an explicit XxxArgsConstructor annotation that would generate a no-args constructor Fixes #1703, fixes #1704, fixes #1712 --- .../eclipse/handlers/EclipseHandlerUtil.java | 8 +++++ .../lombok/eclipse/handlers/HandleConstructor.java | 9 +++++ .../eclipse/handlers/HandleEqualsAndHashCode.java | 7 +--- .../lombok/javac/handlers/HandleConstructor.java | 41 +++++++++++++--------- .../javac/handlers/HandleEqualsAndHashCode.java | 37 +++++++++---------- .../lombok/javac/handlers/JavacHandlerUtil.java | 8 +++++ 6 files changed, 67 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java index 340e233c..2dce285c 100644 --- a/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java +++ b/src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java @@ -1854,4 +1854,12 @@ public class EclipseHandlerUtil { private static long[] copy(long[] array) { return array == null ? null : array.clone(); } + + public static boolean isDirectDescendantOfObject(EclipseNode typeNode) { + if (!(typeNode.get() instanceof TypeDeclaration)) throw new IllegalArgumentException("not a type node"); + TypeDeclaration typeDecl = (TypeDeclaration) typeNode.get(); + if (typeDecl.superclass == null) return true; + String p = typeDecl.superclass.toString(); + return p.equals("Object") || p.equals("java.lang.Object"); + } } diff --git a/src/core/lombok/eclipse/handlers/HandleConstructor.java b/src/core/lombok/eclipse/handlers/HandleConstructor.java index 295c89ca..f9029319 100644 --- a/src/core/lombok/eclipse/handlers/HandleConstructor.java +++ b/src/core/lombok/eclipse/handlers/HandleConstructor.java @@ -211,6 +211,8 @@ public class HandleConstructor { } public void generateExtraNoArgsConstructor(EclipseNode typeNode, EclipseNode sourceNode) { + if (!isDirectDescendantOfObject(typeNode)) return; + Boolean v = typeNode.getAst().readConfiguration(ConfigurationKeys.NO_ARGS_CONSTRUCTOR_EXTRA_PRIVATE); if (v != null && !v) return; @@ -298,6 +300,13 @@ public class HandleConstructor { } } } + + for (EclipseNode child : node.down()) { + if (annotationTypeMatches(NoArgsConstructor.class, child)) return true; + if (annotationTypeMatches(RequiredArgsConstructor.class, child) && findRequiredFields(node).isEmpty()) return true; + if (annotationTypeMatches(AllArgsConstructor.class, child) && findAllFields(node).isEmpty()) return true; + } + return false; } diff --git a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java index e30df698..c99b9b5f 100644 --- a/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java @@ -157,12 +157,7 @@ public class HandleEqualsAndHashCode extends EclipseAnnotationHandler { @@ -197,6 +197,8 @@ public class HandleConstructor { } public void generateExtraNoArgsConstructor(JavacNode typeNode, JavacNode source) { + if (!isDirectDescendantOfObject(typeNode)) return; + Boolean v = typeNode.getAst().readConfiguration(ConfigurationKeys.NO_ARGS_CONSTRUCTOR_EXTRA_PRIVATE); if (v != null && !v) return; @@ -278,6 +280,13 @@ public class HandleConstructor { } } } + + for (JavacNode child : node.down()) { + if (annotationTypeMatches(NoArgsConstructor.class, child)) return true; + if (annotationTypeMatches(RequiredArgsConstructor.class, child) && findRequiredFields(node).isEmpty()) return true; + if (annotationTypeMatches(AllArgsConstructor.class, child) && findAllFields(node).isEmpty()) return true; + } + return false; } diff --git a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java index 12071cfd..aa0fe633 100644 --- a/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java +++ b/src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java @@ -21,7 +21,7 @@ */ package lombok.javac.handlers; -import static lombok.core.handlers.HandlerUtil.*; +import static lombok.core.handlers.HandlerUtil.handleFlagUsage; import static lombok.javac.Javac.*; import static lombok.javac.handlers.JavacHandlerUtil.*; @@ -29,20 +29,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import lombok.ConfigurationKeys; -import lombok.EqualsAndHashCode; -import lombok.core.AST.Kind; -import lombok.core.configuration.CallSuperType; -import lombok.core.AnnotationValues; -import lombok.core.handlers.HandlerUtil; -import lombok.core.handlers.InclusionExclusionUtils; -import lombok.core.handlers.InclusionExclusionUtils.Included; -import lombok.javac.Javac; -import lombok.javac.JavacAnnotationHandler; -import lombok.javac.JavacNode; -import lombok.javac.JavacTreeMaker; -import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult; - import org.mangosdk.spi.ProviderFor; import com.sun.tools.javac.code.BoundKind; @@ -67,6 +53,20 @@ import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; +import lombok.ConfigurationKeys; +import lombok.EqualsAndHashCode; +import lombok.core.AST.Kind; +import lombok.core.AnnotationValues; +import lombok.core.configuration.CallSuperType; +import lombok.core.handlers.HandlerUtil; +import lombok.core.handlers.HandlerUtil.FieldAccess; +import lombok.core.handlers.InclusionExclusionUtils; +import lombok.core.handlers.InclusionExclusionUtils.Included; +import lombok.javac.JavacAnnotationHandler; +import lombok.javac.JavacNode; +import lombok.javac.JavacTreeMaker; +import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult; + /** * Handles the {@code lombok.EqualsAndHashCode} annotation for javac. */ @@ -122,7 +122,6 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler