mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 02:16:18 +00:00
A source file with just @Getter in it now compiles in javac8, but there is still a looooong way to go.
This commit is contained in:
1 parent
45697b5081
commit
1b4d8669c4
7 files changed
+109
-42
No files matched your search
@@ -360,7 +360,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
|
||||
void removeDeferredErrors(JavacNode node) {
|
||||
DiagnosticPosition pos = node.get().pos();
|
||||
JCCompilationUnit top = (JCCompilationUnit) top().get();
|
||||
removeFromDeferredDiagnostics(pos.getStartPosition(), pos.getEndPosition(top.endPositions));
|
||||
removeFromDeferredDiagnostics(pos.getStartPosition(), Javac.getEndPosition(pos, top));
|
||||
}
|
||||
|
||||
/** Supply either a position or a node (in that case, position of the node is used) */
|
||||
|
||||
@@ -139,7 +139,7 @@ public class HandleEqualsAndHashCode extends JavacAnnotationHandler<EqualsAndHas
|
||||
}
|
||||
}
|
||||
|
||||
JCTree extending = ((JCClassDecl)typeNode.get()).getExtendsClause();
|
||||
JCTree extending = Javac.getExtendsClause((JCClassDecl)typeNode.get());
|
||||
if (extending != null) {
|
||||
String p = extending.toString();
|
||||
isDirectDescendantOfObject = p.equals("Object") || p.equals("java.lang.Object");
|
||||
|
||||
@@ -29,6 +29,7 @@ import java.util.Collections;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.core.AnnotationValues;
|
||||
import lombok.javac.Javac;
|
||||
import lombok.javac.JavacAnnotationHandler;
|
||||
import lombok.javac.JavacNode;
|
||||
|
||||
@@ -121,7 +122,7 @@ public class HandleSneakyThrows extends JavacAnnotationHandler<SneakyThrows> {
|
||||
|
||||
JCVariableDecl catchParam = maker.VarDef(maker.Modifiers(Flags.FINAL), node.toName("$ex"), varType, null);
|
||||
JCExpression lombokLombokSneakyThrowNameRef = chainDots(node, "lombok", "Lombok", "sneakyThrow");
|
||||
JCBlock catchBody = maker.Block(0, List.<JCStatement>of(maker.Throw(maker.Apply(
|
||||
JCBlock catchBody = maker.Block(0, List.<JCStatement>of(Javac.makeThrow(maker, maker.Apply(
|
||||
List.<JCExpression>nil(), lombokLombokSneakyThrowNameRef,
|
||||
List.<JCExpression>of(maker.Ident(node.toName("$ex")))))));
|
||||
|
||||
|
||||
@@ -946,8 +946,8 @@ public class JavacHandlerUtil {
|
||||
if (isPrimitive(varDecl.vartype)) return null;
|
||||
Name fieldName = varDecl.name;
|
||||
JCExpression npe = chainDots(variable, "java", "lang", "NullPointerException");
|
||||
JCTree exception = maker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(maker.Literal(fieldName.toString())), null);
|
||||
JCStatement throwStatement = maker.Throw(exception);
|
||||
JCExpression exception = maker.NewClass(null, List.<JCExpression>nil(), npe, List.<JCExpression>of(maker.Literal(fieldName.toString())), null);
|
||||
JCStatement throwStatement = Javac.makeThrow(maker, exception);
|
||||
JCBlock throwBlock = maker.Block(0, List.of(throwStatement));
|
||||
return maker.If(Javac.makeBinary(maker, CTC_EQUAL, maker.Ident(fieldName), Javac.makeLiteral(maker, CTC_BOT, null)), throwBlock, null);
|
||||
}
|
||||
|
||||
@@ -742,9 +742,9 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
|
||||
else
|
||||
print("class " + tree.name);
|
||||
printTypeParameters(tree.typarams);
|
||||
if (tree.getExtendsClause() != null) {
|
||||
if (Javac.getExtendsClause(tree) != null) {
|
||||
print(" extends ");
|
||||
printExpr(tree.getExtendsClause());
|
||||
printExpr(Javac.getExtendsClause(tree));
|
||||
}
|
||||
if (tree.implementing.nonEmpty()) {
|
||||
print(" implements ");
|
||||
|
||||
@@ -25,8 +25,6 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import lombok.Lombok;
|
||||
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
import com.sun.tools.javac.util.Context;
|
||||
@@ -73,9 +71,9 @@ public class CommentCatcher {
|
||||
}
|
||||
scannerFactory.getMethod("preRegister", Context.class).invoke(null, context);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw Javac.sneakyThrow(e.getCause());
|
||||
} catch (Exception e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw Javac.sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,9 +87,9 @@ public class CommentCatcher {
|
||||
}
|
||||
parserFactory.getMethod("setInCompiler", JavaCompiler.class, Context.class, Map.class).invoke(null, compiler, context, commentsMap);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw Javac.sneakyThrow(e.getCause());
|
||||
} catch (Exception e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw Javac.sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,22 +35,23 @@ import javax.lang.model.type.NoType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeVisitor;
|
||||
|
||||
import lombok.Lombok;
|
||||
|
||||
import com.sun.tools.javac.code.Type;
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCBinary;
|
||||
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
|
||||
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
|
||||
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.JCLiteral;
|
||||
import com.sun.tools.javac.tree.JCTree.JCModifiers;
|
||||
import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;
|
||||
import com.sun.tools.javac.tree.JCTree.JCStatement;
|
||||
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
|
||||
import com.sun.tools.javac.tree.JCTree.JCUnary;
|
||||
import com.sun.tools.javac.tree.TreeMaker;
|
||||
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
|
||||
import com.sun.tools.javac.util.List;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
|
||||
@@ -62,6 +63,9 @@ public class Javac {
|
||||
// prevent instantiation
|
||||
}
|
||||
|
||||
private static final ConcurrentMap<String, Object> TYPE_TAG_CACHE = new ConcurrentHashMap<String, Object>();
|
||||
private static final ConcurrentMap<String, Object> TREE_TAG_CACHE = new ConcurrentHashMap<String, Object>();
|
||||
|
||||
/** Matches any of the 8 primitive names, such as {@code boolean}. */
|
||||
private static final Pattern PRIMITIVE_TYPE_NAME_PATTERN = Pattern.compile("^(boolean|byte|short|int|long|float|double|char)$");
|
||||
|
||||
@@ -148,10 +152,6 @@ public class Javac {
|
||||
return ctc1 == null ? ctc2 == null : ctc1.equals(ctc2);
|
||||
}
|
||||
|
||||
private static final ConcurrentMap<String, Object> TYPE_TAG_CACHE = new ConcurrentHashMap<String, Object>();
|
||||
private static final ConcurrentMap<String, Object> TREE_TAG_CACHE = new ConcurrentHashMap<String, Object>();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves the provided TypeTag value, in a compiler version independent manner.
|
||||
*
|
||||
@@ -168,7 +168,7 @@ public class Javac {
|
||||
* @return the value of the typetag constant (either enum instance or an Integer object).
|
||||
*/
|
||||
public static Object getTypeTag(String identifier) {
|
||||
return getFieldCached(TYPE_TAG_CACHE, getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.code.TypeTag" : "com.sun.tools.javac.code.TypeTags", identifier);
|
||||
return getFieldCached(TYPE_TAG_CACHE, getJavaCompilerVersion() < 8 ? "com.sun.tools.javac.code.TypeTags" : "com.sun.tools.javac.code.TypeTag", identifier);
|
||||
}
|
||||
|
||||
public static Object getTreeTag(String identifier) {
|
||||
@@ -181,11 +181,11 @@ public class Javac {
|
||||
try {
|
||||
value = Class.forName(className).getField(fieldName).get(null);
|
||||
} catch (NoSuchFieldException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
|
||||
cache.putIfAbsent(fieldName, value);
|
||||
@@ -200,8 +200,8 @@ public class Javac {
|
||||
return tree.typetag;
|
||||
}
|
||||
|
||||
private static final Method createIdent, createLiteral, createUnary, createBinary;
|
||||
|
||||
private static final Method createIdent, createLiteral, createUnary, createBinary, createThrow, getExtendsClause, getEndPosition;
|
||||
|
||||
static {
|
||||
if (getJavaCompilerVersion() < 8) {
|
||||
createIdent = getMethod(TreeMaker.class, "TypeIdent", int.class);
|
||||
@@ -220,23 +220,40 @@ public class Javac {
|
||||
if (getJavaCompilerVersion() < 8) {
|
||||
createUnary = getMethod(TreeMaker.class, "Unary", int.class, JCExpression.class);
|
||||
} else {
|
||||
createUnary = getMethod(TreeMaker.class, "Unary", "com.sun.tools.javac.code.TypeTag", JCExpression.class.getName());
|
||||
createUnary = getMethod(TreeMaker.class, "Unary", "com.sun.tools.javac.tree.JCTree$Tag", JCExpression.class.getName());
|
||||
}
|
||||
createUnary.setAccessible(true);
|
||||
|
||||
if (getJavaCompilerVersion() < 8) {
|
||||
createBinary = getMethod(TreeMaker.class, "Binary", Integer.TYPE, JCExpression.class, JCExpression.class);
|
||||
} else {
|
||||
createBinary = getMethod(TreeMaker.class, "Binary", "com.sun.tools.javac.code.TypeTag", JCExpression.class.getName(), JCExpression.class.getName());
|
||||
createBinary = getMethod(TreeMaker.class, "Binary", "com.sun.tools.javac.tree.JCTree$Tag", JCExpression.class.getName(), JCExpression.class.getName());
|
||||
}
|
||||
createBinary.setAccessible(true);
|
||||
|
||||
if (getJavaCompilerVersion() < 8) {
|
||||
createThrow = getMethod(TreeMaker.class, "Throw", JCTree.class);
|
||||
} else {
|
||||
createThrow = getMethod(TreeMaker.class, "Throw", JCExpression.class);
|
||||
}
|
||||
createBinary.setAccessible(true);
|
||||
|
||||
getExtendsClause = getMethod(JCClassDecl.class, "getExtendsClause", new Class<?>[0]);
|
||||
getExtendsClause.setAccessible(true);
|
||||
|
||||
if (getJavaCompilerVersion() < 8) {
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", java.util.Map.class);
|
||||
} else {
|
||||
getEndPosition = getMethod(DiagnosticPosition.class, "getEndPosition", "com.sun.tools.javac.tree.EndPosTable");
|
||||
}
|
||||
getEndPosition.setAccessible(true);
|
||||
}
|
||||
|
||||
private static Method getMethod(Class<?> clazz, String name, Class<?>... paramTypes) {
|
||||
try {
|
||||
return clazz.getMethod(name, paramTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,9 +263,9 @@ public class Javac {
|
||||
for (int i = 0; i < paramTypes.length; i++) c[i] = Class.forName(paramTypes[i]);
|
||||
return clazz.getMethod(name, c);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -256,9 +273,9 @@ public class Javac {
|
||||
try {
|
||||
return (JCExpression) createIdent.invoke(maker, ctc);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,9 +283,9 @@ public class Javac {
|
||||
try {
|
||||
return (JCLiteral) createLiteral.invoke(maker, ctc, argument);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -276,9 +293,9 @@ public class Javac {
|
||||
try {
|
||||
return (JCUnary) createUnary.invoke(maker, ctc, argument);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,9 +303,48 @@ public class Javac {
|
||||
try {
|
||||
return (JCBinary) createBinary.invoke(maker, ctc, lhsArgument, rhsArgument);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw Lombok.sneakyThrow(e.getCause());
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
public static JCStatement makeThrow(TreeMaker maker, JCExpression expression) {
|
||||
try {
|
||||
return (JCStatement) createThrow.invoke(maker, expression);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
public static JCTree getExtendsClause(JCClassDecl decl) {
|
||||
try {
|
||||
return (JCTree) getExtendsClause.invoke(decl);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
public static Object getDocComments(JCCompilationUnit cu) {
|
||||
try {
|
||||
return JCCOMPILATIONUNIT_DOCCOMMENTS.get(cu);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int getEndPosition(DiagnosticPosition pos, JCCompilationUnit top) {
|
||||
try {
|
||||
Object endPositions = JCCOMPILATIONUNIT_ENDPOSITIONS.get(top);
|
||||
return (Integer) getEndPosition.invoke(pos, endPositions);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw sneakyThrow(e);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw sneakyThrow(e.getCause());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,9 +374,9 @@ public class Javac {
|
||||
return (Type) JC_NO_TYPE.newInstance();
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
} catch (InstantiationException e) {
|
||||
throw Lombok.sneakyThrow(e);
|
||||
throw sneakyThrow(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,7 +399,7 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
private static final Field JCTREE_TAG, JCLITERAL_TYPETAG, JCPRIMITIVETYPETREE_TYPETAG;
|
||||
private static final Field JCTREE_TAG, JCLITERAL_TYPETAG, JCPRIMITIVETYPETREE_TYPETAG, JCCOMPILATIONUNIT_ENDPOSITIONS, JCCOMPILATIONUNIT_DOCCOMMENTS;
|
||||
private static final Method JCTREE_GETTAG;
|
||||
static {
|
||||
Field f = null;
|
||||
@@ -364,6 +420,18 @@ public class Javac {
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCPRIMITIVETYPETREE_TYPETAG = f;
|
||||
|
||||
f = null;
|
||||
try {
|
||||
f = JCCompilationUnit.class.getDeclaredField("endPositions");
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCCOMPILATIONUNIT_ENDPOSITIONS = f;
|
||||
|
||||
f = null;
|
||||
try {
|
||||
f = JCCompilationUnit.class.getDeclaredField("docComments");
|
||||
} catch (NoSuchFieldException e) {}
|
||||
JCCOMPILATIONUNIT_DOCCOMMENTS = f;
|
||||
|
||||
Method m = null;
|
||||
try {
|
||||
m = JCTree.class.getDeclaredMethod("getTag");
|
||||
@@ -424,7 +492,7 @@ public class Javac {
|
||||
}
|
||||
}
|
||||
|
||||
private static RuntimeException sneakyThrow(Throwable t) {
|
||||
static RuntimeException sneakyThrow(Throwable t) {
|
||||
if (t == null) throw new NullPointerException("t");
|
||||
Javac.<RuntimeException>sneakyThrow0(t);
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user