Wednesday 15 May 2013

java - How to use getConstructor with Generic class? -



java - How to use getConstructor with Generic class? -

this first brush reflection , generics in java please pardon ignorance. trying instantiate class using reflection , generics getting error in toy program. goal instantiate constructor of inst class.

code:

/* *this builder class build instance */ bundle generics.expclasst; import java.lang.reflect.constructor; import java.lang.reflect.invocationtargetexception; public class builder { public static<e> e createinst(class<e> c) throws instantiationexception, illegalaccessexception { //class<?>[] type = new class<?>[1]; //type[0] = inst.class; seek { constructor<e> ctor = c.getconstructor(c); //c.getconstructor(type); homecoming (ctor.newinstance("testing")); } grab (nosuchmethodexception | securityexception | illegalargumentexception | invocationtargetexception e) { e.printstacktrace(); homecoming null; } } } /* * class need instance */ bundle generics.expclasst; public class inst { private string var; public inst(string s) { this.var = s; } public string getvar() { homecoming var; } public static void main(string args[]){ inst instobj; seek { instobj = builder.createinst(inst.class); system.out.println(instobj.getvar()); } grab (instantiationexception | illegalaccessexception e) { //e.printstacktrace(); } } }

exception:

java.lang.nosuchmethodexception: generics.expclasst.inst.<init>(generics.expclasst.inst) @ java.lang.class.getconstructor0(class.java:2800) @ java.lang.class.getconstructor(class.java:1708) @ generics.expclasst.builder.createinst(builder.java:15) @ generics.expclasst.inst.main(inst.java:19) exception in thread "main" java.lang.nullpointerexception @ generics.expclasst.inst.main(inst.java:20)

thank in advance time , assistance!!

constructor<e> ctor = c.getconstructor(c); should constructor<e> ctor = c.getconstructor(string.class);

from javadocs

returns constructor object reflects specified public constructor of class represented class object. parametertypes parameter array of class objects identify constructor's formal parameter types, in declared order. if class object represents inner class declared in non-static context, formal parameter types include explicit enclosing instance first parameter. constructor reflect public constructor of class represented class object formal parameter types match specified parametertypes.

parameters: parametertypes - parameter array

this, basically, means, getconstructor(class...) expects pass class types have been defined classes constructor, in case public inst(string s)

builder import java.lang.reflect.constructor; import java.lang.reflect.invocationtargetexception; public class builder { public static <e> e createinst(class<e> c) throws instantiationexception, illegalaccessexception { //class<?>[] type = new class<?>[1]; //type[0] = inst.class; seek { constructor<e> ctor = c.getconstructor(string.class); //c.getconstructor(type); homecoming (ctor.newinstance("testing")); } grab (nosuchmethodexception | securityexception | illegalargumentexception | invocationtargetexception e) { e.printstacktrace(); homecoming null; } } } inst public class inst { private string var; public inst(string s) { this.var = s; } public string getvar() { homecoming var; } } main public class test { public static void main(string[] args) { inst instobj; seek { instobj = builder.createinst(inst.class); system.out.println(instobj.getvar()); } grab (instantiationexception | illegalaccessexception e) { //e.printstacktrace(); } } }

you might have read through code conventions java tm programming language, create easier people read code , read others

java generics reflection

No comments:

Post a Comment