Skip to content

Commit

Permalink
Add system property to enable runtime invisible annotation indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Aug 9, 2021
1 parent 36da74f commit 4d6fd30
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/jboss/jandex/Indexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public final class Indexer {
private final static int HAS_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS = 18;

private final static byte[] INIT_METHOD_NAME = Utils.toUTF8("<init>");
public final static String PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS = "jandex.runtime-invisible-annotations";

private static class InnerClassInfo {
private InnerClassInfo(DotName innerClass, DotName enclosingClass, String simpleName, int flags) {
Expand Down Expand Up @@ -453,12 +454,20 @@ private boolean processAttribute(DataInputStream data, AnnotationTarget target,

switch (annotationAttribute) {
case HAS_RUNTIME_INVISIBLE_ANNOTATIONS:
if (!Boolean.getBoolean(PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS)) {
break;
}
// Fall through
case HAS_RUNTIME_ANNOTATION:
processAnnotations(data, target);
processed = true;
break;

case HAS_RUNTIME_INVISIBLE_PARAM_ANNOTATIONS:
if (!Boolean.getBoolean(PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS)) {
break;
}
// Fall through
case HAS_RUNTIME_PARAM_ANNOTATION:
if (!(target instanceof MethodInfo))
throw new IllegalStateException("RuntimeVisibleParameterAnnotations appeared on a non-method");
Expand All @@ -471,6 +480,10 @@ private boolean processAttribute(DataInputStream data, AnnotationTarget target,
break;

case HAS_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS:
if (!Boolean.getBoolean(PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS)) {
break;
}
// Fall through
case HAS_RUNTIME_TYPE_ANNOTATION:
processTypeAnnotations(data, target);
processed = true;
Expand Down
33 changes: 20 additions & 13 deletions src/test/java/org/jboss/jandex/test/BasicTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void doSomething(int x, long y){}
@MethodAnnotation2
@MethodAnnotation4
void doSomething(int x, long y, String foo){}

public class Nested {
public Nested(int noAnnotation) {}
public Nested(@ParameterAnnotation byte annotated) {}
Expand All @@ -135,11 +135,11 @@ public Nested(@ParameterAnnotation byte annotated) {}

public enum Enum {
A(1), B(2);

private Enum(int noAnnotation) {}
private Enum(@ParameterAnnotation byte annotated) {}
}

@TestAnnotation(name = "Test", ints = { 1, 2, 3, 4, 5 }, klass = Void.class, nested = @NestedAnnotation(1.34f), nestedArray = {
@NestedAnnotation(3.14f), @NestedAnnotation(2.27f) }, enums = { ElementType.TYPE, ElementType.PACKAGE }, longValue = 10)
public static class NestedA implements Serializable {
Expand Down Expand Up @@ -425,8 +425,15 @@ class Target {
void execute(@RuntimeInvisible(placement = "arg") String arg) {
}
}
Index index;

try {
System.setProperty(Indexer.PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS, "true");
index = Index.of(Target.class);
} finally {
System.clearProperty(Indexer.PROP_ENABLE_RUNTIME_INVISIBLE_ANNOTATIONS);
}

Index index = Index.of(Target.class);
DotName annoName = DotName.createSimple(RuntimeInvisible.class.getName());
List<AnnotationInstance> rtInvisible = index.getAnnotations(annoName);
assertEquals(2, rtInvisible.size());
Expand Down Expand Up @@ -492,40 +499,40 @@ private void verifyDummy(Index index, boolean v2features) {
assertEquals(MethodAnnotation2.class.getName(), method.annotation(DotName.createSimple(MethodAnnotation2.class.getName())).name().toString());
assertEquals(MethodAnnotation4.class.getName(), method.annotation(DotName.createSimple(MethodAnnotation4.class.getName())).name().toString());
assertFalse(method.hasAnnotation(DotName.createSimple(MethodAnnotation3.class.getName())));

assertEquals("x", method.parameterName(0));
assertEquals("y", method.parameterName(1));
assertEquals("foo", method.parameterName(2));

ClassInfo nested = index.getClassByName(DotName.createSimple(DummyClass.Nested.class.getName()));
assertNotNull(nested);
// synthetic param counts here
MethodInfo nestedConstructor1 = nested.method("<init>",
MethodInfo nestedConstructor1 = nested.method("<init>",
Type.create(DotName.createSimple(DummyClass.class.getName()), Type.Kind.CLASS), PrimitiveType.INT);
assertNotNull(nestedConstructor1);
// synthetic param counts here
assertEquals(2, nestedConstructor1.parameters().size());
// synthetic param does not counts here
assertEquals("noAnnotation", nestedConstructor1.parameterName(0));

MethodInfo nestedConstructor2 = nested.method("<init>",
MethodInfo nestedConstructor2 = nested.method("<init>",
Type.create(DotName.createSimple(DummyClass.class.getName()), Type.Kind.CLASS), PrimitiveType.BYTE);
assertNotNull(nestedConstructor2);
// synthetic param counts here
assertEquals(2, nestedConstructor2.parameters().size());
// synthetic param does not counts here
assertEquals("annotated", nestedConstructor2.parameterName(0));

AnnotationInstance paramAnnotation = nestedConstructor2.annotation(DotName.createSimple(ParameterAnnotation.class.getName()));
assertNotNull(paramAnnotation);
assertEquals(Kind.METHOD_PARAMETER, paramAnnotation.target().kind());
assertEquals("annotated", paramAnnotation.target().asMethodParameter().name());
assertEquals(0, paramAnnotation.target().asMethodParameter().position());

ClassInfo enumClass = index.getClassByName(DotName.createSimple(Enum.class.getName()));
assertNotNull(enumClass);
// synthetic param counts here (for ECJ)
MethodInfo enumConstructor1 = enumClass.method("<init>",
MethodInfo enumConstructor1 = enumClass.method("<init>",
Type.create(DotName.createSimple("java.lang.String"), Type.Kind.CLASS), PrimitiveType.INT, PrimitiveType.INT);
if(enumConstructor1 == null) {
enumConstructor1 = enumClass.method("<init>", PrimitiveType.INT);
Expand All @@ -539,7 +546,7 @@ private void verifyDummy(Index index, boolean v2features) {
// synthetic param does not counts here
assertEquals("noAnnotation", enumConstructor1.parameterName(0));

MethodInfo enumConstructor2 = enumClass.method("<init>",
MethodInfo enumConstructor2 = enumClass.method("<init>",
Type.create(DotName.createSimple("java.lang.String"), Type.Kind.CLASS), PrimitiveType.INT, PrimitiveType.BYTE);
if(enumConstructor2 == null) {
enumConstructor2 = enumClass.method("<init>", PrimitiveType.BYTE);
Expand All @@ -552,7 +559,7 @@ private void verifyDummy(Index index, boolean v2features) {
}
// synthetic param does not counts here
assertEquals("annotated", enumConstructor2.parameterName(0));

paramAnnotation = enumConstructor2.annotation(DotName.createSimple(ParameterAnnotation.class.getName()));
assertNotNull(paramAnnotation);
assertEquals(Kind.METHOD_PARAMETER, paramAnnotation.target().kind());
Expand Down

0 comments on commit 4d6fd30

Please sign in to comment.