lundi 4 décembre 2017

Dynamically load class and get Annotated classes/methods

Approach one importing class static way.

import com.this.class.AnnotationClass
    if (method.isAnnotationPresent((Class<? extends Annotation>) AnnotationClass)) {
                Annotation Class methodLevel= method.getAnnotation(AnnotationClass.class);
    }

Approach 2 : Loading class dynamically.

Class AnnotationClass= Class.forName(" com.this.class.AnnotationClass")

 if (method.isAnnotationPresent((Class<? extends Annotation>) AnnotationClass)) {
                    Annotation Class methodLevel= method.getAnnotation(AnnotationClass.class);
        }

Approach 2 throws an error AnnotationClass cannot be resolved to a type. what is the approach I need to follow when I need to load the class at runtime.





Aucun commentaire:

Enregistrer un commentaire