Skip to content

Commit 835d323

Browse files
committed
filter out TypeAnnotations that have a null annotation
1 parent 3d14311 commit 835d323

File tree

7 files changed

+37
-9
lines changed

7 files changed

+37
-9
lines changed

src/java.base/share/classes/jdk/internal/vm/VMSupport.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,19 @@ private static void encodeMemberValue(DataOutputStream dos, Object value) throws
468468
* @param dos stream for assembling the byte array
469469
*/
470470
public static void encodeTypeAnnotations(DataOutputStream dos, TypeAnnotation[] typeAnnotations) throws IOException {
471-
writeLength(dos, typeAnnotations.length);
471+
int wellFormed = 0;
472472
for (TypeAnnotation ta : typeAnnotations) {
473-
encodeTypeAnnotation(dos, ta);
473+
// Ignore not well-formed type annotations like
474+
// TypeAnnotationParser.mapTypeAnnotations does
475+
if (ta.getAnnotation() != null) {
476+
wellFormed++;
477+
}
478+
}
479+
writeLength(dos, wellFormed);
480+
for (TypeAnnotation ta : typeAnnotations) {
481+
if (ta.getAnnotation() != null) {
482+
encodeTypeAnnotation(dos, ta);
483+
}
474484
}
475485
}
476486

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaField.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/MemberTypeChanged.java
3232
* TestResolvedJavaType.java
3333
* @clean jdk.internal.vm.test.AnnotationTestInput$Missing
34+
* jdk.internal.vm.test.AnnotationTestInput$MissingTypeQualifier
3435
* @compile ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberDeleted.java
3536
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberAdded.java
3637
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberTypeChanged.java

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/MemberTypeChanged.java
3232
* TestResolvedJavaType.java
3333
* @clean jdk.internal.vm.test.AnnotationTestInput$Missing
34+
* jdk.internal.vm.test.AnnotationTestInput$MissingTypeQualifier
3435
* @compile ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberDeleted.java
3536
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberAdded.java
3637
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberTypeChanged.java

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaRecordComponent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/MemberTypeChanged.java
3232
* TestResolvedJavaType.java
3333
* @clean jdk.internal.vm.test.AnnotationTestInput$Missing
34+
* jdk.internal.vm.test.AnnotationTestInput$MissingTypeQualifier
3435
* @compile ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberDeleted.java
3536
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberAdded.java
3637
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberTypeChanged.java

test/hotspot/jtreg/compiler/jvmci/jdk.vm.ci.runtime.test/src/jdk/vm/ci/runtime/test/TestResolvedJavaType.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/MemberAdded.java
3232
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/MemberTypeChanged.java
3333
* @clean jdk.internal.vm.test.AnnotationTestInput$Missing
34+
* jdk.internal.vm.test.AnnotationTestInput$MissingTypeQualifier
3435
* @compile ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberDeleted.java
3536
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberAdded.java
3637
* ../../../../../../../../../../../jdk/jdk/internal/vm/AnnotationEncodingDecoding/alt/MemberTypeChanged.java
@@ -1249,6 +1250,7 @@ private Method findTestMethod(Method apiMethod) {
12491250
@Test
12501251
public void getTypeAnnotationValuesTest() {
12511252
getTypeAnnotationValuesTest(AnnotationTestInput.AnnotatedClass.class);
1253+
getTypeAnnotationValuesTest(AnnotationTestInput.AnnotatedClass2.class);
12521254
getTypeAnnotationValuesTest(int.class);
12531255
getTypeAnnotationValuesTest(void.class);
12541256
for (Class<?> c : classes) {
@@ -1268,15 +1270,18 @@ private static void getTypeAnnotationValuesTest(Class<?> cls) {
12681270
private static final Method classGetRawTypeAnnotations = lookupMethod(Class.class, "getRawTypeAnnotations");
12691271
private static final Method classGetConstantPool = lookupMethod(Class.class, "getConstantPool");
12701272

1271-
private static TypeAnnotation[] getTypeAnnotations(Class<?> c) {
1273+
private static List<TypeAnnotation> getTypeAnnotations(Class<?> c) {
12721274
byte[] rawAnnotations = invokeMethod(classGetRawTypeAnnotations, c);
12731275
ConstantPool cp = invokeMethod(classGetConstantPool, c);
1274-
return TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, c);
1276+
return Stream.of(TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, c))
1277+
.filter(ta -> ta.getAnnotation() != null)
1278+
.toList();
12751279
}
12761280

12771281
@Test
12781282
public void getAnnotationValuesTest() {
12791283
getAnnotationValuesTest(AnnotationTestInput.AnnotatedClass.class);
1284+
getAnnotationValuesTest(AnnotationTestInput.AnnotatedClass2.class);
12801285
getAnnotationValuesTest(int.class);
12811286
getAnnotationValuesTest(void.class);
12821287
for (Class<?> c : classes) {
@@ -1383,11 +1388,11 @@ public static List<AnnotationValue> getAnnotationValuesTest(AnnotatedElement ann
13831388
}
13841389

13851390
public static void assertTypeAnnotationsEquals(
1386-
TypeAnnotation[] typeAnnotations,
1391+
List<TypeAnnotation> typeAnnotations,
13871392
List<TypeAnnotationValue> typeAnnotationValues) throws AssertionError {
1388-
assertEquals(typeAnnotations.length, typeAnnotationValues.size());
1389-
for (int i = 0; i < typeAnnotations.length; i++) {
1390-
TypeAnnotation typeAnnotation = typeAnnotations[i];
1393+
assertEquals(typeAnnotations.size(), typeAnnotationValues.size());
1394+
for (int i = 0; i < typeAnnotations.size(); i++) {
1395+
TypeAnnotation typeAnnotation = typeAnnotations.get(i);
13911396
TypeAnnotationValue typeAnnotationValue = typeAnnotationValues.get(i);
13921397
assertTypeAnnotationEquals(typeAnnotation, typeAnnotationValue);
13931398
}

test/jdk/jdk/internal/vm/AnnotationEncodingDecoding/AnnotationTestInput.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.lang.annotation.RetentionPolicy;
3131
import java.lang.annotation.Target;
3232
import java.lang.reflect.Method;
33+
import java.util.function.Predicate;
3334

3435
public class AnnotationTestInput {
3536

@@ -177,7 +178,9 @@ public static class InheritedName3 extends Super3 {}
177178
@SuppressWarnings({"rawtypes", "all"})
178179
public static class AnnotatedClass extends @TypeQualifier Thread implements @TypeQualifier Serializable {}
179180

180-
public static record AnnotatedRecord(@TypeQualifier @Named("obj1Component Name") String obj1Component, @Missing @NestedAnno("int1 value") int int1Component, @Missing float componentWithMissingAnno) {}
181+
public record AnnotatedRecord(@TypeQualifier @Named("obj1Component Name") String obj1Component, @Missing @NestedAnno("int1 value") int int1Component, @Missing float componentWithMissingAnno) {}
182+
183+
public static abstract class AnnotatedClass2<T> implements Predicate<@MissingTypeQualifier T> {}
181184

182185
@Single(string = "a",
183186
stringArray = {"a", "b"},
@@ -248,6 +251,12 @@ public static record AnnotatedRecord(@TypeQualifier @Named("obj1Component Name")
248251
String comment() default "";
249252
}
250253

254+
// Define a type-use annotation that is missing at runtime
255+
@Target(ElementType.TYPE_USE)
256+
@Retention(RetentionPolicy.RUNTIME)
257+
@interface MissingTypeQualifier {
258+
}
259+
251260
@Retention(RetentionPolicy.RUNTIME)
252261
public @interface NestedAnno {
253262
String value();

test/jdk/jdk/internal/vm/AnnotationEncodingDecoding/TestAnnotationEncodingDecoding.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* @modules java.base/jdk.internal.vm
2828
* java.base/sun.reflect.annotation
2929
* @clean jdk.internal.vm.test.AnnotationTestInput$Missing
30+
* jdk.internal.vm.test.AnnotationTestInput$MissingTypeQualifier
3031
* @compile alt/MemberDeleted.java alt/MemberTypeChanged.java
3132
* @run testng/othervm
3233
* jdk.internal.vm.test.TestAnnotationEncodingDecoding

0 commit comments

Comments
 (0)