Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

jandex get indirect annotations

34 views
Skip to first unread message

Ivan Kulaga

unread,
Feb 23, 2024, 11:46:17 AM2/23/24
to SmallRye
Hello, beatiful smallrye community! Could you please tell me if there is a way of getting all "indirect" annotations from a class?
For example, i have two annotations:

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ ElementType.TYPE })
public @interface Indirect{}


and

@Retention(RetentionPolicy.RUNTIME)
@Documented
@Target({ ElementType.TYPE })
@Indirect
public @interface Direct{}


I am annotating my class directly with the second annotation:
@Direct
public class MyClass{}


and building jandex index:
Index index = Index.of(MyClass.class);

This index contains only @Direct annotation info. Is there a way to load @Indirect annotation info into an index?

Ladislav Thon

unread,
Feb 24, 2024, 5:47:51 AM2/24/24
to smal...@googlegroups.com
Hi,

there are 2 parts to your question.

1. Jandex is a [relatively] faithful representation of JVM class files. If it's not in the bytecode, you're not getting it from Jandex. Your index is created by `Index.of(MyClass.class)` and hence only contains information from the class file of `MyClass`. That is, you can get the information that the class is annotated by `@Direct`, but you cannot obtain the declaration of `Direct`. As a perhaps better example, consider a somewhat different declaration of the `Direct` annotation type. Say it contains a member with a default value, like

public @interface Direct {
    String value() default "foobar";
}

Further, assume that the occurrence of `@Direct` in `MyClass` doesn't specify the value of `value()`, just like in your example. Jandex would be able to tell you that `MyClass` is annotated `@Direct`, but wouldn't be able to give you the value of `value()`. Why? Because the class file doesn't contain that information. Jandex's `AnnotationInstance` provides a set of methods to read a value of given annotation member which don't consider the default value (you will just get `null`), and a second set of methods that do consider the default value. The second set of methods accept an `IndexView` as a parameter, and that `IndexView` must contain the declaration of the annotation type. That's where the default value is read from.

It is generally good to create as much complete indexes as possible. It is practically never possible to create a fully complete index, because such index would typically have to contain [a subset of] the JDK standard library, which is not practical. As a good compromise, it is common to create indexes on a JAR granularity. JARs may carry their own indexes, typically in `META-INF/jandex.idx`.

2. As mentioned above, a Jandex `ClassInfo` contains information just from a single class file. Indirect annotations ("stereotypes") are not present in the class file, so they are not present in the `ClassInfo`. You have to find annotations on the class, lookup their declarations, find annotations on them, etc. It is common to do that recursively, but I found that the worklist algorithm is typically a lot better, often combined with a set of class names that were already processed (and those can be skipped quickly when they appear in the worklist multiple times).

Hope that helps,

LT


pá 23. 2. 2024 v 17:46 odesílatel Ivan Kulaga <kulagaivan...@gmail.com> napsal:
--
You received this message because you are subscribed to the Google Groups "SmallRye" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smallrye+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/smallrye/e6bd6fce-db6c-4716-8d71-da5539c157fdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages