Bonjours,
J'ai un problème avec l'API introspection: je cherche à récupérer l'instance d'un objet (et je n'y arrive pas).
Je m'explique:
J'ai a l'instance d'une classe A:
A a=(A)...
Cet objet tient (entre autre) un objet b, instance de B en attribut privé
Je cherche à récupérer b par introspection pour le passer en argument d'une méthode (récupérée elle aussi par introspection):
Je récupère IllegalArgumentException car obj ne contient pas b.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Method methode; //deja initialisé Object obj2; //idem List fieldsList = Arrays.asList(a.getClass().getDeclaredFields()); Iterator itFieldsList = fieldsList.iterator(); while (itFieldsList.hasNext()) {//parcours de la liste des attributs Field field = (Field) itFieldsList.next(); Object[] obj = {field}; try { methode.invoque(obj2,obj); } catch (IllegalArgumentException e2) { e2.printStackTrace(); } }
Comment m'en sortir ? Dois-je récupérer b par ses accesseurs ?
Eric LLUSCA
Partager