La copie de code est la suivante:
package com.java.db;
import java.lang.reflect.constructor;
import java.lang.reflect.field;
import java.lang.reflect.invocationtargetException;
import java.lang.reflect.method;
import java.util.arraylist;
import java.util.arrays;
Importer java.util.iterator;
Importer java.util.list;
importation java.util.map;
Importer com.java.entity.BookShelf;
import com.java.util.getMetAdatacloumName;
classe publique getNewinstances <T> {
Classe [] cl = {};
Objet [] ob = {};
/ **
* Réglé sur vide après chaque utilisation, sinon il s'accumulera
* /
public void setNullToArrays () {
this.cl = new class [] {};
this.ob = nouvel objet [] {};
}
/ **
* Copier le tableau d'objets
*
* @param obj
* La valeur réelle requise dans la méthode de construction
* @retour
* /
objet public [] getObjectArrays (objet obj) {
ob = arrays.copyof (ob, ob.length + 1);
ob [ob.length - 1] = obj;
Retour OB;
}
/ **
* Copier le tableau des classes
*
* @param CLA
* Classe à ajouter
*
* @retour
* /
@SuppressWarnings ("non contrôlé")
classe publique [] getClassArrays (classe <?> CLA) {
if (Cla! = null) {
Cl = Arrays.Copyof (Cl, Cl.Length + 1);
cl [cl.length - 1] = CLA;
retour cl;
}autre{
retour cl;
}
}
/ **
* Obtenez une instance de la classe
*
* @Param Clazz
* Classe à instanciation
* Classe @return après instanciation
* @Throws InstantiationException
* @throws illégalaccessException
* @throws illégalargumentException
* @Throws SecurityException
* @throws invocationtargexception
* @throws noSuchMethodexception
* /
@SuppressWarnings ("non contrôlé")
Objet public getClassNewInstance (classe <?> Clazz)
lance l'instanciationxception, illégalaccessException,
IllégalargumentException, SecurityException,
InvocationTargetException, NosuchMethodexception {
Objet oj = null;
Contre = Clazz.getDeclaredConstructors (); // Obtenez le constructeur
Class [] CLA = CONS [1] .GetParAmEterTypes ();
System.out.println ("Aimisez l'utilisateur, s'il faut ajouter la taille du paramètre du constructeur de champ:" + CLA.Length);
pour (int i = 0; i <cla.length; i ++) {
String classstr = cl [i] .toString ();
// System.out.println ("Type d'argument:" + classstr);
if (classstr.equals ("class java.lang.string")) {
getClassArrays (string.class);
} else if (classstr.equals ("int")) {
getClassArrays (int.class);
} else if (classstr.equals ("double")) {
getClassArrays (double.class);
} else if (classstr.equals ("boolean")) {
getClassArrays (boolean.class);
} else if (classstr.equals ("float")) {
getClassArrays (float.class);
} else if (classstr.equals ("class java.lang.integer")) {
getClassArrays (Integer.class);
} else if (classstr.equals ("class java.lang.float")) {
getClassArrays (float.class);
}
}
oj = Clazz.newinstance (); // renvoie la construction d'instanciation spécifique de l'objet actuel dans Bdoperater
retour oj;
}
/ **
* Obtenez des classes d'instructions spécifiques via des constructeurs
* @Param Clazz
* @retour
* @throws illégalargumentException
* @Throws SecurityException
* @Throws InstantiationException
* @throws illégalaccessException
* @throws invocationtargexception
* @throws noSuchMethodexception
* /
L'objet public GetObjCon (classe <?> Clazz) lève illégalArgumentException, SecurityException, InstantiationException, illégalAccessException, InvocationTargetException, NosuchMethodexception {
Objet obj = null;
obj = this.getClassNewInstance (Clazz);
retour obj;
}
/ **
* Obtenez une instance de l'objet
* @Param Clazz
* @retour
* @Throws InstantiationException
* @throws illégalaccessException
* /
Objet public Getnewinstance (classe Clazz) lève l'instantiationxception, illégalaccessException {
Objet obj = null;
obj = Clazz.newinstance ();
retour obj;
}
/ **
* Obtenez toutes les propriétés de la classe en fonction de la réflexion
* Classe Clazz @param qui doit être récupérée
* Collection d'attribut @return
* @Throws SecurityException
* @throws illégalargumentException
* @Throws InstantiationException
* @throws illégalaccessException
* @throws invocationtargexception
* @throws noSuchMethodexception
* /
Le domaine public [] GetFielsDarray (classe <objet> Clazz) lève la sécurité, illégalargumentException, InstantiationException, illégalaccessException, invocationTargetException, NosuchMethodexception {
Champ [] champs = null;
Fields = Clazz.getDeclaredFields ();
champs de retour;
}
/ **
* Obtenez des attributs de format de setter en fonction de la chaîne
* Propriété @param str qui doit être formatée
* @retour
* /
public String getsetterStr (String str) {
Chaîne info = null;
String strvalue = str.substring (0,1) .toupperase ();
info = "set" + strvalue + str.substring (1, str.length ());
retour des informations;
}
/ **
* Restaurer Setxx à xx
* @param str
* @retour
* /
public String setSetstr (String str) {
Chaîne info = null;
String strvalue = str.substring (3, str.length ());
String Lower = StrValue.SubString (0) .tolowerCase (). Substring (0,1);
info = inférieur + str.substring (4, str.length ());
retour des informations;
}
/ **
* Obtenez la méthode dans la classe
* @Param Clazz requis classe pour obtenir la méthode
* @retour
* /
Méthode publique [] getMethodsArray (class Clazz) {
Méthode [] Methods = Clazz.getMethods ();
méthodes de retour;
}
/ **
* Instancier le constructeur selon la liste <map>
* @param listmap
* @Param Clazz
* @Param TableName Database Name
* @retour
* @throws noSuchMethodexception
* @throws invocationtargexception
* @Throws SecurityException
* @throws illégalargumentException
* @throws illégalaccessException
* @Throws InstantiationException
* /
@SuppressWarnings ({"Unchecked"})
Public List <Bject> getListByMap (liste <map <string, objet >> listmap, class Clazz, String TableName) lève InstantiationException, illégalaccessException, illégalargumentException, SecurityException, InvocationTargetException, NosuchMethodexception {
List <object> listLast = new ArrayList <Bject> ();
List <string> metalist = getMetAdatacloumName.getCloumNameList (tableName);
Iterator <map <string, objet >> it = listmap.iterator ();
while (it.hasnext ()) {
Map <string, objet> map = it.next ();
Iterator <string> iitt = metalist.iterator ();
while (iitt.hasnext ()) {
Chaîne info = iitt.next ();
this.getObjectArrays (map.get (info));
}
System.out.println ("Call Reflection:" + this.cl.length + "" + this.ob.length);
Objet tobj = this.getClassNewInstance (Clazz) .getClass (). GetConstructor (this.cl) .newinstance (this.ob);
listLast.Add (TOBJ);
this.setNullToArrays ();
}
return listLast;
}
public static void main (String [] args) {
GetNewInstances ge = new GetNewInstances ();
System.out.println (ge.getSetterstr ("namespace") == "setNamespace");
System.out.println ("1A" == "1A");
System.out.println (GE.SetSettr ("setNamespace"));
}
}