From ba47865bf01d938a15c6146d757dceeaf2f4da4e Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sun, 13 Aug 2023 16:00:15 +0200 Subject: [PATCH 1/2] [fixes #3448] Keep initializer position for lazy getter --- src/core/lombok/javac/JavacAugments.java | 1 + .../lombok/javac/handlers/HandleGetter.java | 19 ++----------------- .../javac/handlers/JavacHandlerUtil.java | 11 +++++++---- .../after-delombok/GetterLazyArguments.java | 17 +++++++++++++++++ .../GetterLazyErrorPosition.java | 19 +++++++++++++++++++ .../after-ecj/GetterLazyArguments.java | 18 ++++++++++++++++++ .../resource/before/GetterLazyArguments.java | 3 +++ .../before/GetterLazyErrorPosition.java | 8 ++++++++ .../GetterLazyErrorPosition.java.messages | 1 + .../GetterLazyErrorPosition.java.messages | 1 + 10 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 test/transform/resource/after-delombok/GetterLazyErrorPosition.java create mode 100644 test/transform/resource/before/GetterLazyErrorPosition.java create mode 100644 test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages create mode 100644 test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages diff --git a/src/core/lombok/javac/JavacAugments.java b/src/core/lombok/javac/JavacAugments.java index e5d1fc433f..a41d550790 100644 --- a/src/core/lombok/javac/JavacAugments.java +++ b/src/core/lombok/javac/JavacAugments.java @@ -34,4 +34,5 @@ private JavacAugments() { public static final FieldAugment JCTree_handled = FieldAugment.augment(JCTree.class, boolean.class, "lombok$handled"); public static final FieldAugment JCTree_generatedNode = FieldAugment.circularSafeAugment(JCTree.class, JCTree.class, "lombok$generatedNode"); public static final FieldAugment JCImport_deletable = FieldAugment.circularSafeAugment(JCImport.class, Boolean.class, "lombok$deletable"); + public static final FieldAugment JCTree_keepPosition = FieldAugment.augment(JCTree.class, boolean.class, "lombok$keepPosition"); } diff --git a/src/core/lombok/javac/handlers/HandleGetter.java b/src/core/lombok/javac/handlers/HandleGetter.java index 3f7fbd2016..a8a98d60d0 100644 --- a/src/core/lombok/javac/handlers/HandleGetter.java +++ b/src/core/lombok/javac/handlers/HandleGetter.java @@ -23,6 +23,7 @@ import static lombok.core.handlers.HandlerUtil.*; import static lombok.javac.Javac.*; +import static lombok.javac.JavacAugments.JCTree_keepPosition; import static lombok.javac.JavacTreeMaker.TypeTag.*; import static lombok.javac.handlers.JavacHandlerUtil.*; @@ -47,7 +48,6 @@ import lombok.spi.Provides; import com.sun.tools.javac.code.Flags; -import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCBinary; import com.sun.tools.javac.tree.JCTree.JCBlock; @@ -229,18 +229,9 @@ public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker tr boolean makeFinal = shouldMakeFinal(field, accessors); List statements; - JCTree toClearOfMarkers = null; - int[] methodArgPos = null; boolean addSuppressWarningsUnchecked = false; if (lazy && !inNetbeansEditor(field)) { - toClearOfMarkers = fieldNode.init; - if (toClearOfMarkers instanceof JCMethodInvocation) { - List args = ((JCMethodInvocation) toClearOfMarkers).args; - methodArgPos = new int[args.length()]; - for (int i = 0; i < methodArgPos.length; i++) { - methodArgPos[i] = args.get(i).pos; - } - } + JCTree_keepPosition.set(fieldNode.init, true); statements = createLazyGetterBody(treeMaker, field, source); addSuppressWarningsUnchecked = LombokOptionsFactory.getDelombokOptions(field.getContext()).getFormatPreferences().generateSuppressWarnings(); } else { @@ -268,12 +259,6 @@ public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker tr JCMethodDecl decl = recursiveSetGeneratedBy(treeMaker.MethodDef(treeMaker.Modifiers(access, annsOnMethod), methodName, methodType, methodGenericParams, parameters, throwsClauses, methodBody, annotationMethodDefaultValue), source); - if (toClearOfMarkers != null) recursiveSetGeneratedBy(toClearOfMarkers, null); - if (methodArgPos != null) { - for (int i = 0; i < methodArgPos.length; i++) { - ((JCMethodInvocation) toClearOfMarkers).args.get(i).pos = methodArgPos[i]; - } - } decl.mods.annotations = decl.mods.annotations.appendList(delegates); if (addSuppressWarningsUnchecked) { ListBuffer suppressions = new ListBuffer(); diff --git a/src/core/lombok/javac/handlers/JavacHandlerUtil.java b/src/core/lombok/javac/handlers/JavacHandlerUtil.java index 6993a6baa5..436b747544 100644 --- a/src/core/lombok/javac/handlers/JavacHandlerUtil.java +++ b/src/core/lombok/javac/handlers/JavacHandlerUtil.java @@ -25,6 +25,7 @@ import static lombok.core.handlers.HandlerUtil.*; import static lombok.javac.Javac.*; import static lombok.javac.JavacAugments.JCTree_generatedNode; +import static lombok.javac.JavacAugments.JCTree_keepPosition; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; @@ -126,6 +127,7 @@ private static class MarkingScanner extends TreeScanner { @Override public void scan(JCTree tree) { if (tree == null) return; + if (JCTree_keepPosition.get(tree)) return; setGeneratedBy(tree, source); super.scan(tree); } @@ -173,10 +175,11 @@ public static T setGeneratedBy(T node, JavacNode sourceNode) } JCTree_generatedNode.set(node, sourceNode.get()); - if (!inNetbeansEditor(sourceNode.getContext()) || isParameter(node)) { - node.pos = sourceNode.getStartPos(); - storeEnd(node, sourceNode.getEndPosition(), (JCCompilationUnit) sourceNode.top().get()); - } + if (JCTree_keepPosition.get(node)) return node; + if (inNetbeansEditor(sourceNode.getContext()) && !isParameter(node)) return node; + + node.pos = sourceNode.getStartPos(); + storeEnd(node, sourceNode.getEndPosition(), (JCCompilationUnit) sourceNode.top().get()); return node; } diff --git a/test/transform/resource/after-delombok/GetterLazyArguments.java b/test/transform/resource/after-delombok/GetterLazyArguments.java index 604513cd88..ef3f341c31 100644 --- a/test/transform/resource/after-delombok/GetterLazyArguments.java +++ b/test/transform/resource/after-delombok/GetterLazyArguments.java @@ -17,6 +17,7 @@ static String stringRunnable(String arg1, Runnable arg2) { private final java.util.concurrent.atomic.AtomicReference field3 = new java.util.concurrent.atomic.AtomicReference(); private final java.util.concurrent.atomic.AtomicReference field4 = new java.util.concurrent.atomic.AtomicReference(); private final java.util.concurrent.atomic.AtomicReference field5 = new java.util.concurrent.atomic.AtomicReference(); + private final java.util.concurrent.atomic.AtomicReference field6 = new java.util.concurrent.atomic.AtomicReference(); @java.lang.SuppressWarnings({"all", "unchecked"}) public String getField1() { @@ -99,4 +100,20 @@ public String getField5() { } return (String) (value == this.field5 ? null : value); } + + @java.lang.SuppressWarnings({"all", "unchecked"}) + public String getField6() { + java.lang.Object value = this.field6.get(); + if (value == null) { + synchronized (this.field6) { + value = this.field6.get(); + if (value == null) { + final String actualValue = true ? stringInt(true ? "a" : "b", true ? 1 : 0) : ""; + value = actualValue == null ? this.field6 : actualValue; + this.field6.set(value); + } + } + } + return (String) (value == this.field6 ? null : value); + } } \ No newline at end of file diff --git a/test/transform/resource/after-delombok/GetterLazyErrorPosition.java b/test/transform/resource/after-delombok/GetterLazyErrorPosition.java new file mode 100644 index 0000000000..76797d4427 --- /dev/null +++ b/test/transform/resource/after-delombok/GetterLazyErrorPosition.java @@ -0,0 +1,19 @@ +class GetterLazyErrorPosition { + private final java.util.concurrent.atomic.AtomicReference field = new java.util.concurrent.atomic.AtomicReference(); + + @java.lang.SuppressWarnings({"all", "unchecked"}) + public String getField() { + java.lang.Object value = this.field.get(); + if (value == null) { + synchronized (this.field) { + value = this.field.get(); + if (value == null) { + final String actualValue = true ? "" : new ErrorPosition(); + value = actualValue == null ? this.field : actualValue; + this.field.set(value); + } + } + } + return (String) (value == this.field ? null : value); + } +} diff --git a/test/transform/resource/after-ecj/GetterLazyArguments.java b/test/transform/resource/after-ecj/GetterLazyArguments.java index 41f1771f09..14086d8192 100644 --- a/test/transform/resource/after-ecj/GetterLazyArguments.java +++ b/test/transform/resource/after-ecj/GetterLazyArguments.java @@ -4,6 +4,7 @@ class GetterLazyArguments { private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference field3 = new java.util.concurrent.atomic.AtomicReference(); private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference field4 = new java.util.concurrent.atomic.AtomicReference(); private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference field5 = new java.util.concurrent.atomic.AtomicReference(); + private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference field6 = new java.util.concurrent.atomic.AtomicReference(); GetterLazyArguments() { super(); } @@ -103,4 +104,21 @@ static String stringRunnable(String arg1, Runnable arg2) { } return (String) ((value == this.field5) ? null : value); } + public @java.lang.SuppressWarnings({"all", "unchecked"}) String getField6() { + java.lang.Object value = this.field6.get(); + if ((value == null)) + { + synchronized (this.field6) + { + value = this.field6.get(); + if ((value == null)) + { + final String actualValue = (true ? stringInt((true ? "a" : "b"), (true ? 1 : 0)) : ""); + value = ((actualValue == null) ? this.field6 : actualValue); + this.field6.set(value); + } + } + } + return (String) ((value == this.field6) ? null : value); + } } \ No newline at end of file diff --git a/test/transform/resource/before/GetterLazyArguments.java b/test/transform/resource/before/GetterLazyArguments.java index 47dfae55f8..e662da1fd8 100644 --- a/test/transform/resource/before/GetterLazyArguments.java +++ b/test/transform/resource/before/GetterLazyArguments.java @@ -18,4 +18,7 @@ class GetterLazyArguments { @lombok.Getter(lazy=true) private final String field5 = stringRunnable(("a"), () -> { }); + + @lombok.Getter(lazy=true) + private final String field6 = true ? stringInt(true ? "a" : "b", true ? 1 : 0) : ""; } diff --git a/test/transform/resource/before/GetterLazyErrorPosition.java b/test/transform/resource/before/GetterLazyErrorPosition.java new file mode 100644 index 0000000000..8cc8649731 --- /dev/null +++ b/test/transform/resource/before/GetterLazyErrorPosition.java @@ -0,0 +1,8 @@ +//platform javac: positions in eclipse are hard... +class GetterLazyErrorPosition { + @lombok.Getter(lazy=true) + private final String field = + true ? + "" : + new ErrorPosition(); +} diff --git a/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages b/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages new file mode 100644 index 0000000000..0a57f3d147 --- /dev/null +++ b/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages @@ -0,0 +1 @@ +6 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition diff --git a/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages b/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages new file mode 100644 index 0000000000..3f0f96971d --- /dev/null +++ b/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages @@ -0,0 +1 @@ +11 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition From 3fb99fa69969bb3eb3529e426665fb96945b26db Mon Sep 17 00:00:00 2001 From: Rawi01 Date: Sun, 13 Aug 2023 16:23:04 +0200 Subject: [PATCH 2/2] Fix javac6 tests --- .../messages-delombok/GetterLazyErrorPosition.java.messages | 2 +- .../messages-idempotent/GetterLazyErrorPosition.java.messages | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages b/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages index 0a57f3d147..5177d3325b 100644 --- a/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages +++ b/test/transform/resource/messages-delombok/GetterLazyErrorPosition.java.messages @@ -1 +1 @@ -6 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition +7 cannot find symbol \ No newline at end of file diff --git a/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages b/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages index 3f0f96971d..25a5759cb3 100644 --- a/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages +++ b/test/transform/resource/messages-idempotent/GetterLazyErrorPosition.java.messages @@ -1 +1 @@ -11 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition +11 cannot find symbol \ No newline at end of file