From 8e7fcac2e39f7eb8809130ae73b4d17965a5d9f2 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 18 Apr 2021 06:33:36 +0200 Subject: [records] [@NonNull] slight refactor to ensure any generated canonical constructor is visible to downstream APs We used to remove the canonical constructor on a record that javac generates, and replace it with our own, as we put null checks in it. However, the type mirrors and such Annotation Processors use already have the implicit canonical constructor available. Instead of trying to remove those, let's just take the existing implicit constructor and add to that (and mark it explicit, of course). --- src/core/lombok/javac/handlers/HandleNonNull.java | 34 ++++++++++++++++------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/core') diff --git a/src/core/lombok/javac/handlers/HandleNonNull.java b/src/core/lombok/javac/handlers/HandleNonNull.java index 786a7659..fe66432a 100644 --- a/src/core/lombok/javac/handlers/HandleNonNull.java +++ b/src/core/lombok/javac/handlers/HandleNonNull.java @@ -71,7 +71,7 @@ import lombok.spi.Provides; @Provides @HandlerPriority(value = 512) // 2^9; onParameter=@__(@NonNull) has to run first. public class HandleNonNull extends JavacAnnotationHandler { - private JCMethodDecl createRecordArgslessConstructor(JavacNode typeNode, JavacNode source) { + private JCMethodDecl createRecordArgslessConstructor(JavacNode typeNode, JavacNode source, JCMethodDecl existingCtr) { JavacTreeMaker maker = typeNode.getTreeMaker(); java.util.List fields = new ArrayList(); @@ -94,8 +94,18 @@ public class HandleNonNull extends JavacAnnotationHandler { JCModifiers mods = maker.Modifiers(toJavacModifier(AccessLevel.PUBLIC) | COMPACT_RECORD_CONSTRUCTOR, List.nil()); JCBlock body = maker.Block(0L, List.nil()); - JCMethodDecl constr = maker.MethodDef(mods, typeNode.toName(""), null, List.nil(), params.toList(), List.nil(), body, null); - return recursiveSetGeneratedBy(constr, source); + if (existingCtr == null) { + JCMethodDecl constr = maker.MethodDef(mods, typeNode.toName(""), null, List.nil(), params.toList(), List.nil(), body, null); + return recursiveSetGeneratedBy(constr, source); + } else { + existingCtr.mods = mods; + existingCtr.params = params.toList(); + existingCtr.body = body; + existingCtr = recursiveSetGeneratedBy(existingCtr, source); + addSuppressWarningsAll(existingCtr.mods, typeNode, typeNode.getNodeFor(getGeneratedBy(existingCtr)), typeNode.getContext()); + addGenerated(existingCtr.mods, typeNode, typeNode.getNodeFor(getGeneratedBy(existingCtr)), typeNode.getContext()); + return existingCtr; + } } /** @@ -113,16 +123,17 @@ public class HandleNonNull extends JavacAnnotationHandler { JCClassDecl cDecl = (JCClassDecl) typeNode.get(); if ((cDecl.mods.flags & RECORD) == 0) return answer; - ListBuffer newDefs = new ListBuffer(); boolean generateConstructor = false; + JCMethodDecl existingCtr = null; + for (JCTree def : cDecl.defs) { - boolean remove = false; if (def instanceof JCMethodDecl) { JCMethodDecl md = (JCMethodDecl) def; if (md.name.contentEquals("")) { if ((md.mods.flags & Flags.GENERATEDCONSTR) != 0) { - remove = true; + existingCtr = md; + existingCtr.mods.flags = existingCtr.mods.flags & ~Flags.GENERATEDCONSTR; generateConstructor = true; } else { if (!isTolerate(typeNode, md)) { @@ -134,13 +145,16 @@ public class HandleNonNull extends JavacAnnotationHandler { } } } - if (!remove) newDefs.append(def); } if (generateConstructor) { - cDecl.defs = newDefs.toList(); - JCMethodDecl ctr = createRecordArgslessConstructor(typeNode, source); - injectMethod(typeNode, ctr); + JCMethodDecl ctr; + if (existingCtr != null) { + ctr = createRecordArgslessConstructor(typeNode, source, existingCtr); + } else { + ctr = createRecordArgslessConstructor(typeNode, source, null); + injectMethod(typeNode, ctr); + } answer = answer.prepend(ctr); } -- cgit