How do I pass the "T" argument to as a method parameter using Java generics? -
I am using jbis 7.1.3 with Java 6. I'm having trouble finding out what I should do to make my method work properly in the code below "???" Whatever I want to do is to institute an example of my "T" class, every time I start the classroom, I am using Generic Constructor. Below is the method ...
Public Class Mayclass & lt; T-abstrum model spreads & gt; {Public void myMethod () {... Last Hashmap & lt; String, string & gt; Data = new hashmap & lt; String, string & gt; (); Last T object = Deviation (???, data); } Private T Received Instances (Last Class & lt; T & gt; Class 1, Final Hashmap & lt; String, String & gt; Data) {T Rate Object = Null; Try {creator CT = class 1. getDeclaredConstructor (HashMap.class); Object [] argList = new object [1]; ArgList [0] = data; RetObj = (t) ct.newInstance (argList); } Hold (exception e) {} // retraction retObj; } // getInstance
Here is my Constructor for the abstract model class ...
Public abstract class AbstractDIDOModel {Protected Hashmap & lt; String, string & gt; Information; Abstract Modell (last hashmap & lt; string, string & gt; data) {this.data = data; }
If your class has type parameter set, Because the type of information is available at runtime, you can capture it and store it in a member field. You can then use that area as a class within your generic creation method.
Public abstract class AbstractDIDOModel & lt; T & gt; {Private class & lt ;? & Gt; PersistentClass = (Class & lt;? & Gt;) ((Parameted Type) this.getClass (). GetGenericSuperclass ()). GetActualTypeArguments () [0]; Private T Received Instances (Last Hashmap & lt; String, String & gt; Data) {T retObj = null; Try {creator CT = persistentClass.getDeclaredConstructor (HashMap.class); Object [] argList = new object [1]; ArgList [0] = data; RetObj = (t) ct.newInstance (argList); } Hold (exception e) {} // retraction retObj; }
Finally, declare your concrete sections like this:
Public Class Mayclass & lt; T-abstrum model spreads & gt; {....}
Comments
Post a Comment