1

I have a class with several public functions. Each function has an annotation that tags it so that the main application can find all functions that have that annotation. At startup, the application searches for all the functions, then adds them to a map for later use. This approach is used because the application has many of these classes, and those classes often have dozens of those functions resulting in about 160 functions.

An undesirable side effect of this design is that the functions (aka fields) are all flagged by the editor as "never used". The enclosing class is also marked as "never used". That is true, the functions are never explicitly used, however, certain events will cause the function to be pulled from the map and then executed, so they are indirectly executed.

Is there a way to have the annotation signal to the compiler that the function is used, thus removing the warning for both the field and the parent class?

Here's a visual of what I'm trying to describe:

The annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FindMe{
    String value() default "";
}

The class with the function:

public class MyClass {

   @FindMe("abc")
   public static MyFunctionalInterface someFunction = (task) -> {
      ...
   }
}

In the above example, MyClass, as well as someFunction, are seen as unused.

Is there a way to make the compiler think they are actually used?

  • The compiler? I'm not aware of such a warning by Java compilers. Do you mean the IDE? If so, you are looking for annotations supported by your IDE. This question might help and looks like an exact dupe: [Telling IntelliJ IDEA which methods not to identify as unused](https://stackoverflow.com/q/5283972/112968) – knittl Dec 04 '22 at 18:01

0 Answers0