Skip to content

Commit 6e004cd

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

File tree

7 files changed

+52
-29
lines changed

7 files changed

+52
-29
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: 4 additions & 6 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
@@ -62,7 +63,6 @@
6263
import org.junit.Assert;
6364
import org.junit.Test;
6465
import sun.reflect.annotation.TypeAnnotation;
65-
import sun.reflect.annotation.TypeAnnotationParser;
6666

6767
import java.io.ByteArrayOutputStream;
6868
import java.io.IOException;
@@ -207,19 +207,17 @@ private Method findTestMethod(Method apiMethod) {
207207
*/
208208
private static void getTypeAnnotationValuesTest(Field field) {
209209
ResolvedJavaField javaField = metaAccess.lookupJavaField(field);
210-
TypeAnnotation[] typeAnnotations = getTypeAnnotations(field);
210+
List<TypeAnnotation> typeAnnotations = getTypeAnnotations(field);
211211
List<TypeAnnotationValue> typeAnnotationValues = javaField.getTypeAnnotationValues();
212212
TestResolvedJavaType.assertTypeAnnotationsEquals(typeAnnotations, typeAnnotationValues);
213213
}
214214

215215
private static final Method fieldGetTypeAnnotationBytes = lookupMethod(Field.class, "getTypeAnnotationBytes0");
216-
private static final Method classGetConstantPool = lookupMethod(Class.class, "getConstantPool");
217216

218-
private static TypeAnnotation[] getTypeAnnotations(Field f) {
217+
private static List<TypeAnnotation> getTypeAnnotations(Field f) {
219218
byte[] rawAnnotations = invokeMethod(fieldGetTypeAnnotationBytes, f);
220219
Class<?> container = f.getDeclaringClass();
221-
jdk.internal.reflect.ConstantPool cp = invokeMethod(classGetConstantPool, container);
222-
return TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, container);
220+
return TestResolvedJavaType.getTypeAnnotations(rawAnnotations, container);
223221
}
224222

225223
@Test

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

Lines changed: 4 additions & 6 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
@@ -69,7 +70,6 @@
6970
import org.junit.Test;
7071

7172
import sun.reflect.annotation.TypeAnnotation;
72-
import sun.reflect.annotation.TypeAnnotationParser;
7373

7474
import java.lang.annotation.Annotation;
7575
import java.lang.annotation.ElementType;
@@ -603,7 +603,7 @@ static void methodWithThreeAnnotations() {
603603
*/
604604
private static void getTypeAnnotationValuesTest(Executable executable) {
605605
ResolvedJavaMethod method = metaAccess.lookupJavaMethod(executable);
606-
TypeAnnotation[] typeAnnotations = getTypeAnnotations(executable);
606+
List<TypeAnnotation> typeAnnotations = getTypeAnnotations(executable);
607607
List<TypeAnnotationValue> typeAnnotationValues = method.getTypeAnnotationValues();
608608
TestResolvedJavaType.assertTypeAnnotationsEquals(typeAnnotations, typeAnnotationValues);
609609
}
@@ -669,13 +669,11 @@ public static void assertParameterAnnotationsEquals(
669669
}
670670

671671
private static final Method executableGetTypeAnnotationBytes = lookupMethod(Executable.class, "getTypeAnnotationBytes");
672-
private static final Method classGetConstantPool = lookupMethod(Class.class, "getConstantPool");
673672

674-
private static TypeAnnotation[] getTypeAnnotations(Executable e) {
673+
private static List<TypeAnnotation> getTypeAnnotations(Executable e) {
675674
byte[] rawAnnotations = invokeMethod(executableGetTypeAnnotationBytes, e);
676675
Class<?> container = e.getDeclaringClass();
677-
jdk.internal.reflect.ConstantPool cp = invokeMethod(classGetConstantPool, container);
678-
return TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, container);
676+
return TestResolvedJavaType.getTypeAnnotations(rawAnnotations, container);
679677
}
680678

681679
@Test

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

Lines changed: 5 additions & 8 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
@@ -50,7 +51,6 @@
5051

5152
package jdk.vm.ci.runtime.test;
5253

53-
import jdk.internal.vm.test.AnnotationTestInput;
5454
import jdk.internal.vm.test.AnnotationTestInput.AnnotatedRecord;
5555
import jdk.vm.ci.meta.JavaType;
5656
import jdk.vm.ci.meta.ResolvedJavaMethod;
@@ -59,7 +59,6 @@
5959
import jdk.vm.ci.meta.annotation.TypeAnnotationValue;
6060
import org.junit.Test;
6161
import sun.reflect.annotation.TypeAnnotation;
62-
import sun.reflect.annotation.TypeAnnotationParser;
6362

6463
import java.lang.reflect.Field;
6564
import java.lang.reflect.Method;
@@ -141,7 +140,7 @@ public void getAnnotationValuesTest() {
141140
*/
142141
private static void getTypeAnnotationValuesTest(RecordComponent rc) {
143142
ResolvedJavaRecordComponent resolvedRc = metaAccess.lookupJavaRecordComponent(rc);
144-
TypeAnnotation[] typeAnnotations = getTypeAnnotations(rc);
143+
List<TypeAnnotation> typeAnnotations = getTypeAnnotations(rc);
145144
List<TypeAnnotationValue> typeAnnotationValues = resolvedRc.getTypeAnnotationValues();
146145
TestResolvedJavaType.assertTypeAnnotationsEquals(typeAnnotations, typeAnnotationValues);
147146
if (!typeAnnotationValues.isEmpty()) {
@@ -150,13 +149,11 @@ private static void getTypeAnnotationValuesTest(RecordComponent rc) {
150149
}
151150

152151
private static final Field recordComponentTypeAnnotations = lookupField(RecordComponent.class, "typeAnnotations");
153-
private static final Method classGetConstantPool = lookupMethod(Class.class, "getConstantPool");
154152

155-
private static TypeAnnotation[] getTypeAnnotations(RecordComponent rc) {
156-
byte[] rawAnnotations = getFieldValue(recordComponentTypeAnnotations, rc);
153+
private static List<TypeAnnotation> getTypeAnnotations(RecordComponent rc) {
157154
Class<?> container = rc.getDeclaringRecord();
158-
jdk.internal.reflect.ConstantPool cp = invokeMethod(classGetConstantPool, container);
159-
return TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, container);
155+
byte[] rawAnnotations = getFieldValue(recordComponentTypeAnnotations, rc);
156+
return TestResolvedJavaType.getTypeAnnotations(rawAnnotations, container);
160157
}
161158

162159
@Test

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

Lines changed: 16 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,23 @@ 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 getTypeAnnotations(rawAnnotations, c);
1277+
}
1278+
1279+
public static List<TypeAnnotation> getTypeAnnotations(byte[] rawAnnotations, Class<?> container) {
1280+
ConstantPool cp = invokeMethod(classGetConstantPool, container);
1281+
return Stream.of(TypeAnnotationParser.parseTypeAnnotations(rawAnnotations, cp, null, false, container))
1282+
.filter(ta -> ta.getAnnotation() != null)
1283+
.toList();
12751284
}
12761285

12771286
@Test
12781287
public void getAnnotationValuesTest() {
12791288
getAnnotationValuesTest(AnnotationTestInput.AnnotatedClass.class);
1289+
getAnnotationValuesTest(AnnotationTestInput.AnnotatedClass2.class);
12801290
getAnnotationValuesTest(int.class);
12811291
getAnnotationValuesTest(void.class);
12821292
for (Class<?> c : classes) {
@@ -1383,11 +1393,11 @@ public static List<AnnotationValue> getAnnotationValuesTest(AnnotatedElement ann
13831393
}
13841394

13851395
public static void assertTypeAnnotationsEquals(
1386-
TypeAnnotation[] typeAnnotations,
1396+
List<TypeAnnotation> typeAnnotations,
13871397
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];
1398+
assertEquals(typeAnnotations.size(), typeAnnotationValues.size());
1399+
for (int i = 0; i < typeAnnotations.size(); i++) {
1400+
TypeAnnotation typeAnnotation = typeAnnotations.get(i);
13911401
TypeAnnotationValue typeAnnotationValue = typeAnnotationValues.get(i);
13921402
assertTypeAnnotationEquals(typeAnnotation, typeAnnotationValue);
13931403
}

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)