- 相關(guān)推薦
java使用反射技術(shù)示例
復(fù)制代碼 代碼如下:
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;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.java.entity.BookShelf;
import com.java.util.GetMetaDataCloumName;
public class GetNewInstances
Class[] cl = {};
Object[] ob = {};
/**
* 每次用完之后設(shè)為空 不然會(huì)累加
*/
public void setNullToArrays(){
this.cl = new Class[]{};
this.ob = new Object[]{};
}
/**
* copy Object數(shù)組
*
* @param obj
* 構(gòu)造方法里需要的實(shí)際值
* @return
*/
public Object[] getObjectArrays(Object obj) {
ob = Arrays.copyOf(ob,ob.length + 1);
ob[ob.length - 1] = obj;
return ob;
}
/**
* copy Class 數(shù)組
*
* @param cla
* 要添加的class
*
* @return
*/
@SuppressWarnings("unchecked")
public Class[] getClassArrays(Class
if (cla != null) {
cl = Arrays.copyOf(cl,cl.length + 1);
cl[cl.length - 1] = cla;
return cl;
}else{
return cl;
}
}
/**
* 得到類(lèi)的實(shí)例
*
* @param clazz
* 要實(shí)例化的類(lèi)
* @return 實(shí)例化之后的類(lèi)
* @throws InstantiationException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
@SuppressWarnings("unchecked")
public Object getClassNewInstance(Class
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, SecurityException,
InvocationTargetException, NoSuchMethodException {
Object oj = null;
Constructor[] cons = clazz.getDeclaredConstructors();// 得到構(gòu)造函數(shù)
Class[] cla = cons[1].getParameterTypes();
System.out.println("提示用戶是否需要添加字段 構(gòu)造函數(shù)參數(shù)的大小:"+cla.length);
for (int i = 0; i < cla.length; i++) {
String classStr = cla[i].toString();
// System.out.println("參數(shù)的類(lèi)型:"+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();//返回當(dāng)前對(duì)象 具體的實(shí)例化構(gòu)造在BDOperater
return oj;
}
/**
* 通過(guò)構(gòu)造函數(shù)得到具體的實(shí)例類(lèi)
* @param clazz
* @return
* @throws IllegalArgumentException
* @throws SecurityException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public Object getObjCon(Class
Object obj=null;
obj = this.getClassNewInstance(clazz);
return obj;
}
/**
* 得到對(duì)象的實(shí)例
* @param clazz
* @return
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Object getNewinstance(Class clazz) throws InstantiationException, IllegalAccessException{
Object obj = null;
obj = clazz.newInstance();
return obj;
}
/**
* 根據(jù)反射得到類(lèi)中的所有屬性
* @param clazz 需要被獲取屬性的類(lèi)
* @return 屬性集合
* @throws SecurityException
* @throws IllegalArgumentException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws InvocationTargetException
* @throws NoSuchMethodException
*/
public Field[] getFielsdArray(Class
Field[] fields = null;
fields = clazz.getDeclaredFields();
return fields;
}
/**
* 根據(jù)字符串得到setter格式的屬性
* @param str 需要格式化的屬性
* @return
*/
public String getSetterStr(String str){
String info = null;
String strValue = str.substring(0,1).toUpperCase();
info = "set"+strValue+str.substring(1,str.length());
return info;
}
/**
* 把setXX還原為XX
* @param str
* @return
*/
public String setSetStr(String str){
String info = null;
String strValue = str.substring(3,str.length());
String lower = strValue.substring(0).toLowerCase().substring(0,1);
info = lower+str.substring(4,str.length());
return info;
}
/**
* 得到類(lèi)中的方法
* @param clazz 需要的得到方法的類(lèi)
* @return
*/
public Method[] getMethodsArray(Class clazz){
Method[] methods = clazz.getMethods();
return methods;
}
/**
* 根據(jù)list
* @param listMap
* @param clazz
* @param tableName 數(shù)據(jù)庫(kù)名稱
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings({ "unchecked" })
public List
List
List
Iterator<Map
while(it.hasNext()){
Map
Iterator
while(iitt.hasNext()){
String info = iitt.next();
this.getObjectArrays(map.get(info));
}
System.out.println("調(diào)用反射:"+this.cl.length+" "+this.ob.length);
Object 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"));
}
}
【java使用反射技術(shù)示例】相關(guān)文章:
深入理解java的反射07-16
Java編程學(xué)習(xí)示例07-31
關(guān)于循環(huán)的java代碼示例09-26
java.exe和javaw.exe的區(qū)別和使用方法示例09-21
使用Java的枚舉類(lèi)型的方法10-19
Java重載使用方法08-22