mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-15 02:17:12 +00:00
[review][refactor] No meaningful changes: Improved comments, javadoc, and cleaned up some code.
This commit is contained in:
6 files changed
+62
-107
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2025 The Project Lombok Authors.
|
||||
* Copyright (C) 2009-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
|
||||
@@ -152,14 +152,14 @@ public class DocCommentIntegrator {
|
||||
return pos;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused") // As of JDK27 this is the only getEndPosition; EndPosTable is gone.
|
||||
@SuppressWarnings("unused") // We compile against very old versions of javac intentionally (to support old stuff), but this is the method for javac 27+
|
||||
public int getEndPosition() {
|
||||
int end = Javac.getTreeEndPos(node);
|
||||
if (end > pos) return end;
|
||||
return pos + docCommentContent_.length();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused") // We compile against very old versions of javac intentionally (to support old stuff), but this is the method for newer impls.
|
||||
@SuppressWarnings("unused") // We compile against very old versions of javac intentionally (to support old stuff), but this is the method for javac 9(?)-26.
|
||||
public int getEndPosition(EndPosTable endPosTable) {
|
||||
int end = endPosTable == null ? 0 : endPosTable.getEndPos(node);
|
||||
if (end > pos) return end;
|
||||
|
||||
@@ -14,24 +14,20 @@ import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.comp.Todo;
|
||||
|
||||
public class JavaCompiler {
|
||||
// Shared by JDK6-9
|
||||
public boolean keepComments;
|
||||
public boolean genEndPos;
|
||||
public Todo todo;
|
||||
/* JDK [6,) */ public boolean keepComments;
|
||||
/* JDK [6,26] */ public boolean genEndPos; // JDK27+: Removed; end pos are stored on nodes themselves
|
||||
/* JDK [6,) */ public Todo todo;
|
||||
|
||||
public JavaCompiler(Context context) {}
|
||||
public int errorCount() { return 0; }
|
||||
public static String version() { return "<stub>"; }
|
||||
public JCCompilationUnit parse(String fileName) throws IOException { return null; }
|
||||
public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {return null;}
|
||||
/* JDK [6,) */ public JavaCompiler(Context context) {}
|
||||
/* JDK [6,) */ public int errorCount() { return 0; }
|
||||
/* JDK [6,) */ public static String version() { return "<stub>"; }
|
||||
/* JDK [6,) */ public JCCompilationUnit parse(String fileName) throws IOException { return null; }
|
||||
/* JDK [6,) */ public List<JCCompilationUnit> enterTrees(List<JCCompilationUnit> roots) {return null;}
|
||||
|
||||
//JDK up to 8
|
||||
public void initProcessAnnotations(Iterable<? extends Processor> processors) throws IOException {}
|
||||
public JavaCompiler processAnnotations(List<JCCompilationUnit> roots, List<String> classnames) {return this;}
|
||||
|
||||
// JDK 9
|
||||
public void initProcessAnnotations(Iterable<? extends Processor> processors, Collection<? extends JavaFileObject> initialFiles, Collection<String> initialClassNames) {}
|
||||
public void processAnnotations(List<JCCompilationUnit> roots, Collection<String> classnames) {}
|
||||
public void close() {}
|
||||
public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) { return null; }
|
||||
/* JDK [6,8] */ public void initProcessAnnotations(Iterable<? extends Processor> processors) throws IOException {}
|
||||
/* JDK [9,) */ public void initProcessAnnotations(Iterable<? extends Processor> processors, Collection<? extends JavaFileObject> initialFiles, Collection<String> initialClassNames) {}
|
||||
/* JDK [6,8] */ public JavaCompiler processAnnotations(List<JCCompilationUnit> roots, List<String> classnames) {return this;}
|
||||
/* JDK [9,) */ public void processAnnotations(List<JCCompilationUnit> roots, Collection<String> classnames) {}
|
||||
/* JDK [9,) */ public void close() {}
|
||||
/* JDK [9,) */ public List<JCCompilationUnit> initModules(List<JCCompilationUnit> roots) { return null; }
|
||||
}
|
||||
@@ -7,9 +7,16 @@ import com.sun.tools.javac.tree.JCTree;
|
||||
|
||||
public class JavacParser {
|
||||
// Up to javac26 the last boolean is keepEndPositions; as of javac27 (JDK-8372948) it is parseModuleInfo.
|
||||
protected JavacParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions) {
|
||||
// Javac -9(?) has:
|
||||
// protected JavacParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions)
|
||||
// Javac 27+ has:
|
||||
// protected JavacParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, boolean parseModuleInfo)
|
||||
// These signatures are identical.
|
||||
|
||||
protected JavacParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, boolean varied) {
|
||||
}
|
||||
|
||||
// Javac 10(?)-26.
|
||||
protected JavacParser(ParserFactory fac, Lexer S, boolean keepDocComments, boolean keepLineMap, boolean keepEndPositions, boolean parseModuleInfo) {
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2011-2019 The Project Lombok Authors.
|
||||
* Copyright (C) 2011-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
|
||||
@@ -106,11 +106,12 @@ public class CommentCatcher {
|
||||
parserFactory = Class.forName("lombok.javac.java7.CommentCollectingParserFactory");
|
||||
} else if (javaCompilerVersion == 8) {
|
||||
parserFactory = Class.forName("lombok.javac.java8.CommentCollectingParserFactory");
|
||||
} else if (Javac.endPosStoredOnTree()) {
|
||||
} else if (!Javac.endPosStoredOnTree()) {
|
||||
parserFactory = Class.forName("lombok.javac.java9.CommentCollectingParserFactory");
|
||||
} else {
|
||||
assert javaCompilerVersion > 26;
|
||||
// JDK-8372948 also reshaped JavacParser's constructors.
|
||||
parserFactory = Class.forName("lombok.javac.java27.CommentCollectingParserFactory");
|
||||
} else {
|
||||
parserFactory = Class.forName("lombok.javac.java9.CommentCollectingParserFactory");
|
||||
}
|
||||
Permit.getMethod(parserFactory, "setInCompiler", JavaCompiler.class, Context.class).invoke(null, compiler, context);
|
||||
} catch (InvocationTargetException e) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2025 The Project Lombok Authors.
|
||||
* Copyright (C) 2009-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
|
||||
@@ -125,15 +125,7 @@ public class Javac {
|
||||
return version;
|
||||
}
|
||||
|
||||
private static final Class<?> DOCCOMMENTTABLE_CLASS;
|
||||
|
||||
static {
|
||||
Class<?> c = null;
|
||||
try {
|
||||
c = Class.forName("com.sun.tools.javac.tree.DocCommentTable");
|
||||
} catch (Throwable ignore) {}
|
||||
DOCCOMMENTTABLE_CLASS = c;
|
||||
}
|
||||
private static final Class<?> DOCCOMMENTTABLE_CLASS = Permit.permissiveGetClass("com.sun.tools.javac.tree.DocCommentTable");
|
||||
|
||||
public static boolean instanceOfDocCommentTable(Object o) {
|
||||
return DOCCOMMENTTABLE_CLASS != null && DOCCOMMENTTABLE_CLASS.isInstance(o);
|
||||
@@ -213,7 +205,10 @@ public class Javac {
|
||||
|
||||
private static final Method getExtendsClause, getEndPosition, storeEnd;
|
||||
|
||||
/** As of javac27 (JDK-8372948), end positions live in {@code JCTree.endpos} and {@code EndPosTable} no longer exists. */
|
||||
/**
|
||||
* Do JCTree objects track their own endpos (true) or are they stored in a separate EndPos table (false)?
|
||||
* As of javac27 (JDK-8372948), end positions live in {@code JCTree.endpos} and {@code EndPosTable} no longer exists.
|
||||
*/
|
||||
private static final boolean ENDPOS_ON_TREE;
|
||||
|
||||
static {
|
||||
@@ -224,25 +219,26 @@ public class Javac {
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", java.util.Map.class);
|
||||
storeEnd = getMethod(java.util.Map.class, "put", Object.class, Object.class);
|
||||
} else {
|
||||
Class<?> endPosTable;
|
||||
Class<?> endPosTableType;
|
||||
try {
|
||||
endPosTable = Class.forName("com.sun.tools.javac.tree.EndPosTable");
|
||||
endPosTableType = Class.forName("com.sun.tools.javac.tree.EndPosTable");
|
||||
} catch (ClassNotFoundException ex) {
|
||||
endPosTable = null;
|
||||
endPosTableType = null;
|
||||
}
|
||||
ENDPOS_ON_TREE = endPosTable == null;
|
||||
if (ENDPOS_ON_TREE) {
|
||||
if (endPosTableType == null) {
|
||||
ENDPOS_ON_TREE = true;
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", new Class<?>[0]);
|
||||
storeEnd = null;
|
||||
} else {
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", endPosTable);
|
||||
ENDPOS_ON_TREE = false;
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", endPosTableType);
|
||||
Method storeEndMethodTemp;
|
||||
try {
|
||||
storeEndMethodTemp = Permit.getMethod(endPosTable, "storeEnd", JCTree.class, int.class);
|
||||
storeEndMethodTemp = Permit.getMethod(endPosTableType, "storeEnd", JCTree.class, int.class);
|
||||
} catch (NoSuchMethodException e) {
|
||||
try {
|
||||
endPosTable = Class.forName("com.sun.tools.javac.parser.JavacParser$AbstractEndPosTable");
|
||||
storeEndMethodTemp = Permit.getMethod(endPosTable, "storeEnd", JCTree.class, int.class);
|
||||
endPosTableType = Class.forName("com.sun.tools.javac.parser.JavacParser$AbstractEndPosTable");
|
||||
storeEndMethodTemp = Permit.getMethod(endPosTableType, "storeEnd", JCTree.class, int.class);
|
||||
} catch (NoSuchMethodException ex) {
|
||||
throw sneakyThrow(ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
@@ -268,18 +264,6 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
private static Method getMethod(Class<?> clazz, String name, String... paramTypes) {
|
||||
try {
|
||||
Class<?>[] c = new Class[paramTypes.length];
|
||||
for (int i = 0; i < paramTypes.length; i++) c[i] = Class.forName(paramTypes[i]);
|
||||
return Permit.getMethod(clazz, name, c);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw sneakyThrow(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In some versions, the field's type is {@code JCTree}, in others it is {@code JCExpression}, which at the JVM level are not the same.
|
||||
*/
|
||||
@@ -447,7 +431,7 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns -1 if this javac has no {@code JCTree.endpos} field (pre-27). */
|
||||
/** Returns -1 if this javac has no {@code JCTree.endpos} field (JDK[6,26]). */
|
||||
public static int getTreeEndPos(JCTree tree) {
|
||||
if (JCTREE_ENDPOS == null) return -1;
|
||||
try {
|
||||
@@ -473,30 +457,9 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
private static final Class<?> JC_VOID_TYPE, JC_NO_TYPE;
|
||||
|
||||
static {
|
||||
Class<?> c = null;
|
||||
try {
|
||||
c = Class.forName("com.sun.tools.javac.code.Type$JCVoidType");
|
||||
} catch (Throwable ignore) {}
|
||||
JC_VOID_TYPE = c;
|
||||
c = null;
|
||||
try {
|
||||
c = Class.forName("com.sun.tools.javac.code.Type$JCNoType");
|
||||
} catch (Throwable ignore) {}
|
||||
JC_NO_TYPE = c;
|
||||
}
|
||||
|
||||
private static final Field symtabVoidType = getFieldIfExists(Symtab.class, "voidType");
|
||||
|
||||
private static Field getFieldIfExists(Class<?> c, String fieldName) {
|
||||
try {
|
||||
return Permit.getField(c, "voidType");
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private static final Class<?> JC_VOID_TYPE = Permit.permissiveGetClass("com.sun.tools.javac.code.Type$JCVoidType");
|
||||
private static final Class<?> JC_NO_TYPE = Permit.permissiveGetClass("com.sun.tools.javac.code.Type$JCNoType");
|
||||
private static final Field symtabVoidType = Permit.permissiveGetField(Symtab.class, "voidType");
|
||||
|
||||
public static Type createVoidType(Symtab symbolTable, TypeTag tag) {
|
||||
if (symtabVoidType != null) try {
|
||||
@@ -542,29 +505,9 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
private static final Field JCTREE_ENDPOS;
|
||||
static {
|
||||
Field f = null;
|
||||
try {
|
||||
f = Permit.getField(JCTree.class, "endpos");
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCTREE_ENDPOS = f;
|
||||
}
|
||||
|
||||
private static final Field JCCOMPILATIONUNIT_ENDPOSITIONS, JCCOMPILATIONUNIT_DOCCOMMENTS;
|
||||
static {
|
||||
Field f = null;
|
||||
try {
|
||||
f = Permit.getField(JCCompilationUnit.class, "endPositions");
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCCOMPILATIONUNIT_ENDPOSITIONS = f;
|
||||
|
||||
f = null;
|
||||
try {
|
||||
f = Permit.getField(JCCompilationUnit.class, "docComments");
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCCOMPILATIONUNIT_DOCCOMMENTS = f;
|
||||
}
|
||||
private static final Field JCTREE_ENDPOS = Permit.permissiveGetField(JCTree.class, "endpos");
|
||||
private static final Field JCCOMPILATIONUNIT_ENDPOSITIONS = Permit.permissiveGetField(JCCompilationUnit.class, "endPositions");
|
||||
private static final Field JCCOMPILATIONUNIT_DOCCOMMENTS = Permit.permissiveGetField(JCCompilationUnit.class, "docComments");
|
||||
|
||||
static RuntimeException sneakyThrow(Throwable t) {
|
||||
if (t == null) throw new NullPointerException("t");
|
||||
@@ -574,6 +517,6 @@ public class Javac {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
|
||||
throw (T)t;
|
||||
throw (T) t;
|
||||
}
|
||||
}
|
||||
@@ -370,4 +370,12 @@ public class Permit {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> permissiveGetClass(String fullyQualifiedTypeName) {
|
||||
try {
|
||||
return Class.forName(fullyQualifiedTypeName);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user