mardi 31 mai 2016

No annotations present when reflecting a method using class.getDeclaredMethod

I'm writing some unit tests using reflection, and I'm having troubles to reflect a method preserving its annotations.

I declared this interface:

private interface Provider {
    void mock(@Email String email);
}

And I'm trying to reflect this method, as follows:

Class stringClass = String.class;
Method method = Provider.class.getDeclaredMethod("mock", String.class);
AnnotatedType annotatedType = method.getAnnotatedParameterTypes()[0];
Annotation annotation = annotatedType.getAnnotation(Annotation.class);

I would expect that annotation variable holds an instance of @Email annotation, but instead, it's a null reference.

So, how can I retrieve the annotations for an specific param when reflecting a method?

Thanks!





Aucun commentaire:

Enregistrer un commentaire