Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read RuntimeInvisible*Annotations in Indexer #129

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/main/java/org/jboss/jandex/IndexWriterV2.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,8 @@ private void writeAnnotations(PackedDataOutputStream stream, AnnotationInstance[
}
}

private void writeAnnotations(PackedDataOutputStream stream, Collection<AnnotationInstance> annotations) throws IOException {
private void writeAnnotations(PackedDataOutputStream stream, Collection<AnnotationInstance> annotations)
throws IOException {
if (annotations.isEmpty()) {
writeAnnotations(stream, AnnotationInstance.EMPTY_ARRAY);
} else {
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,8 @@ private void processAttributes(DataInputStream data, AnnotationTarget target) th
byte annotationAttribute = constantPoolAnnoAttrributes[index - 1];
if (annotationAttribute == HAS_RUNTIME_ANNOTATION || annotationAttribute == HAS_RUNTIME_INVISIBLE_ANNOTATION) {
processAnnotations(data, target, annotationAttribute == HAS_RUNTIME_ANNOTATION);
} else if (annotationAttribute == HAS_RUNTIME_PARAM_ANNOTATION || annotationAttribute == HAS_RUNTIME_INVISIBLE_PARAM_ANNOTATION) {
} else if (annotationAttribute == HAS_RUNTIME_PARAM_ANNOTATION
|| annotationAttribute == HAS_RUNTIME_INVISIBLE_PARAM_ANNOTATION) {
if (!(target instanceof MethodInfo)) {
if (annotationAttribute == HAS_RUNTIME_PARAM_ANNOTATION) {
throw new IllegalStateException("RuntimeVisibleParameterAnnotations appeared on a non-method");
Expand All @@ -453,9 +454,11 @@ private void processAttributes(DataInputStream data, AnnotationTarget target) th
}
int numParameters = data.readUnsignedByte();
for (short p = 0; p < numParameters; p++) {
processAnnotations(data, new MethodParameterInfo((MethodInfo) target, p), annotationAttribute == HAS_RUNTIME_PARAM_ANNOTATION);
processAnnotations(data, new MethodParameterInfo((MethodInfo) target, p),
annotationAttribute == HAS_RUNTIME_PARAM_ANNOTATION);
}
} else if (annotationAttribute == HAS_RUNTIME_TYPE_ANNOTATION || annotationAttribute == HAS_RUNTIME_INVISIBLE_TYPE_ANNOTATION) {
} else if (annotationAttribute == HAS_RUNTIME_TYPE_ANNOTATION
|| annotationAttribute == HAS_RUNTIME_INVISIBLE_TYPE_ANNOTATION) {
processTypeAnnotations(data, target, annotationAttribute == HAS_RUNTIME_TYPE_ANNOTATION);
} else if (annotationAttribute == HAS_SIGNATURE) {
processSignature(data, target);
Expand Down Expand Up @@ -1958,7 +1961,7 @@ && match(buf, offset, RUNTIME_INVISIBLE_PARAM_ANNOTATIONS)) {
annoAttributes[pos] = HAS_RUNTIME_INVISIBLE_PARAM_ANNOTATION;
} else if (len == RUNTIME_INVISIBLE_TYPE_ANNOTATIONS_LEN
&& match(buf, offset, RUNTIME_INVISIBLE_TYPE_ANNOTATIONS)) {
annoAttributes[pos] = HAS_RECORD;
annoAttributes[pos] = HAS_RUNTIME_INVISIBLE_TYPE_ANNOTATION;
}
offset += len;
break;
Expand Down
71 changes: 50 additions & 21 deletions core/src/test/java/org/jboss/jandex/test/BasicTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedParameterizedType;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.FileVisitResult;
Expand All @@ -49,7 +52,10 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget.Kind;
Expand Down Expand Up @@ -77,6 +83,12 @@ public class BasicTestCase {

}

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE_PARAMETER, ElementType.TYPE_USE })
public @interface TypeUseAnnotation {

}

@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation {
String name();
Expand Down Expand Up @@ -123,11 +135,20 @@ public class BasicTestCase {
}

@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.TYPE, ElementType.PARAMETER })
@Target({ ElementType.TYPE, ElementType.PARAMETER, ElementType.TYPE_USE })
@interface RuntimeInvisible {
String placement();
}

@RuntimeInvisible(placement = "class")
class RuntimeInvisibleTarget {
@SuppressWarnings("unused")
@MethodAnnotation1
void execute(
@ParameterAnnotation @RuntimeInvisible(placement = "arg") List<@TypeUseAnnotation @RuntimeInvisible(placement = "type use") String> arg) {
}
}

// @formatter:off
@TestAnnotation(name = "Test", override = "somethingelse", ints = { 1, 2, 3, 4, 5 }, klass = Void.class,
nested = @NestedAnnotation(1.34f), nestedArray = { @NestedAnnotation(3.14f), @NestedAnnotation(2.27f) },
Expand Down Expand Up @@ -475,39 +496,47 @@ public void testNullClass() throws IOException {
}

@Test
public void testRuntimeInvisiblePresentInV11() throws IOException {
@RuntimeInvisible(placement = "class")
class Target {
@SuppressWarnings("unused")
void execute(@RuntimeInvisible(placement = "arg") String arg) {
}
}
public void testRuntimeInvisiblePresentInV11() throws Exception {
// Check @RuntimeInvisible not available to reflection APIs
Class<RuntimeInvisibleTarget> testTarget = RuntimeInvisibleTarget.class;
assertEquals(0, testTarget.getDeclaredAnnotations().length);

Parameter[] executeParams = testTarget.getDeclaredMethod("execute", List.class).getParameters();
assertEquals(1, executeParams[0].getDeclaredAnnotations().length);

assertEquals(0, Target.class.getDeclaredAnnotations().length);
assertEquals(0, Target.class.getDeclaredMethods()[0].getDeclaredAnnotations().length);
AnnotatedParameterizedType apt = (AnnotatedParameterizedType) executeParams[0].getAnnotatedType();
AnnotatedType argGenericType = apt.getAnnotatedActualTypeArguments()[0];
assertEquals(1, argGenericType.getDeclaredAnnotations().length);

Index index = testClassConstantSerialisation(Index.of(Target.class), 11);
Index index = testClassConstantSerialisation(Index.of(testTarget), 11);
DotName annoName = DotName.createSimple(RuntimeInvisible.class.getName());
List<AnnotationInstance> rtInvisible = index.getAnnotations(annoName);

assertEquals(2, rtInvisible.size());
assertEquals(4, rtInvisible.size());
Map<String, List<AnnotationInstance>> placements = new HashMap<>(3);

for (AnnotationInstance a : rtInvisible) {
assertFalse(a.runtimeVisible());
String placement = a.value("placement").asString();
placements.compute(placement, (k, v) -> {
if (v == null) {
v = new ArrayList<>();
}
v.add(a);
return v;
});
}

assertEquals(1, placements.get("class").size());
// @RuntimeInvisible recorded on both method parameter and method parameter type
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is interesting, but correct I guess? Since the annotation has ElementType.PARAMETER, ElementType.TYPE_USE.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, exactly. I wasn't expecting it initially, but it makes sense.

assertEquals(2, placements.get("arg").size());
assertEquals(1, placements.get("type use").size());
}

@Test
public void testRuntimeInvisibleAbsentFromV10() throws IOException {
@RuntimeInvisible(placement = "class")
class Target {
@SuppressWarnings("unused")
@MethodAnnotation1
void execute(@RuntimeInvisible(placement = "arg") String arg) {
}
}

Index index = testClassConstantSerialisation(Index.of(Target.class), 10);
Class<RuntimeInvisibleTarget> testTarget = RuntimeInvisibleTarget.class;
Index index = testClassConstantSerialisation(Index.of(testTarget), 10);
DotName annoName = DotName.createSimple(RuntimeInvisible.class.getName());
List<AnnotationInstance> rtInvisible = index.getAnnotations(annoName);

Expand Down