当前位置:学者斋 >

计算机 >java语言 >

java使用反射技术示例

java使用反射技术示例

复制代码 代码如下:

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"));

}

}

标签: 示例 JAVA 技术
  • 文章版权属于文章作者所有,转载请注明 https://xuezhezhai.com/jsj/java/xxpkw.html