Dúvida com Reflection e Annonation!

3 respostas
rodrigo_corinthians
public class PersonBean {
	private Integer id;
	private String name;
	private List<CompanyPersonBean> companyPersonList;

 // Get' and Set's
}

Galera bom dia.

Via reflection eu gostaria de saber a classe(CompanyPersonBean) que foi anotada no List(companyPersonList) isto é possível? Eu sei que diversos frameworks mvc(como o Struts 2) fazem isso mas só não sei como. :wink:

3 Respostas

T
import java.lang.reflect.*;
import java.util.*;

   class CompanyPersonBean {
   }

   class PersonBean { 
       private Integer id; 
       private String name; 
       private List&lt;CompanyPersonBean&gt; companyPersonList; 
     
     // Get' and Set's 
   }

class PersonBeanTest {
    public static void main(String[] args) throws NoSuchFieldException {
        PersonBean pb = new PersonBean();
        Field f = pb.getClass().getDeclaredField ("companyPersonList");
        Type t = f.getGenericType();
        ParameterizedType pt = (ParameterizedType) t;
        Type[] ata = pt.getActualTypeArguments();
        Type rawType = pt.getRawType();
        System.out.println ("rawType=" + rawType);
        System.out.println ("args=" + Arrays.asList (ata));
    }
}

Deve imprimir:

rawType=interface java.util.List
args=[class CompanyPersonBean]

rodrigo_corinthians

Legal cara funcionou perfeitamente, obrigado :slight_smile:

Marky.Vasconcelos

Ps: isto é generics
Annotations é @Annotation
^^

Criado 5 de setembro de 2007
Ultima resposta 5 de set. de 2007
Respostas 3
Participantes 3