jeudi 9 août 2018

Get all methods with a particular annotation in a package

I know we can get all methods with a particular annotation inside a class. But I want all methods inside a package with that annotation. My use case : I want to annotate and identify few set of methods and ask my user to choose one of them add in a flow to execute.

@Retention(RetentionPolicy.RUNTIME)
@Target({ METHOD })
public @interface Step {
    String Type();// default "Trigger";
    String SubType();
    String [] tags();
}

public interface APITrigger {

    @Step(Type = "TRIGGER", SubType = "GET_CLASS_INSTANCE", tags = { "trigger", "build instance", "class object" })
    public Object getClassInstance(@NonNull final String packageNclassName)
            throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, NoSuchFieldException;

    @Step(Type = "TRIGGER", SubType = "INVOKE_API_N_RETURN", tags = { "trigger", "hit api", "trigger api and return" })
    public Map<Class<?>, Object> invokeOveloadedAPINReturn(@NonNull final Object classInstance,
            @NonNull final String methodName);
}
public interface Comparator {

    @Step(Type = "COMPARATOR", SubType = "GENERIC_COMPARATOR", tags = { "compare", "expected", "observed", "any type", "generic comparator" })
    public <T> ComparisonResult Compare(T Expected, T Observed);

}

I want list of methods [getclassinstance, invokeOveloadedAPINReturn, Compare] as those three are annotated with Step. Can we do this with reflection.





Aucun commentaire:

Enregistrer un commentaire