[Closes #855] [jdk8] annotations are now legit on types and type parameters in JDK8, but we’d print errors in delombok when encountering these.

This commit is contained in:
Reinier Zwitserloot committed 2015-08-18 23:12:17 +02:00
1 parent 05760d67ef
commit 1dcbc161c4
3 files changed
+89 -1

No files matched your search

@@ -1518,6 +1518,11 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
public void visitTypeParameter(JCTypeParameter tree) {
try {
List<JCExpression> annotations = readExpressionListOrNil(tree, "annotations");
if (!annotations.isEmpty()) {
printExprs(annotations);
print(" ");
}
print(tree.name);
if (tree.bounds.nonEmpty()) {
print(" extends ");
@@ -1614,6 +1619,8 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
} else if ("JCMemberReference".equals(simpleName)) {
visitReference0(tree);
return;
} else if ("JCAnnotatedType".equals(simpleName)) {
visitAnnotatedType0(tree);
} else {
print("(UNKNOWN[" + tree.getClass().getSimpleName() + "]: " + tree + ")");
println();
@@ -1662,13 +1669,31 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
printExprs(typeArgs);
print(">");
}
;
print(readObject(tree, "mode").toString().equals("INVOKE") ? readObject(tree, "name") : "new");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void visitAnnotatedType0(JCTree tree) {
try {
JCTree underlyingType = readTree(tree, "underlyingType");
if (underlyingType instanceof JCFieldAccess) {
printExpr(((JCFieldAccess) underlyingType).selected);
print(".");
printExprs(readExpressionList(tree, "annotations"));
print(" ");
print(((JCFieldAccess) underlyingType).name);
} else {
printExprs(readExpressionList(tree, "annotations"));
print(" ");
printExpr(underlyingType);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private JCTree readTree(JCTree tree, String fieldName) {
try {
return (JCTree) readObject0(tree, fieldName);
@@ -1695,6 +1720,15 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
}
}
@SuppressWarnings("unchecked")
private List<JCExpression> readExpressionListOrNil(JCTree tree, String fieldName) throws IOException {
try {
return (List<JCExpression>) readObject0(tree, fieldName, List.nil());
} catch (Exception e) {
return List.nil();
}
}
private Object readObject(JCTree tree, String fieldName) {
try {
return readObject0(tree, fieldName);
@@ -1712,4 +1746,13 @@ public class PrettyCommentsPrinter extends JCTree.Visitor {
throw e;
}
}
@SuppressWarnings("unchecked")
private Object readObject0(JCTree tree, String fieldName, Object defaultValue) throws Exception {
try {
return tree.getClass().getDeclaredField(fieldName).get(tree);
} catch (Exception e) {
return defaultValue;
}
}
}
@@ -0,0 +1,22 @@
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;
public class TypeAnnotations {
@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
@interface Foo {
}
@Target(ElementType.TYPE_USE)
@interface Bar {
}
public List<@Foo String> test(@Foo String param) {
@Bar
String local = "bar";
@Foo
java.io.@Foo File[] array = {};
return new java.util.@Foo ArrayList<java.lang.@Foo String>();
}
public <@Foo T extends java.lang.@Foo Number> T test2(@Bar String... varargs) {
return null;
}
}
@@ -0,0 +1,23 @@
// version 8:
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;
public class TypeAnnotations {
@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
@interface Foo {}
@Target(ElementType.TYPE_USE)
@interface Bar {}
public List<@Foo String> test(@Foo String param) {
@Bar String local = "bar";
@Foo java.io.@Foo File[] array = {};
return new java.util.@Foo ArrayList<java.lang.@Foo String>();
}
public <@Foo T extends java.lang.@Foo Number> T test2(@Bar String... varargs) {
return null;
}
}