jeudi 4 juin 2020

Method annotation to retrieve parameters

I'm trying to use a method annotation to retrieve all parameters of a method. Something similar to ArgumentCaptor from Mockito. But I cannot find any example online. Is that possible somehow?. What I want is something like:

The annotated method:

@ValidParameters
public void execute(MyObject myObject, String stuff) {
  // some cool code
}

The annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ValidParameters {
}

The code I wanted to run:

public static void main(String[] args) {
    private void validate() throws Exception {
        Class<?> clazz = MyClass.getClass();
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(ValidParameters.class)) {
                // Retrieve the parameters using the annotation
            }
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire