-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix losing type annotations with different visibility
In case a single type annotation target (such as a `MethodInfo`) has multiple type annotations, some of them runtime-retained and some class-retained, all annotations of one kind are lost. This is because `Indexer.processTypeAnnotations()` was written with the assumption that it is only called once for any given target. This was true in Jandex 2, which only supported runtime-retained annotations, but since Jandex 3.0, class-retained annotations are supported as well. Hence, the method can legally be called twice for any given target. Fortunately, this issue doesn't exist for non-type annotations.
- Loading branch information
Showing
5 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
core/src/test/java/org/jboss/jandex/test/ClassAndRuntimeTypeAnnotationsOnMethodTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.jboss.jandex.test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
import java.io.IOException; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Index; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.jboss.jandex.PrimitiveType; | ||
import org.jboss.jandex.Type; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ClassAndRuntimeTypeAnnotationsOnMethodTest { | ||
@Retention(RetentionPolicy.CLASS) | ||
@Target(ElementType.TYPE_USE) | ||
@interface ClassAnnotation { | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE_USE) | ||
@interface RuntimeAnnotation { | ||
} | ||
|
||
static class TestMethod { | ||
static void method(@RuntimeAnnotation int foo, @ClassAnnotation String bar) { | ||
} | ||
} | ||
|
||
@Test | ||
public void test() throws IOException { | ||
Index index = Index.of(ClassAnnotation.class, RuntimeAnnotation.class, TestMethod.class); | ||
MethodInfo method = index.getClassByName(TestMethod.class).firstMethod("method"); | ||
assertNotNull(method); | ||
assertEquals(2, method.parametersCount()); | ||
|
||
Type param0 = method.parameterType(0); | ||
assertEquals(Type.Kind.PRIMITIVE, param0.kind()); | ||
assertEquals(PrimitiveType.Primitive.INT, param0.asPrimitiveType().primitive()); | ||
assertEquals(1, param0.annotations().size()); | ||
assertEquals(RuntimeAnnotation.class.getName(), param0.annotations().get(0).name().toString()); | ||
|
||
Type param1 = method.parameterType(1); | ||
assertEquals(Type.Kind.CLASS, param1.kind()); | ||
assertEquals(DotName.STRING_NAME, param1.asClassType().name()); | ||
assertEquals(1, param1.annotations().size()); | ||
assertEquals(ClassAnnotation.class.getName(), param1.annotations().get(0).name().toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package test; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
// copy of `MyAnnotation`, just different retention | ||
@Retention(RetentionPolicy.CLASS) | ||
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, | ||
ElementType.TYPE_USE }) | ||
@interface MyClassAnnotation { | ||
String value(); | ||
} |
4 changes: 4 additions & 0 deletions
4
test-data/src/main/java/test/RecordWithDifferentVisibilityAnnotation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package test; | ||
|
||
public record RecordWithDifferentVisibilityAnnotation(@MyAnnotation("foo") int foo, @MyClassAnnotation("bar") String bar) { | ||
} |