mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-13 00:14:50 +00:00
[fixes #3448] Keep initializer position for lazy getter
This commit is contained in:
1 parent
000ce6d19a
commit
ba47865bf0
10 files changed
+77
-21
No files matched your search
@@ -34,4 +34,5 @@ public final class 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");
|
||||
}
|
||||
@@ -23,6 +23,7 @@ package lombok.javac.handlers;
|
||||
|
||||
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.javac.JavacTreeMaker.TypeTag;
|
||||
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 class HandleGetter extends JavacAnnotationHandler<Getter> {
|
||||
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 {
|
||||
@@ -268,12 +259,6 @@ public class HandleGetter extends JavacAnnotationHandler<Getter> {
|
||||
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>();
|
||||
|
||||
@@ -25,6 +25,7 @@ import static com.sun.tools.javac.code.Flags.GENERATEDCONSTR;
|
||||
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 @@ public class JavacHandlerUtil {
|
||||
|
||||
@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 class JavacHandlerUtil {
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ class GetterLazyArguments {
|
||||
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() {
|
||||
@@ -99,4 +100,20 @@ class GetterLazyArguments {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -103,4 +104,21 @@ class GetterLazyArguments {
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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) : "";
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//platform javac: positions in eclipse are hard...
|
||||
class GetterLazyErrorPosition {
|
||||
@lombok.Getter(lazy=true)
|
||||
private final String field =
|
||||
true ?
|
||||
"" :
|
||||
new ErrorPosition();
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
6 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition
|
||||
@@ -0,0 +1 @@
|
||||
11 cannot find symbol symbol: class ErrorPosition location: class GetterLazyErrorPosition
|
||||
Reference in new issue
Block a user