Create custom annotation as an alias for Framework-Annotation?

is it possible to create a custom annotation alias instead

@SuppressWarnings("unused") // EventBus
public void onEvent(SomeMessage msg) { ... }

      

as

@EventBusListener
public void onEvent(SomeMessage msg) { ... }

      

This would be more self-documenting and should include SuppressWarnings of course ... Sorry if this one is trivial, but my google search hasn't helped me so far.

+3


source to share


2 answers


One approach is to write an annotation processor that transforms the AST (internal representation of the source code compiler). In each case, @EventBusListener

your annotation processor inserts the entry @SuppressWarnings("unused")

. In subsequent phases of the compiler, the annotation will be displayed.

Annotation processors usually don't change the source code, so it takes a little work. The AST is shipped to the annotation processor as an interface type, so your annotation processor will have to assign it to a specific class and perform side effects for the specific class. The Lombok project is an example of annotation processing that modifies an AST at compile time.



You can just write an annotation @SuppressWarnings("unused")

.

+1


source


You can implement it like this:



@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.CONSTRUCTOR, ElementType.LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
@SuppressWarnings("unused")
public @interface EventBusListener{
    @AliasFor(annotation = SuppressWarnings.class, attribute = "value") String[] value() default {"unused"};
}

      

+1


source







All Articles