java.lang.NoSuchMethodException and the parameterized constructor -
i have class foo follows:
public class foo extends bar { public foo(com.example.x displaystate, com.example.y parent) { super(displaystate, parent); } }
the bar class shown below:
public abstract class bar { public bar(com.example.x displaystate, com.example.y parent) { // received params } }
additionally, there factory class following method:
public static object getinstance(class thesourceclass, class thetargetclass, class[] parameterclasses, object[] parametervalues) { class theclass = (thetargetclass == null) ? thesourceclass : thetargetclass; try { constructor classconstructor = theclass.getconstructor(parameterclasses); return classconstructor.newinstance(parametervalues); } catch (nosuchmethodexception exp) { // log }
the problem nosuchmethodexception
exception arises:
java.lang.nosuchmethodexception: com.example.foo.<init>(com.example.x, com.example.y) @ java.lang.class.getconstructor0(class.java:2715) @ java.lang.class.getconstructor(class.java:1659) @ com.example.myfactory.getinstance(myfactory.java:30)
i have debugged code , class information has correct details defined constructor; however, exception here.
does have idea missed? thanks!
the following example produce instance of foo
. example ensures constructor in bar
exists accepts x
, y
.
foo.java
public class foo extends bar { public foo(com.example.x displaystate, com.example.y parent) { super(displaystate, parent); } }
bar.java
import com.example.x; import com.example.y; public class bar { public bar(x displaystate, y parent) { // todo auto-generated constructor stub } }
x.java
package com.example; public class x { }
y.java
package com.example; public class y { }
test.java
import java.lang.reflect.constructor; import com.example.x; import com.example.y; public class test { public static void main(string[] args) { class[] classes = {x.class, y.class}; object[] values = {new x(), new y()}; object obj = getinstance(foo.class, null, classes, values); system.out.println(obj); } public static object getinstance(class thesourceclass, class thetargetclass, class[] parameterclasses, object[] parametervalues) { class theclass = (thetargetclass == null) ? thesourceclass : thetargetclass; try { constructor classconstructor = theclass .getconstructor(parameterclasses); return classconstructor.newinstance(parametervalues); } catch (exception e) { } return null; } }