Skip to content

Commit fb5a553

Browse files
committed
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
1 parent 370705e commit fb5a553

10 files changed

+563
-43
lines changed

doc/changelog.markdown

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Lombok Changelog
33

44
### v1.16.23 "Edgy Guinea Pig"
55
* v1.16.22 is the latest stable release of Project Lombok.
6+
* BUGFIX: Do not generate a private no-args constructor if that breaks the code. [Issue #1703](https://github.com/rzwitserloot/lombok/issues/1703), [Issue #1704](https://github.com/rzwitserloot/lombok/issues/1704), [Issue #1712](https://github.com/rzwitserloot/lombok/issues/1712)
67
* BUGFIX: Using boolean parameters in lombok annotations would fail. [Issue #1709](https://github.com/rzwitserloot/lombok/issues/1709)
78
* BUGFIX: Delombok would give an error message. [Issue #1705](https://github.com/rzwitserloot/lombok/issues/1705)
89
* FEATURE: Google's [Flogger (a.k.a. FluentLogger)](https://google.github.io/flogger/) is now available via `@Flogger`. [Issue #1697](https://github.com/rzwitserloot/lombok/issues/1697)

src/core/lombok/eclipse/handlers/EclipseHandlerUtil.java

+8
Original file line numberDiff line numberDiff line change
@@ -1854,4 +1854,12 @@ public static NameReference createNameReference(String name, Annotation source)
18541854
private static long[] copy(long[] array) {
18551855
return array == null ? null : array.clone();
18561856
}
1857+
1858+
public static boolean isDirectDescendantOfObject(EclipseNode typeNode) {
1859+
if (!(typeNode.get() instanceof TypeDeclaration)) throw new IllegalArgumentException("not a type node");
1860+
TypeDeclaration typeDecl = (TypeDeclaration) typeNode.get();
1861+
if (typeDecl.superclass == null) return true;
1862+
String p = typeDecl.superclass.toString();
1863+
return p.equals("Object") || p.equals("java.lang.Object");
1864+
}
18571865
}

src/core/lombok/eclipse/handlers/HandleConstructor.java

+9
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ public enum SkipIfConstructorExists {
211211
}
212212

213213
public void generateExtraNoArgsConstructor(EclipseNode typeNode, EclipseNode sourceNode) {
214+
if (!isDirectDescendantOfObject(typeNode)) return;
215+
214216
Boolean v = typeNode.getAst().readConfiguration(ConfigurationKeys.NO_ARGS_CONSTRUCTOR_EXTRA_PRIVATE);
215217
if (v != null && !v) return;
216218

@@ -298,6 +300,13 @@ private static boolean noArgsConstructorExists(EclipseNode node) {
298300
}
299301
}
300302
}
303+
304+
for (EclipseNode child : node.down()) {
305+
if (annotationTypeMatches(NoArgsConstructor.class, child)) return true;
306+
if (annotationTypeMatches(RequiredArgsConstructor.class, child) && findRequiredFields(node).isEmpty()) return true;
307+
if (annotationTypeMatches(AllArgsConstructor.class, child) && findAllFields(node).isEmpty()) return true;
308+
}
309+
301310
return false;
302311
}
303312

src/core/lombok/eclipse/handlers/HandleEqualsAndHashCode.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,7 @@ public void generateMethods(EclipseNode typeNode, EclipseNode errorNode, List<In
157157
}
158158
}
159159

160-
boolean isDirectDescendantOfObject = true;
161-
162-
if (typeDecl.superclass != null) {
163-
String p = typeDecl.superclass.toString();
164-
isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object");
165-
}
160+
boolean isDirectDescendantOfObject = isDirectDescendantOfObject(typeNode);
166161

167162
if (isDirectDescendantOfObject && callSuper) {
168163
errorNode.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless.");

src/core/lombok/javac/handlers/HandleConstructor.java

+25-16
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,8 @@
2222
package lombok.javac.handlers;
2323

2424
import static lombok.core.handlers.HandlerUtil.*;
25-
import static lombok.javac.handlers.JavacHandlerUtil.*;
2625
import static lombok.javac.Javac.*;
27-
28-
import lombok.AccessLevel;
29-
import lombok.AllArgsConstructor;
30-
import lombok.Builder;
31-
import lombok.ConfigurationKeys;
32-
import lombok.NoArgsConstructor;
33-
import lombok.RequiredArgsConstructor;
34-
import lombok.core.AnnotationValues;
35-
import lombok.core.AST.Kind;
36-
import lombok.delombok.LombokOptionsFactory;
37-
import lombok.javac.Javac;
38-
import lombok.javac.JavacAnnotationHandler;
39-
import lombok.javac.JavacNode;
40-
import lombok.javac.JavacTreeMaker;
41-
import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
26+
import static lombok.javac.handlers.JavacHandlerUtil.*;
4227

4328
import org.mangosdk.spi.ProviderFor;
4429

@@ -62,6 +47,21 @@
6247
import com.sun.tools.javac.util.ListBuffer;
6348
import com.sun.tools.javac.util.Name;
6449

50+
import lombok.AccessLevel;
51+
import lombok.AllArgsConstructor;
52+
import lombok.Builder;
53+
import lombok.ConfigurationKeys;
54+
import lombok.NoArgsConstructor;
55+
import lombok.RequiredArgsConstructor;
56+
import lombok.core.AST.Kind;
57+
import lombok.core.AnnotationValues;
58+
import lombok.delombok.LombokOptionsFactory;
59+
import lombok.javac.Javac;
60+
import lombok.javac.JavacAnnotationHandler;
61+
import lombok.javac.JavacNode;
62+
import lombok.javac.JavacTreeMaker;
63+
import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
64+
6565
public class HandleConstructor {
6666
@ProviderFor(JavacAnnotationHandler.class)
6767
public static class HandleNoArgsConstructor extends JavacAnnotationHandler<NoArgsConstructor> {
@@ -197,6 +197,8 @@ public enum SkipIfConstructorExists {
197197
}
198198

199199
public void generateExtraNoArgsConstructor(JavacNode typeNode, JavacNode source) {
200+
if (!isDirectDescendantOfObject(typeNode)) return;
201+
200202
Boolean v = typeNode.getAst().readConfiguration(ConfigurationKeys.NO_ARGS_CONSTRUCTOR_EXTRA_PRIVATE);
201203
if (v != null && !v) return;
202204

@@ -278,6 +280,13 @@ private static boolean noArgsConstructorExists(JavacNode node) {
278280
}
279281
}
280282
}
283+
284+
for (JavacNode child : node.down()) {
285+
if (annotationTypeMatches(NoArgsConstructor.class, child)) return true;
286+
if (annotationTypeMatches(RequiredArgsConstructor.class, child) && findRequiredFields(node).isEmpty()) return true;
287+
if (annotationTypeMatches(AllArgsConstructor.class, child) && findAllFields(node).isEmpty()) return true;
288+
}
289+
281290
return false;
282291
}
283292

src/core/lombok/javac/handlers/HandleEqualsAndHashCode.java

+16-21
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,14 @@
2121
*/
2222
package lombok.javac.handlers;
2323

24-
import static lombok.core.handlers.HandlerUtil.*;
24+
import static lombok.core.handlers.HandlerUtil.handleFlagUsage;
2525
import static lombok.javac.Javac.*;
2626
import static lombok.javac.handlers.JavacHandlerUtil.*;
2727

2828
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.Collections;
3131

32-
import lombok.ConfigurationKeys;
33-
import lombok.EqualsAndHashCode;
34-
import lombok.core.AST.Kind;
35-
import lombok.core.configuration.CallSuperType;
36-
import lombok.core.AnnotationValues;
37-
import lombok.core.handlers.HandlerUtil;
38-
import lombok.core.handlers.InclusionExclusionUtils;
39-
import lombok.core.handlers.InclusionExclusionUtils.Included;
40-
import lombok.javac.Javac;
41-
import lombok.javac.JavacAnnotationHandler;
42-
import lombok.javac.JavacNode;
43-
import lombok.javac.JavacTreeMaker;
44-
import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
45-
4632
import org.mangosdk.spi.ProviderFor;
4733

4834
import com.sun.tools.javac.code.BoundKind;
@@ -67,6 +53,20 @@
6753
import com.sun.tools.javac.util.ListBuffer;
6854
import com.sun.tools.javac.util.Name;
6955

56+
import lombok.ConfigurationKeys;
57+
import lombok.EqualsAndHashCode;
58+
import lombok.core.AST.Kind;
59+
import lombok.core.AnnotationValues;
60+
import lombok.core.configuration.CallSuperType;
61+
import lombok.core.handlers.HandlerUtil;
62+
import lombok.core.handlers.HandlerUtil.FieldAccess;
63+
import lombok.core.handlers.InclusionExclusionUtils;
64+
import lombok.core.handlers.InclusionExclusionUtils.Included;
65+
import lombok.javac.JavacAnnotationHandler;
66+
import lombok.javac.JavacNode;
67+
import lombok.javac.JavacTreeMaker;
68+
import lombok.javac.handlers.JavacHandlerUtil.MemberExistsResult;
69+
7070
/**
7171
* Handles the {@code lombok.EqualsAndHashCode} annotation for javac.
7272
*/
@@ -122,7 +122,6 @@ public void generateMethods(JavacNode typeNode, JavacNode source, java.util.List
122122
return;
123123
}
124124

125-
boolean isDirectDescendantOfObject = true;
126125
boolean implicitCallSuper = callSuper == null;
127126
if (callSuper == null) {
128127
try {
@@ -132,11 +131,7 @@ public void generateMethods(JavacNode typeNode, JavacNode source, java.util.List
132131
}
133132
}
134133

135-
JCTree extending = Javac.getExtendsClause((JCClassDecl)typeNode.get());
136-
if (extending != null) {
137-
String p = extending.toString();
138-
isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object");
139-
}
134+
boolean isDirectDescendantOfObject = isDirectDescendantOfObject(typeNode);
140135

141136
if (isDirectDescendantOfObject && callSuper) {
142137
source.addError("Generating equals/hashCode with a supercall to java.lang.Object is pointless.");

src/core/lombok/javac/handlers/JavacHandlerUtil.java

+8
Original file line numberDiff line numberDiff line change
@@ -1813,4 +1813,12 @@ private static void copyJavadoc_jdk6_7(JavacNode from, JCTree to, CopyJavadoc co
18131813
docComments.put(from.get(), filtered[1]);
18141814
}
18151815
}
1816+
1817+
public static boolean isDirectDescendantOfObject(JavacNode typeNode) {
1818+
if (!(typeNode.get() instanceof JCClassDecl)) throw new IllegalArgumentException("not a type node");
1819+
JCTree extending = Javac.getExtendsClause((JCClassDecl)typeNode.get());
1820+
if (extending == null) return true;
1821+
String p = extending.toString();
1822+
return p.equals("Object") || p.equals("java.lang.Object");
1823+
}
18161824
}

0 commit comments

Comments
 (0)