I have a problem with my constructor , I can't add parameter with my constructor
my code :
import inteToM.CreateFileAction; // said not use import
import dto.m.CreateFile;
//...
// code
Class<?> dtoClass = Class.forName("dto.mToInte.CreateFile");
DtoM dto = (DtoM) JAXB.unmarshal(sr, dtoClass );
Class<?> actionClass = Class.forName("inteToM.CreateFileAction");
Constructor<?> actionConstruct = actionClass.getConstructor(); //dto.getClass()
ActionM aAction = (ActionIM) actionConstruct.newInstance(dto); // not working
ActionM bAction = (ActionIM) actionConstruct.newInstance(); // work
my class: CreateFichierAction
public class CreateFileAction {
import dto.mToInte.CreateFile;
public CreateFileAction () {
System.out.println(" constructor null");
}
public CreateFileAction (CreateFile file) {
System.out.println(" constructor not null");
this.file_c= file;
}
}
My error : java.lang.NoSuchMethodException:
So I don't understand why I can't add parameter with my constructor.
I have a prob with the method : getContructor();
If i make this :
Constructor<?> actionConstruct = actionClass.getConstructor(CreateFileAction.class);
I have this error :
java.lang.NoSuchMethodException: inteToM.CreateFileAction.<init>(inteToM.CreateFileAction)
If I make this :
Constructor<?> actionConstruct = actionClass.getConstructor(dto.m.CreateFile.class);
I have this :
java.lang.NoSuchMethodException: inteToM.CreateFileAction.<init>(dto.m.CreateFile)
Thanks for help.
.classto get theClassobjects? If you actually know which class you want to reflect in, then you don't needforName.actionClasswithCreateFile.classinstead of theforNamebusiness.actionConstruct.newInstance(dto);which is aDtoM dtobutCreateFileActiontakes aCreateFileobject.actionClass.getConstructor(dto.m.CreateFile.class);andactionClass.newInstance(dtoClass.newInstance())should work