Skip to content

Commit c97d50d

Browse files
committed
8369508: Type annotations on anonymous new class creation expressions can't be retrieved
Reviewed-by: vromero
1 parent 0687f12 commit c97d50d

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Annotate.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ private <T extends Attribute.Compound> void annotateNow(Symbol toAnnotate,
332332

333333
Assert.checkNonNull(c, "Failed to create annotation");
334334

335+
if (env.info.isAnonymousNewClass) {
336+
// Annotations on anonymous class instantiations should be attributed,
337+
// but not attached to the enclosing element. They will be visited
338+
// separately and attached to the synthetic class declaration.
339+
continue;
340+
}
341+
335342
if (a.type.isErroneous() || a.type.tsym.isAnnotationType()) {
336343
if (annotated.containsKey(a.type.tsym)) {
337344
ListBuffer<T> l = annotated.get(a.type.tsym);
@@ -1144,8 +1151,11 @@ public void visitClassDef(JCClassDecl tree) {
11441151
public void visitNewClass(JCNewClass tree) {
11451152
scan(tree.encl);
11461153
scan(tree.typeargs);
1147-
if (tree.def == null) {
1154+
try {
1155+
env.info.isAnonymousNewClass = tree.def != null;
11481156
scan(tree.clazz);
1157+
} finally {
1158+
env.info.isAnonymousNewClass = false;
11491159
}
11501160
scan(tree.args);
11511161
// the anonymous class instantiation if any will be visited separately.

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,16 +2777,9 @@ public void visitNewClass(final JCNewClass tree) {
27772777

27782778
// Attribute clazz expression and store
27792779
// symbol + type back into the attributed tree.
2780-
Type clazztype;
2781-
2782-
try {
2783-
env.info.isAnonymousNewClass = tree.def != null;
2784-
clazztype = TreeInfo.isEnumInit(env.tree) ?
2785-
attribIdentAsEnumType(env, (JCIdent)clazz) :
2786-
attribType(clazz, env);
2787-
} finally {
2788-
env.info.isAnonymousNewClass = false;
2789-
}
2780+
Type clazztype = TreeInfo.isEnumInit(env.tree) ?
2781+
attribIdentAsEnumType(env, (JCIdent)clazz) :
2782+
attribType(clazz, env);
27902783

27912784
clazztype = chk.checkDiamond(tree, clazztype);
27922785
chk.validate(clazz, localEnv);
@@ -5256,8 +5249,7 @@ public void visitAnnotatedType(JCAnnotatedType tree) {
52565249
Type underlyingType = attribType(tree.underlyingType, env);
52575250
Type annotatedType = underlyingType.preannotatedType();
52585251

5259-
if (!env.info.isAnonymousNewClass)
5260-
annotate.annotateTypeSecondStage(tree, tree.annotations, annotatedType);
5252+
annotate.annotateTypeSecondStage(tree, tree.annotations, annotatedType);
52615253
result = tree.type = annotatedType;
52625254
}
52635255

test/langtools/tools/javac/annotations/typeAnnotations/NewClassTypeAnnotation.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
* @run main NewClassTypeAnnotation
3232
*/
3333
import com.sun.source.tree.NewClassTree;
34-
import com.sun.source.tree.Tree;
3534
import com.sun.source.util.TaskEvent;
3635
import com.sun.source.util.TaskListener;
3736
import com.sun.source.util.TreePathScanner;
@@ -91,6 +90,7 @@ class Test<T> {
9190
9291
public void testMethod() {
9392
new Test<@TypeAnnotation String>();
93+
new Test<@TypeAnnotation String>() {};
9494
}
9595
}
9696
""");
@@ -112,11 +112,7 @@ class Scanner extends TreePathScanner<Void, Void> {
112112
@Override
113113
public Void visitNewClass(final NewClassTree node, final Void unused) {
114114
TypeMirror type = trees.getTypeMirror(getCurrentPath());
115-
System.err.println(">>> " + type);
116-
for (Tree t : getCurrentPath()) {
117-
System.err.println(t);
118-
}
119-
actual.add(String.format("Expression: %s, Type: %s", node, type));
115+
actual.add(String.format("Type: %s", type));
120116
return null;
121117
}
122118
}
@@ -144,8 +140,8 @@ public void finished(TaskEvent e) {
144140

145141
List<String> expected =
146142
List.of(
147-
"Expression: new Test<@TypeAnnotation String>(), Type:"
148-
+ " test.Test<java.lang.@test.Test.TypeAnnotation String>");
143+
"Type: test.Test<java.lang.@test.Test.TypeAnnotation String>",
144+
"Type: <anonymous test.Test<java.lang.@test.Test.TypeAnnotation String>>");
149145
if (!expected.equals(actual)) {
150146
throw new AssertionError("expected: " + expected + ", actual: " + actual);
151147
}

0 commit comments

Comments
 (0)