lundi 8 avril 2019

Can I get any value of annotation, without annotation`s name?

I would like to write a method that returned the value of the annotation method.

I tried to use this variants, nothing succeeded

Params:

  • clazz - Сlass that has annotations
  • annotationClazz - My Annotation.class
  • parametersName - method`s name

This is my code:

public static Object getAnnotationValue(Class clazz, Class annotationClazz, String parametersName) {

    Annotation an = clazz.getAnnotation(annotationClazz);
    if (an.equals(null)) {
        throw new CoreError("Класс " + clazz + " не содержит аннотацию " + annotationClazz);
    }
    PageName pn = (PageName) an;

    try {
        //its working!
        System.out.println(pn.value());

        //not working :(
        System.out.println(an.getClass().getMethod(parametersName).getDefaultValue()); //not working :(
        System.out.println(an.annotationType().getDeclaredMethod(parametersName, annotationClazz).getDefaultValue());
        System.out.println(pn.getClass().getMethod(parametersName).getDefaultValue());
        System.out.println(pn.annotationType().getDeclaredMethod(parametersName, annotationClazz).getDefaultValue());
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

Is it even possible?





Aucun commentaire:

Enregistrer un commentaire