Skip to content

Commit ba2b47b

Browse files
authored
Merge pull request #2975 from Rawi01/organize-imports-val
Move 'val' -> 'final var' code to patch method
2 parents ba68962 + 9ef7656 commit ba2b47b

File tree

4 files changed

+13
-26
lines changed

4 files changed

+13
-26
lines changed

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -2052,11 +2052,7 @@ static Annotation[] addAnnotation(ASTNode source, Annotation[] originalAnnotatio
20522052
}
20532053

20542054
int pS = source.sourceStart, pE = source.sourceEnd;
2055-
long p = (long)pS << 32 | pE;
2056-
long[] poss = new long[annotationTypeFqn.length];
2057-
Arrays.fill(poss, p);
2058-
QualifiedTypeReference qualifiedType = new QualifiedTypeReference(annotationTypeFqn, poss);
2059-
setGeneratedBy(qualifiedType, source);
2055+
TypeReference qualifiedType = generateQualifiedTypeRef(source, annotationTypeFqn);
20602056
Annotation ann;
20612057
if (args != null && args.length == 1 && args[0] instanceof Expression) {
20622058
SingleMemberAnnotation sma = new SingleMemberAnnotation(qualifiedType, pS);

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

+2-17
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
package lombok.eclipse.handlers;
2323

2424
import static lombok.core.handlers.HandlerUtil.handleFlagUsage;
25-
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
25+
import static lombok.eclipse.handlers.EclipseHandlerUtil.typeMatches;
2626

2727
import lombok.ConfigurationKeys;
2828
import lombok.val;
2929
import lombok.var;
3030
import lombok.core.HandlerPriority;
3131
import lombok.eclipse.DeferUntilPostDiet;
32-
import lombok.eclipse.Eclipse;
3332
import lombok.eclipse.EclipseASTAdapter;
3433
import lombok.eclipse.EclipseASTVisitor;
3534
import lombok.eclipse.EclipseNode;
@@ -41,13 +40,10 @@
4140
import org.eclipse.jdt.internal.compiler.ast.ForeachStatement;
4241
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
4342
import org.eclipse.jdt.internal.compiler.ast.NullLiteral;
44-
import org.eclipse.jdt.internal.compiler.ast.SingleTypeReference;
4543
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
46-
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
4744

4845
/*
49-
* Java 1-9: This class just handles 3 basic error cases. The real meat of eclipse 'val' support is in {@code PatchVal} and {@code PatchValEclipse}.
50-
* Java 10+: Lombok uses the native 'var' support and transforms 'val' to 'final var'.
46+
* This class just handles 3 basic error cases. The real meat of eclipse 'val' support is in {@code PatchVal} and {@code PatchValEclipse}
5147
*/
5248
@Provides(EclipseASTVisitor.class)
5349
@DeferUntilPostDiet
@@ -101,16 +97,5 @@ public class HandleVal extends EclipseASTAdapter {
10197
localNode.addError("variable initializer is 'null'");
10298
return;
10399
}
104-
105-
// For Java >= 10 we use native support
106-
if (localNode.getSourceVersion() >= 10) {
107-
if (isVal) {
108-
TypeReference originalType = local.type;
109-
local.type = new SingleTypeReference("var".toCharArray(), Eclipse.pos(local.type));
110-
local.modifiers |= ClassFileConstants.AccFinal;
111-
local.annotations = addAnnotation(local.type, local.annotations, originalType.getTypeName());
112-
}
113-
return;
114-
}
115100
}
116101
}

src/eclipseAgent/lombok/eclipse/agent/PatchVal.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
import java.lang.reflect.Field;
6060

6161
import static lombok.Lombok.sneakyThrow;
62-
import static lombok.eclipse.Eclipse.poss;
63-
import static lombok.eclipse.handlers.EclipseHandlerUtil.makeType;
62+
import static lombok.eclipse.Eclipse.*;
63+
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;
6464
import static org.eclipse.jdt.core.compiler.CategorizedProblem.CAT_TYPE;
6565

6666
public class PatchVal {
@@ -204,8 +204,6 @@ public static boolean handleValForLocalDeclaration(LocalDeclaration local, Block
204204
boolean var = isVar(local, scope);
205205
if (!(val || var)) return false;
206206

207-
if (hasNativeVarSupport(scope)) return false;
208-
209207
if (val) {
210208
StackTraceElement[] st = new Throwable().getStackTrace();
211209
for (int i = 0; i < st.length - 2 && i < 10; i++) {
@@ -239,6 +237,13 @@ public static boolean handleValForLocalDeclaration(LocalDeclaration local, Block
239237

240238
TypeReference replacement = null;
241239

240+
// Java 10+: Lombok uses the native 'var' support and transforms 'val' to 'final var'.
241+
if (hasNativeVarSupport(scope) && val) {
242+
replacement = new SingleTypeReference("var".toCharArray(), pos(local.type));
243+
local.initialization = init;
244+
init = null;
245+
}
246+
242247
if (init != null) {
243248
if (init.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) {
244249
return false;

test/transform/resource/before/ValAnonymousSubclassSelfReference.java

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// version :9
12
// issue 2420: to trigger the problem 2 var/val, at least one normal variable and a anonymous self reference is required
23
import java.util.Map;
34
import java.util.HashMap;

0 commit comments

Comments
 (0)