Whoops, Classload errors running lombok on javac with backing JVM 7 or below; fixed.

This commit is contained in:
Reinier Zwitserloot committed 2013-10-10 23:24:04 +02:00
1 parent 728dddbc81
commit 08961edcfe
3 files changed
+69 -38

No files matched your search

@@ -1323,51 +1323,61 @@ public class JavacHandlerUtil {
*
* in 'SETTER' mode, stripping works similarly to 'GETTER' mode, except {@code param} are copied and stripped from the original and {@code @return} are skipped.
*/
@SuppressWarnings("unchecked")
public static void copyJavadoc(JavacNode from, JCTree to, CopyJavadoc copyMode) {
if (copyMode == null) copyMode = CopyJavadoc.VERBATIM;
try {
JCCompilationUnit cu = ((JCCompilationUnit) from.top().get());
Object dc = Javac.getDocComments(cu);
if (dc instanceof Map) {
Map<JCTree, String> docComments = (Map<JCTree, String>) dc;
String javadoc = docComments.get(from.get());
if (javadoc != null) {
String[] filtered = copyMode.split(javadoc);
docComments.put(to, filtered[0]);
docComments.put(from.get(), filtered[1]);
}
} else if (dc instanceof DocCommentTable) {
DocCommentTable dct = (DocCommentTable) dc;
Comment javadoc = dct.getComment(from.get());
if (javadoc != null) {
String[] filtered = copyMode.split(javadoc.getText());
dct.putComment(to, createJavadocComment(filtered[0], from));
dct.putComment(from.get(), createJavadocComment(filtered[1], from));
}
copyJavadoc_jdk6_7(from, to, copyMode, dc);
} else if (Javac.instanceOfDocCommentTable(dc)) {
CopyJavadoc_8.copyJavadoc(from, to, copyMode, dc);
}
} catch (Exception ignore) {}
}
private static Comment createJavadocComment(final String text, final JavacNode field) {
return new Comment() {
@Override public String getText() {
return text;
}
private static class CopyJavadoc_8 {
static void copyJavadoc(JavacNode from, JCTree to, CopyJavadoc copyMode, Object dc) {
DocCommentTable dct = (DocCommentTable) dc;
Comment javadoc = dct.getComment(from.get());
@Override public int getSourcePos(int index) {
return -1;
if (javadoc != null) {
String[] filtered = copyMode.split(javadoc.getText());
dct.putComment(to, createJavadocComment(filtered[0], from));
dct.putComment(from.get(), createJavadocComment(filtered[1], from));
}
@Override public CommentStyle getStyle() {
return CommentStyle.JAVADOC;
}
@Override public boolean isDeprecated() {
return text.contains("@deprecated") && field.getKind() == Kind.FIELD && isFieldDeprecated(field);
}
};
}
private static Comment createJavadocComment(final String text, final JavacNode field) {
return new Comment() {
@Override public String getText() {
return text;
}
@Override public int getSourcePos(int index) {
return -1;
}
@Override public CommentStyle getStyle() {
return CommentStyle.JAVADOC;
}
@Override public boolean isDeprecated() {
return text.contains("@deprecated") && field.getKind() == Kind.FIELD && isFieldDeprecated(field);
}
};
}
}
@SuppressWarnings({"unchecked", "all"})
private static void copyJavadoc_jdk6_7(JavacNode from, JCTree to, CopyJavadoc copyMode, Object dc) {
Map<JCTree, String> docComments = (Map<JCTree, String>) dc;
String javadoc = docComments.get(from.get());
if (javadoc != null) {
String[] filtered = copyMode.split(javadoc);
docComments.put(to, filtered[0]);
docComments.put(from.get(), filtered[1]);
}
}
}
@@ -66,7 +66,17 @@ public class DocCommentIntegrator {
if (map_ instanceof Map) {
((Map<JCTree, String>) map_).put(node, docCommentContent);
return true;
} else if (map_ instanceof DocCommentTable) {
} else if (Javac.instanceOfDocCommentTable(map_)) {
CommentAttacher_8.attach(node, docCommentContent, map_);
return true;
}
return false;
}
/* Container for code which will cause class loader exceptions on javac below 8. By being in a separate class, we avoid the problem. */
private static class CommentAttacher_8 {
static void attach(final JCTree node, String docCommentContent, Object map_) {
final String docCommentContent_ = docCommentContent;
((DocCommentTable) map_).putComment(node, new Comment() {
@Override public String getText() {
@@ -85,10 +95,7 @@ public class DocCommentIntegrator {
return JavacHandlerUtil.nodeHasDeprecatedFlag(node);
}
});
return true;
}
return false;
}
private JCTree findJavadocableNodeOnOrAfter(JCCompilationUnit unit, int endPos) {
+14
View File
@@ -103,6 +103,20 @@ public class Javac {
return 6;
}
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;
}
public static boolean instanceOfDocCommentTable(Object o) {
return DOCCOMMENTTABLE_CLASS != null && DOCCOMMENTTABLE_CLASS.isInstance(o);
}
/**
* Checks if the given expression (that really ought to refer to a type
* expression) represents a primitive type.