java - How to use getConstructor with Generic class? -


This is my first brush with reflection and generic in Java, so please forgive my ignorance. I am trying to instantiate a classroom using reflection and generic, but my toy program gets an error, the goal is to instantiate with the inst class constructor.

code:

  / * * This is a builder class to make an example / package generic.xp class; Import java.lang.reflect.Constructor; Import java.lang.reflect.InvocationTargetException; Public category builder {public stable & lt; E & gt; E throws InstantiationException (class  E & gt; c), IllegalAccessException {// class & lt ;? & Gt; [] Type = New class & lt ;? & Gt; [1]; // type [0] = inst.class; Try {manufacturer & lt; E & gt; Ctor = c.getConstructor (c); //c.getConstructor(type); Return (ctor.newInstance ("test")); } Hold (NoSuchMethodException | Security Exception | InvalidAuthor Exception | InvocationTargetException e) {e.printStackTrace (); Return tap; }}} / * * This is the class for which we have an instance * / package generics. XP Cluster; Public class inst {private string variable; Public inst (strings) {this.var = s; } Public string getVar () {return var; } Public Fixed Zero Main (String Array []) {inst instbb; Try {instObj = builder.createInst (inst.class); Println (instObj.getVar ()); } Hold (InstantiationException | IllegalAccessException e) {//e.printStackTrace (); }}}   

Exception:

  java.lang.NoSuchMethodException: generics.expClassT.inst. & Lt; Init & gt; (generics.expClassT.inst) at generics.expClassT.inst on generics.expClassT.builder.createInst (builder.java.15) java.lang.Class.getConstructor (class.java.708) on java.lang.Class. Exception in generic .exclassT.inst.main (inst.java20) on "main" java.lang.NullPointerException in getConstructor0 (class.Java 2800) .main (inst.java19) threads   < P> Thanks in advance for your time and support!   

constructor & lt; E & gt; Ctor = c.getConstructor (c); should be constructor & lt; E & gt; Ctor = c.getConstructor (String.class);

An array that identifies the manufacturer's formal parameter type in the declared order If this class object represents the declared internal range in a non-static context, then the form In the Aik parameter types, there is a clear attached frequency as the first parameter.

The Constructor reflector is the public creator of the class represented by the class object whose formal parameters are specified by parameter parameter type. Parameter: Parameter Type - Parameter Array

This, basically, means that < From the code> getConstructor (class ...) you expect to pass class types which are defined by the class constructor, in your case public insta (string) < / P>

Builder
  Import java.lang.reflect.Constructor; Import java.lang.reflect.InvocationTargetException; Public Class Builder {Public Stabilized & lt; E & gt; E throws InstantiationException (class  E & gt; c), IllegalAccessException {// class & lt ;? & Gt; [] Type = New class & lt ;? & Gt; [1]; // type [0] = inst.class; Try {manufacturer & lt; E & gt; Ctor = c.getConstructor (String.class); //c.getConstructor(type); Return (ctor.newInstance ("test")); } Hold (NoSuchMethodException | Security Exception | InvalidAuthor Exception | InvocationTargetException e) {e.printStackTrace (); Return tap; }}}   

inst
  public class inst {private string var; Public institute (strings) {this.var = s; } Public string getVar () {return var; }}   

main
  public class test {public static zero main (string [] args) {Inst instAbj; Try {instObj = Builder. (Inst.class); Println (instObj.getVar ()); } Hold (InstantiationException | IllegalAccessException e) {//e.printStackTrace ();           

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -