dimanche 14 août 2016

Reading annotation data

If I have given a class a custom annotation @MyAnnotation1, as well as annotations on its private member variables, all of type @MyAnnotation2, how do I properly declare a method that accepts such a class (could be any class) and then extracts the names and attributes of anything that has an annotation?

@MyAnnotation1(coolness=45)
public class MyObject {

    @MyAnnotation2(someAttribute="thing", anotherAttribute=true)
    private String stringMember;

    @DatabaseField(isVorpal=true)
    private int intMember;

    private int noAnnotationsHere; //has no annotation, should be ignored

   ...

I want to be able to pass MyObject into a function somehow. Do I do it like this?

someFunc(Class<? extends Annotation1>) myObject.getClass());

and then in void someFunc how do I get all the various annotation fields and values? Is there a way to tell that the object is of annotation @MyAnnotation1 and that it has coolness level 45? Is there a way to tell that it has two member variables with @Annotation2 and all their values as well?





Aucun commentaire:

Enregistrer un commentaire