Skip to content

Commit

Permalink
[jdk21][fixes projectlombok#3393] Support changed JCImport.qualid type
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawi01 committed Apr 8, 2023
1 parent b80ff39 commit d9e6e4d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/core/lombok/javac/JavacImportList.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public JavacImportList(JCCompilationUnit cud) {
@Override public String getFullyQualifiedNameForSimpleNameNoAliasing(String unqualified) {
for (JCTree def : defs) {
if (!(def instanceof JCImport)) continue;
JCTree qual = ((JCImport) def).qualid;
JCTree qual = Javac.getQualid((JCImport) def);
if (!(qual instanceof JCFieldAccess)) continue;
String simpleName = ((JCFieldAccess) qual).name.toString();
if (simpleName.equals(unqualified)) {
Expand All @@ -68,7 +68,7 @@ public JavacImportList(JCCompilationUnit cud) {
for (JCTree def : defs) {
if (!(def instanceof JCImport)) continue;
if (((JCImport) def).staticImport) continue;
JCTree qual = ((JCImport) def).qualid;
JCTree qual = Javac.getQualid((JCImport) def);
if (!(qual instanceof JCFieldAccess)) continue;
String simpleName = ((JCFieldAccess) qual).name.toString();
if (!"*".equals(simpleName)) continue;
Expand All @@ -87,7 +87,7 @@ public JavacImportList(JCCompilationUnit cud) {
for (JCTree def : defs) {
if (!(def instanceof JCImport)) continue;
if (((JCImport) def).staticImport) continue;
JCTree qual = ((JCImport) def).qualid;
JCTree qual = Javac.getQualid((JCImport) def);
if (!(qual instanceof JCFieldAccess)) continue;
String simpleName = ((JCFieldAccess) qual).name.toString();
if (!"*".equals(simpleName)) continue;
Expand Down
2 changes: 1 addition & 1 deletion src/core/lombok/javac/handlers/JavacHandlerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ public static void deleteImportFromCompilationUnit(JavacNode node, String name)
if (!(def instanceof JCImport)) continue;
JCImport imp0rt = (JCImport) def;
if (imp0rt.staticImport) continue;
if (!imp0rt.qualid.toString().equals(name)) continue;
if (!Javac.getQualid(imp0rt).toString().equals(name)) continue;
JavacAugments.JCImport_deletable.set(imp0rt, true);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/delombok/lombok/delombok/PrettyPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import com.sun.tools.javac.util.Position;

import lombok.javac.CommentInfo;
import lombok.javac.Javac;
import lombok.javac.PackageName;
import lombok.permit.Permit;
import lombok.javac.CommentInfo.EndConnection;
Expand Down Expand Up @@ -485,8 +486,9 @@ private Name name_value(Name someName) {
}

@Override public void visitImport(JCImport tree) {
if (tree.qualid instanceof JCFieldAccess) {
JCFieldAccess fa = ((JCFieldAccess) tree.qualid);
JCTree qualid = Javac.getQualid(tree);
if (qualid instanceof JCFieldAccess) {
JCFieldAccess fa = ((JCFieldAccess) qualid);
if (fa.name.length() == 1 && fa.name.contentEquals("*")) {
if (fa.selected instanceof JCFieldAccess) {
JCFieldAccess lombokExperimental = (JCFieldAccess) fa.selected;
Expand All @@ -500,7 +502,7 @@ private Name name_value(Name someName) {

aPrint("import ");
if (tree.staticImport) print("static ");
print(tree.qualid);
print(qualid);
println(";", tree);
}

Expand Down
14 changes: 14 additions & 0 deletions src/utils/lombok/javac/Javac.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.tree.JCTree.JCLiteral;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
Expand Down Expand Up @@ -275,6 +276,19 @@ public static JCTree getExtendsClause(JCClassDecl decl) {
}
}

/**
* In some versions, the field's type is {@code JCTree}, in others it is {@code JCFieldAccess}, which at the JVM level are not the same.
*/
private static final Field JCIMPORT_QUALID = Permit.permissiveGetField(JCImport.class, "qualid");

public static JCTree getQualid(JCImport tree) {
try {
return (JCTree) JCIMPORT_QUALID.get(tree);
} catch (IllegalAccessException e) {
throw sneakyThrow(e.getCause());
}
}

public static Object getDocComments(JCCompilationUnit cu) {
try {
return JCCOMPILATIONUNIT_DOCCOMMENTS.get(cu);
Expand Down

0 comments on commit d9e6e4d

Please sign in to comment.