Skip to content

Commit e45c448

Browse files
committed
add support for retrieving all annotations on an element
1 parent 045bb76 commit e45c448

File tree

5 files changed

+45
-26
lines changed

5 files changed

+45
-26
lines changed

src/hotspot/share/jvmci/jvmciCompilerToVM.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,13 +3033,16 @@ static jbyteArray get_encoded_annotation_values(InstanceKlass* holder, Annotatio
30333033
typeArrayOop annotations_oop = Annotations::make_java_array(annotations_array, CHECK_NULL);
30343034
typeArrayHandle annotations = typeArrayHandle(THREAD, annotations_oop);
30353035

3036-
InstanceKlass** filter = filter_length == 1 ?
3037-
(InstanceKlass**) &filter_klass_pointers:
3038-
(InstanceKlass**) filter_klass_pointers;
3039-
objArrayOop filter_oop = oopFactory::new_objArray(vmClasses::Class_klass(), filter_length, CHECK_NULL);
3040-
objArrayHandle filter_classes(THREAD, filter_oop);
3041-
for (int i = 0; i < filter_length; i++) {
3042-
filter_classes->obj_at_put(i, filter[i]->java_mirror());
3036+
objArrayHandle filter_classes;
3037+
if (filter_length != 0) {
3038+
InstanceKlass** filter = filter_length == 1 ?
3039+
(InstanceKlass**) &filter_klass_pointers:
3040+
(InstanceKlass**) filter_klass_pointers;
3041+
objArrayOop filter_oop = oopFactory::new_objArray(vmClasses::Class_klass(), filter_length, CHECK_NULL);
3042+
filter_classes = objArrayHandle(THREAD, filter_oop);
3043+
for (int i = 0; i < filter_length; i++) {
3044+
filter_classes->obj_at_put(i, filter[i]->java_mirror());
3045+
}
30433046
}
30443047

30453048
// invoke VMSupport.encodeAnnotations

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,14 @@ public static byte[] encodeAnnotations(byte[] rawAnnotations,
191191
Class<?> declaringClass,
192192
ConstantPool cp,
193193
Class<? extends Annotation>[] selectAnnotationClasses) {
194-
for (Class<?> c : selectAnnotationClasses) {
195-
if (!c.isAnnotation()) {
196-
throw new IllegalArgumentException(c + " is not an annotation interface");
194+
if (selectAnnotationClasses != null) {
195+
if (selectAnnotationClasses.length == 0) {
196+
throw new IllegalArgumentException("annotation class selection must be null or non-empty");
197+
}
198+
for (Class<?> c : selectAnnotationClasses) {
199+
if (!c.isAnnotation()) {
200+
throw new IllegalArgumentException(c + " is not an annotation interface");
201+
}
197202
}
198203
}
199204
return encodeAnnotations(AnnotationParser.parseSelectAnnotations(rawAnnotations, cp, declaringClass, false, selectAnnotationClasses).values());

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/hotspot/CompilerToVM.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,14 @@
2323

2424
package jdk.vm.ci.hotspot;
2525

26-
import java.lang.reflect.Executable;
27-
import java.lang.reflect.Field;
28-
2926
import jdk.internal.misc.Unsafe;
3027
import jdk.vm.ci.code.BytecodeFrame;
3128
import jdk.vm.ci.code.InstalledCode;
3229
import jdk.vm.ci.code.InvalidInstalledCodeException;
3330
import jdk.vm.ci.code.stack.InspectedFrameVisitor;
3431
import jdk.vm.ci.common.InitTimer;
35-
import static jdk.vm.ci.common.InitTimer.timer;
3632
import jdk.vm.ci.common.JVMCIError;
3733
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
38-
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
3934
import jdk.vm.ci.meta.Constant;
4035
import jdk.vm.ci.meta.ConstantReflectionProvider;
4136
import jdk.vm.ci.meta.JavaConstant;
@@ -44,6 +39,12 @@
4439
import jdk.vm.ci.meta.ResolvedJavaMethod;
4540
import jdk.vm.ci.meta.ResolvedJavaType;
4641

42+
import java.lang.reflect.Executable;
43+
import java.lang.reflect.Field;
44+
45+
import static jdk.vm.ci.common.InitTimer.timer;
46+
import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.runtime;
47+
4748
/**
4849
* Calls from Java into HotSpot. The behavior of all the methods in this class that take a native
4950
* pointer as an argument (e.g., {@link #getSymbol(long)}) is undefined if the argument does not
@@ -1486,7 +1487,7 @@ native byte[] getEncodedFieldAnnotationValues(HotSpotResolvedObjectTypeImpl hold
14861487
Object filterTypes, int filterLength, long filterKlassPointers);
14871488

14881489
/**
1489-
* Helper for passing {@Klass*} values to native code.
1490+
* Helper for passing {@code Klass*} values to native code.
14901491
*/
14911492
static final class KlassPointers implements AutoCloseable {
14921493
final ResolvedJavaType[] types;
@@ -1498,20 +1499,23 @@ static final class KlassPointers implements AutoCloseable {
14981499
}
14991500

15001501
/**
1501-
* Gets the buffer in which to pass the {@Klass*} values to JNI.
1502+
* Gets the buffer in which to pass the {@code Klass*} values to JNI.
15021503
*
1503-
* @return a {@Klass*} value if {@code types.length == 1} otherwise the address of a native
1504-
* buffer holding an array of {@Klass*} values
1504+
* @return 0L if {@code types.length == 0}, a {@code Klass*} value if {@code types.length == 1}
1505+
* otherwise the address of a native buffer holding an array of {@code Klass*} values
15051506
*/
15061507
long buffer() {
1507-
int length = types.length;
1508+
long length = types.length;
1509+
if (length == 0) {
1510+
return 0L;
1511+
}
15081512
if (length == 1) {
15091513
return ((HotSpotResolvedObjectTypeImpl) types[0]).getKlassPointer();
15101514
} else {
15111515
pointersArray = unsafe.allocateMemory(length * Long.BYTES);
15121516
long pos = pointersArray;
1513-
for (int i = 0; i < types.length; i++) {
1514-
HotSpotResolvedObjectTypeImpl hsType = (HotSpotResolvedObjectTypeImpl) types[i];
1517+
for (ResolvedJavaType type : types) {
1518+
HotSpotResolvedObjectTypeImpl hsType = (HotSpotResolvedObjectTypeImpl) type;
15151519
unsafe.putLong(pos, hsType.getKlassPointer());
15161520
pos += Long.BYTES;
15171521
}
@@ -1521,7 +1525,7 @@ long buffer() {
15211525

15221526
@Override
15231527
public void close() {
1524-
if (types.length != 1 && pointersArray != 0) {
1528+
if (types.length > 1 && pointersArray != 0) {
15251529
unsafe.freeMemory(pointersArray);
15261530
pointersArray = 0;
15271531
}

src/jdk.internal.vm.ci/share/classes/jdk/vm/ci/meta/annotation/Annotated.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public interface Annotated {
3939
* Class initialization is not triggered for enum types referenced by the returned
4040
* annotation. This method ignores inherited annotations.
4141
*
42-
* @param types annotation types to select
42+
* @param types annotation types to select. If empty, then all declared annotations are returned.
4343
* @return an immutable map from annotation type to annotation of the annotations directly present
44-
* on this element that match {@code types}
44+
* on this element selected by {@code types}
4545
* @throws IllegalArgumentException if any type in {@code types} is not an annotation interface type
4646
* @throws UnsupportedOperationException if this operation is not supported
4747
*/

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ private static void getAnnotationValueExpectedToFail(Annotated annotated, Resolv
13051305

13061306
/**
13071307
* Tests that {@link AnnotationValue}s obtained from a {@link Class}, {@link Method} or
1308-
* {@link Field} matches {@link AnnotatedElement#getDeclaredAnnotations()} for the corresponding JVMCI
1308+
* {@link Field} match {@link AnnotatedElement#getDeclaredAnnotations()} for the corresponding JVMCI
13091309
* object.
13101310
*
13111311
* @param annotatedElement a {@link Class}, {@link Method} or {@link Field} object
@@ -1331,6 +1331,10 @@ public static List<AnnotationValue> getAnnotationValueTest(AnnotatedElement anno
13311331
private static List<AnnotationValue> testGetAnnotationValue(Annotated annotated, List<Annotation> annotations) throws AssertionError {
13321332
ResolvedJavaType suppressWarningsType = metaAccess.lookupJavaType(SuppressWarnings.class);
13331333
List<AnnotationValue> res = new ArrayList<>(annotations.size());
1334+
1335+
Map<ResolvedJavaType, AnnotationValue> allAnnotationValues = annotated.getDeclaredAnnotationValues();
1336+
assertEquals(annotations.size(), allAnnotationValues.size());
1337+
13341338
for (Annotation a : annotations) {
13351339
var annotationType = metaAccess.lookupJavaType(a.annotationType());
13361340
AnnotationValue av = annotated.getDeclaredAnnotationValue(annotationType);
@@ -1340,6 +1344,9 @@ private static List<AnnotationValue> testGetAnnotationValue(Annotated annotated,
13401344
AnnotationValue av2 = annotated.getDeclaredAnnotationValue(annotationType);
13411345
assertEquals(av, av2);
13421346

1347+
av2 = allAnnotationValues.get(annotationType);
1348+
assertAnnotationsEquals(a, av2);
1349+
13431350
Map<ResolvedJavaType, AnnotationValue> annotationValues = annotated.getDeclaredAnnotationValues(annotationType, suppressWarningsType, suppressWarningsType);
13441351
assertEquals(1, annotationValues.size());
13451352

0 commit comments

Comments
 (0)