简体   繁体   中英

Filter classes that are annotated with a specifi annotation

I'm using Java Agents to manipulate bytecode and I would like to generate methods into classes that are annotated with a specific annotation. How can I do?

Something like this:

@Foo public class Bar {
    // inject methods here
}

If it is possible, filter by full qualified annotation name, like com.example.Foo instead of Foo.class .

I found a suitable solution using ElementMatchers.isAnnotatedWith method and passing annotation class as parameter, but I'm still looking for a way to filter by the full qualified annotation name as pure String .

new AgentBuilder.Default()
            .type(isAnnotatedWith(ToString.class))
            .transform(new AgentBuilder.Transformer() {
                @Override public DynamicType.Builder<?> transform(
                        DynamicType.Builder<?> builder,
                        TypeDescription typeDescription,
                        ClassLoader classLoader,
                        JavaModule javaModule
                ) {
                    // do some logic here
                }
            }).installOn(instrumentation);

您可以使用ElementMatchers来实现hasAnnotation(annotationType(named(...)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM