How to handle attributes in an interceptor interceptor

I have an annotation:

@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Example {
}

And the interceptor class to handle it:

@Interceptor
@Example
public class ExampleInterceptor implements Serializable {
...
}

I would like to add parameter text:

public @interface Example {
    String text();
}

But I don't know how to handle a parameter in an interceptor class. How to change class annotation?

@Interceptor
@Example(text=???????)
public class ExampleInterceptor implements Serializable {
...
}

If I write @Example(text="my text"), the interceptor is called only when the method / class is annotated with @Example(text="my text"). But I want the interceptor to be called independently by the value of the - parameter @Example(text="other text").

And how to get the parameter value? Should I use reflection or is there a better way?

+5
source share
2 answers

An interceptor is called for each attribute value when the annotation is used @Nonbinding.

Annotation:

public @interface Example {
    @Nonbinding String text() default "";
}

interceptor:

@Interceptor
@Example
public class ExampleInterceptor implements Serializable {
    ...
}
+11
source

-, , value ( ), = > = , :

@Example("my text") //instead of @Example(test="My text")
public class ...

, getAnnotation Class, Method, Field Constructor, . , .

Example example = getClass().getAnnotation(); //if you are inside the interceptor you can just use getClass(), but you have to get the class from somewhere
example.value(); //or example.text() if you want your annotation parameter to be named text.
0

All Articles