[JDK26] javac 26 updated how var declarations are stored. Fixed the problem and added a test to confirm.

This commit is contained in:
Reinier Zwitserloot committed 2026-04-08 01:35:50 +02:00
1 parent 87a75b103f
commit bc86bfde63
5 files changed
+60 -5

No files matched your search

+15 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2010-2021 The Project Lombok Authors.
* Copyright (C) 2010-2026 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -123,6 +123,7 @@ public class HandleVal extends JavacASTAdapter {
if (localNode.getSourceVersion() >= 10) {
local.vartype = null;
localNode.getAst().setChanged();
VarDeclDeclKind.setDeclKindToVar(local);
return;
}
@@ -211,4 +212,17 @@ public class HandleVal extends JavacASTAdapter {
return null;
}
}
private static class VarDeclDeclKind {
private static final Field declKind = Permit.permissiveGetField(JCVariableDecl.class, "declKind");
private static final Field typePos = Permit.permissiveGetField(JCVariableDecl.class, "typePos");
private static final Enum<?> DECL_KIND_VAR = Permit.enumValue(JCVariableDecl.class.getName() + "$DeclKind", "VAR");
private static void setDeclKindToVar(JCVariableDecl decl) {
if (declKind != null) {
Permit.permissiveSet(declKind, decl, DECL_KIND_VAR);
Permit.permissiveSet(typePos, decl, decl.pos);
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2025 The Project Lombok Authors.
* Copyright (C) 2016-2026 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -714,7 +714,9 @@ public class PrettyPrinter extends JCTree.Visitor {
if (tree.vartype == null || tree.vartype.pos == -1 || endPos(tree.vartype) == -1) {
print("var");
} else {
print(tree.vartype);
Object kind = readObject(tree, "declKind", null);
if (kind instanceof Enum<?> && "VAR".equals(((Enum<?>) kind).name())) print("var");
else print(tree.vartype);
}
} finally {
innermostArrayBracketsAreVarargs = false;
+19 -2
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018-2025 The Project Lombok Authors.
* Copyright (C) 2018-2026 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -36,6 +36,7 @@ import org.eclipse.jdt.internal.compiler.ast.TypeReference;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
// sunapi suppresses javac's warning about using Unsafe; 'all' suppresses eclipse's warning about the unspecified 'sunapi' key. Leave them both.
@@ -334,6 +335,15 @@ public class Permit {
}
}
public static boolean permissiveSet(Field f, Object receiver, Object newValue) {
try {
set(f, receiver, newValue);
return true;
} catch (Exception ignore) {
return false;
}
}
public static void reportReflectionProblem(Throwable initError, String msg) {
if (!isDebugReflection()) return;
System.err.println("** LOMBOK REFLECTION issue: " + msg);
@@ -348,9 +358,16 @@ public class Permit {
return Permit.<RuntimeException>sneakyThrow0(t);
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> T sneakyThrow0(Throwable t) throws T {
throw (T)t;
}
public static Enum<?> enumValue(String enumTypeName, String enumValueName) {
try {
Class c = Class.forName(enumTypeName);
return Enum.valueOf(c, enumValueName);
} catch (Exception e) {
return null;
}
}
}
@@ -0,0 +1,10 @@
// version 14:
public class ValUndenotable {
public void method(int arg) {
final var x = new Object() {
void foo() {
}
};
x.foo();
}
}
@@ -0,0 +1,12 @@
// version 14:
import lombok.val;
public class ValUndenotable {
public void method(int arg) {
val x = new Object() {
void foo() {
}
};
x.foo();
}
}