コードコピーは次のとおりです。
パッケージcom.java.db;
java.lang.reflt.constructorをインポートします。
java.lang.reflect.fieldをインポートします。
Import java.lang.Reflect.InvocationTargetException;
java.lang.reflt.methodをインポートします。
java.util.arraylistをインポートします。
java.util.arraysをインポートします。
java.util.iteratorをインポートします。
java.util.listをインポートします。
java.util.mapをインポートします。
com.java.entity.bookshelfをインポートします。
com.java.util.getmetadatacloumnameをインポートします。
パブリッククラスgetNewInstances <T> {
class [] cl = {};
object [] ob = {};
/**
*使用するたびに空に設定すると、そうでなければ蓄積します
*/
public void setnulltoarrays(){
this.cl = new class [] {};
this.ob = new Object [] {};
}
/**
*オブジェクト配列をコピーします
*
* @param obj
*建設方法で必要な実際の値
* @戻る
*/
public Object [] getObjectArrays(オブジェクトobj){
ob = arrays.copyof(ob、ob.length + 1);
ob [ob.length -1] = obj;
obを返します。
}
/**
*クラス配列をコピーします
*
* @param cla
*追加するクラス
*
* @戻る
*/
@suppresswarnings( "unchecked")
public class [] getClassArrays(class <?> cla){
if(cla!= null){
cl = arrays.copyof(cl、cl.length + 1);
cl [cl.length -1] = cla;
clを返します。
}それ以外{
clを返します。
}
}
/**
*クラスのインスタンスを取得します
*
* @Param Clazz
*インスタンス化されるクラス
*インスタンス化後の@returnクラス
* @Throws InstantiationException
* @Throws Illegalaccessexception
* @Throws IllegalargumentException
* @Throws SecurityException
* @Throws InvocationTargetException
* @throws nosuchmethodexception
*/
@suppresswarnings( "unchecked")
パブリックオブジェクトgetClassNewInstance(class <?> clazz)
InstantiationException、Illegalaccessexception、
IllegalargumentException、securityException、
InvocationTargetException、nosuchmethodexception {
オブジェクトoj = null;
cons = clazz.getDeclaredConstructors(); //コンストラクターを取得します
class [] cla = cons [1] .getParametertypes();
System.out.println( "フィールドコンストラクターパラメーターのサイズを追加するかどうかをユーザーにチップ:"+cla.length);
for(int i = 0; i <cla.length; i ++){
string classstr = cla [i] .toString();
// system.out.println( "引数タイプ:"+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(); // bdoperaterの現在のオブジェクトの特定のインスタンス化コンストラクトを返します
OJを返します。
}
/**
*コンストラクターを介して特定のインスタンスクラスを取得します
* @Param Clazz
* @戻る
* @Throws IllegalargumentException
* @Throws SecurityException
* @Throws InstantiationException
* @Throws Illegalaccessexception
* @Throws InvocationTargetException
* @throws nosuchmethodexception
*/
public Object getobjcon(class <?> clazz)は、IllegalargumentException、seculityexception、instantiationexception、Illegalaccessexception、InvocationTargetException、nosuchmethodexceptionをスローします{
オブジェクトobj = null;
obj = this.getClassNewInstance(Clazz);
OBJを返します。
}
/**
*オブジェクトのインスタンスを取得します
* @Param Clazz
* @戻る
* @Throws InstantiationException
* @Throws Illegalaccessexception
*/
パブリックオブジェクトGetNewInstance(クラスClazz)は、InstantiationException、Illegalaccessexceptionをスローします{
オブジェクトobj = null;
obj = clazz.newinstance();
OBJを返します。
}
/**
*反射に基づいてクラス内のすべてのプロパティを取得する
* @Param Clazzクラスを取得する必要がある
* @return属性コレクション
* @Throws SecurityException
* @Throws IllegalargumentException
* @Throws InstantiationException
* @Throws Illegalaccessexception
* @Throws InvocationTargetException
* @throws nosuchmethodexception
*/
パブリックフィールド[] GetFielSDarray(class <object> clazz)は、SecurityException、IllegalargumentException、InstantiationException、Illegalaccessexception、InvocationTargetException、nosuchmethodexceptionをスローします{
field [] fields = null;
fields = clazz.getDeclaredFields();
返品フィールド。
}
/**
*文字列に従ってセッター形式の属性を取得します
*フォーマットする必要がある@param strプロパティ
* @戻る
*/
public string getsetterstr(string str){
文字列情報= null;
string strvalue = str.substring(0,1).touppercase();
info = "set"+strvalue+str.substring(1、str.length());
情報を返す;
}
/**
* setxxをxxに復元します
* @param str
* @戻る
*/
public string setsetStr(string str){
文字列情報= null;
string strvalue = str.substring(3、str.length());
string lower = strvalue.substring(0).tolowercase()。サブストリング(0,1);
info = lower+str.substring(4、str.length());
情報を返す;
}
/**
*クラスでメソッドを取得します
* @Param Clazzは、メソッドを取得するためにクラスが必要でした
* @戻る
*/
パブリック方法[] getMethodsArray(クラスClazz){
方法[] methods = clazz.getMethods();
返品方法。
}
/**
* list <map>に従ってコンストラクターをインスタンス化します
* @param listmap
* @Param Clazz
* @param TableNameデータベース名
* @戻る
* @throws nosuchmethodexception
* @Throws InvocationTargetException
* @Throws SecurityException
* @Throws IllegalargumentException
* @Throws Illegalaccessexception
* @Throws InstantiationException
*/
@suppresswarnings({"unchecked"})
パブリックリスト<Object> getListByMap(List <Map <String、Object >> ListMap、class Clazz、String TableName)InstantiationException、Illegalaccessexception、Illegalargumentexception、SecupueException、InvocationTargetException、nosuchmethodexception {
List <Object> listLast = new ArrayList <Object>();
List <String> Metalist = getMetaDatacloumName.getCloumNameList(TableName);
iterator <map <string、object >> it = listmap.iterator();
while(it.hasnext()){
map <string、object> map = it.next();
iterator <string> iitt = metalist.iterator();
while(iitt.hasnext()){
文字列info = iitt.next();
this.getObjectArrays(map.get(info));
}
System.out.println( "コールリフレクション:"+this.cl.length+""+this.ob.length);
オブジェクト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.setsetstr( "setNamesPace"));
}
}