Added a ClassDef wrapper, because its signature changed between javac1.6 and javac1.7. (The wrapper uses reflection). Need for: javac @Builder impl.

Also added some utilities to JavacHandlerUtil.
This commit is contained in:
Reinier Zwitserloot committed 2013-06-18 03:33:13 +02:00
1 parent 359b7845f2
commit e047d2b94e
2 files changed
+53 -4

No files matched your search

@@ -730,11 +730,11 @@ public class JavacHandlerUtil {
*
* Also takes care of updating the JavacAST.
*/
public static void injectField(JavacNode typeNode, JCVariableDecl field) {
injectField(typeNode, field, false);
public static JavacNode injectField(JavacNode typeNode, JCVariableDecl field) {
return injectField(typeNode, field, false);
}
private static void injectField(JavacNode typeNode, JCVariableDecl field, boolean addSuppressWarnings) {
private static JavacNode injectField(JavacNode typeNode, JCVariableDecl field, boolean addSuppressWarnings) {
JCClassDecl type = (JCClassDecl) typeNode.get();
if (addSuppressWarnings) addSuppressWarningsAll(field.mods, typeNode, field.pos, getGeneratedBy(field));
@@ -760,7 +760,7 @@ public class JavacHandlerUtil {
insertAfter.tail = fieldEntry;
}
typeNode.add(field, Kind.FIELD);
return typeNode.add(field, Kind.FIELD);
}
private static boolean isEnumConstant(final JCVariableDecl field) {
@@ -1026,6 +1026,13 @@ public class JavacHandlerUtil {
return result.toList();
}
public static List<JCTypeParameter> copyTypeParams(TreeMaker maker, List<JCTypeParameter> params) {
if (params == null || params.isEmpty()) return params;
ListBuffer<JCTypeParameter> out = ListBuffer.lb();
for (JCTypeParameter tp : params) out.append(maker.TypeParameter(tp.name, tp.bounds));
return out.toList();
}
public static JCExpression namePlusTypeParamsToTypeReference(TreeMaker maker, Name typeName, List<JCTypeParameter> params) {
ListBuffer<JCExpression> typeArgs = ListBuffer.lb();
+42
View File
@@ -22,17 +22,25 @@
package lombok.javac;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sun.tools.javac.code.TypeTags;
import com.sun.tools.javac.main.JavaCompiler;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
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.JCTypeParameter;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
/**
* Container for static utility methods relevant to lombok's operation on javac.
@@ -161,4 +169,38 @@ public class Javac {
}
}
private static Method method;
public static JCClassDecl ClassDef(TreeMaker maker, JCModifiers mods, Name name, List<JCTypeParameter> typarams, JCExpression extending, List<JCExpression> implementing, List<JCTree> defs) {
if (method == null) try {
method = TreeMaker.class.getDeclaredMethod("ClassDef", JCModifiers.class, Name.class, List.class, JCExpression.class, List.class, List.class);
} catch (NoSuchMethodException ignore) {}
if (method == null) try {
method = TreeMaker.class.getDeclaredMethod("ClassDef", JCModifiers.class, Name.class, List.class, JCTree.class, List.class, List.class);
} catch (NoSuchMethodException ignore) {}
if (method == null) throw new IllegalStateException("Lombok bug #20130617-1310: ClassDef doesn't look like anything we thought it would look like.");
if (!Modifier.isPublic(method.getModifiers()) && !method.isAccessible()) {
method.setAccessible(true);
}
try {
return (JCClassDecl) method.invoke(maker, mods, name, typarams, extending, implementing, defs);
} catch (InvocationTargetException e) {
throw sneakyThrow(e.getCause());
} catch (IllegalAccessException e) {
throw sneakyThrow(e.getCause());
}
}
private static RuntimeException sneakyThrow(Throwable t) {
if (t == null) throw new NullPointerException("t");
Javac.<RuntimeException>sneakyThrow0(t);
return null;
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneakyThrow0(Throwable t) throws T {
throw (T)t;
}
}