Skip to content

Commit

Permalink
Merge pull request #3482 from Rawi01/getter-lazy
Browse files Browse the repository at this point in the history
Keep initializer position for lazy getter
  • Loading branch information
rzwitserloot authored Sep 17, 2023
2 parents f16c131 + 3fb99fa commit 60ed4ad
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/core/lombok/javac/JavacAugments.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ private JavacAugments() {
public static final FieldAugment<JCTree, Boolean> JCTree_handled = FieldAugment.augment(JCTree.class, boolean.class, "lombok$handled");
public static final FieldAugment<JCTree, JCTree> JCTree_generatedNode = FieldAugment.circularSafeAugment(JCTree.class, JCTree.class, "lombok$generatedNode");
public static final FieldAugment<JCImport, Boolean> JCImport_deletable = FieldAugment.circularSafeAugment(JCImport.class, Boolean.class, "lombok$deletable");
public static final FieldAugment<JCTree, Boolean> JCTree_keepPosition = FieldAugment.augment(JCTree.class, boolean.class, "lombok$keepPosition");
}
19 changes: 2 additions & 17 deletions src/core/lombok/javac/handlers/HandleGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -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;
Expand Down Expand Up @@ -229,18 +229,9 @@ public JCMethodDecl createGetter(long access, JavacNode field, JavacTreeMaker tr
boolean makeFinal = shouldMakeFinal(field, accessors);

List<JCStatement> statements;
JCTree toClearOfMarkers = null;
int[] methodArgPos = null;
boolean addSuppressWarningsUnchecked = false;
if (lazy && !inNetbeansEditor(field)) {
toClearOfMarkers = fieldNode.init;
if (toClearOfMarkers instanceof JCMethodInvocation) {
List<JCExpression> 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 {
Expand Down Expand Up @@ -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<JCExpression> suppressions = new ListBuffer<JCExpression>();
Expand Down
11 changes: 7 additions & 4 deletions src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -173,10 +175,11 @@ public static <T extends JCTree> 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;
}

Expand Down
17 changes: 17 additions & 0 deletions test/transform/resource/after-delombok/GetterLazyArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static String stringRunnable(String arg1, Runnable arg2) {
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field3 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field4 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field5 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field6 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();

@java.lang.SuppressWarnings({"all", "unchecked"})
public String getField1() {
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class GetterLazyErrorPosition {
private final java.util.concurrent.atomic.AtomicReference<java.lang.Object> field = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();

@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);
}
}
18 changes: 18 additions & 0 deletions test/transform/resource/after-ecj/GetterLazyArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class GetterLazyArguments {
private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference<java.lang.Object> field3 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference<java.lang.Object> field4 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference<java.lang.Object> field5 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
private final @lombok.Getter(lazy = true) java.util.concurrent.atomic.AtomicReference<java.lang.Object> field6 = new java.util.concurrent.atomic.AtomicReference<java.lang.Object>();
GetterLazyArguments() {
super();
}
Expand Down Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions test/transform/resource/before/GetterLazyArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) : "";
}
8 changes: 8 additions & 0 deletions test/transform/resource/before/GetterLazyErrorPosition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//platform javac: positions in eclipse are hard...
class GetterLazyErrorPosition {
@lombok.Getter(lazy=true)
private final String field =
true ?
"" :
new ErrorPosition();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7 cannot find symbol
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
11 cannot find symbol

0 comments on commit 60ed4ad

Please sign in to comment.