當前位置:才華齋>計算機>java語言>

java使用反射技術示例

java語言 閱讀(4.3K)

複製程式碼 程式碼如下:

java使用反射技術示例

package ;

import tructor;

import d;

import cationTargetException;

import od;

import yList;

import ys;

import ator;

import ;

import ;

import Shelf;

import etaDataCloumName;

public class GetNewInstances{

Class[] cl = {};

Object[] ob = {};

/**

* 每次用完之後設為空 不然會累加

*/

public void setNullToArrays(){

= new Class[]{};

= new Object[]{};

}

/**

* copy Object陣列

*

* @param obj

* 構造方法裡需要的實際值

* @return

*/

public Object[] getObjectArrays(Object obj) {

ob = Of(ob,th + 1);

ob[th - 1] = obj;

return ob;

}

/**

* copy Class 陣列

*

* @param cla

* 要新增的class

*

* @return

*/

@SuppressWarnings("unchecked")

public Class[] getClassArrays(Class cla) {

if (cla != null) {

cl = Of(cl,th + 1);

cl[th - 1] = cla;

return cl;

}else{

return cl;

}

}

/**

* 得到類的例項

*

* @param clazz

* 要例項化的類

* @return 例項化之後的'類

* @throws InstantiationException

* @throws IllegalAccessException

* @throws IllegalArgumentException

* @throws SecurityException

* @throws InvocationTargetException

* @throws NoSuchMethodException

*/

@SuppressWarnings("unchecked")

public Object getClassNewInstance(Class clazz)

throws InstantiationException, IllegalAccessException,

IllegalArgumentException, SecurityException,

InvocationTargetException, NoSuchMethodException {

Object oj = null;

Constructor[] cons = eclaredConstructors();// 得到建構函式

Class[] cla = cons[1]arameterTypes();

tln("提示使用者是否需要新增欄位 建構函式引數的大小:"+th);

for (int i = 0; i < th; i++) {

String classStr = cla[i]ring();

// tln("引數的型別:"+classStr);

if (ls("class ng")) {

getClassArrays(s);

} else if (ls("int")) {

getClassArrays(s);

} else if (ls("double")) {

getClassArrays(s);

} else if (ls("boolean")) {

getClassArrays(s);

} else if (ls("float")) {

getClassArrays(s);

} else if (ls("class ger")) {

getClassArrays(s);

}else if(ls("class t")){

getClassArrays(s);

}

}

oj = nstance();//返回當前物件 具體的例項化構造在BDOperater

return oj;

}

/**

* 通過建構函式得到具體的例項類

* @param clazz

* @return

* @throws IllegalArgumentException

* @throws SecurityException

* @throws InstantiationException

* @throws IllegalAccessException

* @throws InvocationTargetException

* @throws NoSuchMethodException

*/

public Object getObjCon(Class clazz) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{

Object obj=null;

obj = lassNewInstance(clazz);

return obj;

}

/**

* 得到物件的例項

* @param clazz

* @return

* @throws InstantiationException

* @throws IllegalAccessException

*/

public Object getNewinstance(Class clazz) throws InstantiationException, IllegalAccessException{

Object obj = null;

obj = nstance();

return obj;

}

/**

* 根據反射得到類中的所有屬性

* @param clazz 需要被獲取屬性的類

* @return 屬性集合

* @throws SecurityException

* @throws IllegalArgumentException

* @throws InstantiationException

* @throws IllegalAccessException

* @throws InvocationTargetException

* @throws NoSuchMethodException

*/

public Field[] getFielsdArray(Classclazz) throws SecurityException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{

Field[] fields = null;

fields = eclaredFields();

return fields;

}

/**

* 根據字串得到setter格式的屬性

* @param str 需要格式化的屬性

* @return

*/

public String getSetterStr(String str){

String info = null;

String strValue = tring(0,1)perCase();

info = "set"+strValue+tring(1,th());

return info;

}

/**

* 把setXX還原為XX

* @param str

* @return

*/

public String setSetStr(String str){

String info = null;

String strValue = tring(3,th());

String lower = tring(0)werCase()tring(0,1);

info = lower+tring(4,th());

return info;

}

/**

* 得到類中的方法

* @param clazz 需要的得到方法的類

* @return

*/

public Method[] getMethodsArray(Class clazz){

Method[] methods = ethods();

return methods;

}

/**

* 根據list例項化建構函式

* @param listMap

* @param clazz

* @param tableName 資料庫名稱

* @return

* @throws NoSuchMethodException

* @throws InvocationTargetException

* @throws SecurityException

* @throws IllegalArgumentException

* @throws IllegalAccessException

* @throws InstantiationException

*/

@SuppressWarnings({ "unchecked" })

public ListgetListByMap(List<Map> listMap,Class clazz,String tableName) throws InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException, InvocationTargetException, NoSuchMethodException{

ListlistLast = new ArrayList();

ListmetaList = loumNameList(tableName);

Iterator<Map> it = ator();

while(ext()){

Mapmap = ();

Iteratoriitt = ator();

while(ext()){

String info = ();

bjectArrays((info));

}

tln("呼叫反射:"+th+" "+th);

Object Tobj = lassNewInstance(clazz)lass()onstructor()nstance();

(Tobj);

ullToArrays();

}

return listLast;

}

public static void main(String[] args) {

GetNewInstances ge = new GetNewInstances();

tln(etterStr("nameSpace")=="setNameSpace");

tln("1a"=="1a");

tln(etStr("setNameSpace"));

}

}