A noter qu'il est nécessaire pour les classes interne de passer une instance de la classe englobante comme le montre l'exemple suivant :
package com.ldu.divers;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test {
public class UnDTO2 {
private String lastName = "";
private String firstName = "";
public UnDTO2(String lastName,String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Class c = UnDTO2.class;
Constructor [] cos = c.getDeclaredConstructors();
for (Constructor constructor : cos) {
System.err.println(constructor.toString());
}
try {
Constructor constr = c.getConstructor(Test.class, String.class,String.class);
UnDTO2 dto = constr.newInstance(new Test(), "monNom","Enwoo");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire