Skip to content

Commit

Permalink
8207017: Type annotations on anonymous classes in initializer blocks …
Browse files Browse the repository at this point in the history
…not written to class file

Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Feb 14, 2023
1 parent 8c2c8b3 commit 2ef001e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,7 @@ private void propagateNewClassAnnotationsToOwner(JCNewClass tree) {
newattrs.append(new Attribute.TypeCompound(old.type, old.values, pos));
}
}

sym.owner.appendUniqueTypeAttributes(newattrs.toList());
appendTypeAnnotationsToOwner(sym, newattrs.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/*
* @test
* @bug 8198945 8207018
* @bug 8198945 8207018 8207017
* @summary Invalid RuntimeVisibleTypeAnnotations for annotation on anonymous class type parameter
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
Expand Down Expand Up @@ -94,11 +94,22 @@ private void g() {
new @TA(3) AnonymousClassTest.@TA(4) Inner() {};
}

// intance initializer
{
new @TA(5) Object() {};
}

// static initializer
static {
new @TA(6) Object() {};
}

public static void main(String args[]) throws Exception {
testAnonymousClassDeclaration();
testTopLevelMethod();
testInnerClassMethod();
testQualifiedSuperType();
testInstanceAndClassInit();
}

static void testAnonymousClassDeclaration() throws Exception {
Expand Down Expand Up @@ -165,6 +176,21 @@ static void testQualifiedSuperType() throws Exception {
}
}

static void testInstanceAndClassInit() throws Exception {
ClassFile cf = ClassFile.read(Paths.get(ToolBox.testClasses, "AnonymousClassTest.class"));
Method method = findMethod(cf, "<init>");
Set<TypeAnnotation> annotations = getRuntimeVisibleTypeAnnotations(method);
assertEquals(
Set.of("@LAnonymousClassTest$TA;(5) NEW, offset=4, location=[INNER_TYPE]"),
annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet()) );

method = findMethod(cf, "<clinit>");
annotations = getRuntimeVisibleTypeAnnotations(method);
assertEquals(
Set.of("@LAnonymousClassTest$TA;(6) NEW, offset=0, location=[INNER_TYPE]"),
annotations.stream().map(a -> annotationDebugString(cf, a)).collect(toSet()) );
}

// Returns the Method's RuntimeVisibleTypeAnnotations, and asserts that there are no RVTIs
// erroneously associated with the Method instead of its Code attribute.
private static Set<TypeAnnotation> getRuntimeVisibleTypeAnnotations(Method method) {
Expand Down

1 comment on commit 2ef001e

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.