?? reflector.java
字號:
package org.minjey.cjsjk.util;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;public class Reflector { public void setValue(Object obj, String fieldname, Object[] vobjs) { Class[] vclas = null; if(vobjs != null) { vclas = new Class[vobjs.length]; for(int i = 0; i < vobjs.length; i++) { vclas[i] = vobjs[i].getClass(); } } setValue(obj, fieldname, vobjs, vclas); } public void setValue(Object obj, String fieldname, Object[] vobjs, Class[] vclas) { doInvoke(obj, fieldname, vobjs, vclas, "set"); } public Object makeObject(String name, Object[] values) { Object obj = null; try { Class cla = Class.forName(name); obj = makeObject(cla, values); } catch (ClassNotFoundException e) { e.printStackTrace(); } return obj; } public Object makeObject(Class type, Object[] values) { Object obj = null; Class[] clas= null; if(values != null) { clas = new Class[values.length]; for(int i = 0; i < values.length; i++) { clas[i] = values[i].getClass(); } } try { obj = type.getConstructor(clas).newInstance(values); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return obj; } /* public Map<String, Object> getValues(Object obj) { Map<String, Object> map = new HashMap<String, Object>(); Class cla = obj.getClass(); Field[] fields = cla.getDeclaredFields(); for(int i = 0; i < fields.length; i ++) { String fieldname = fields[i].getName(); Object value = getValue(obj, fieldname); map.put(fieldname, value); } return map; } */ public Object getValue(Object obj, String fieldname) { return getValue(obj, fieldname, null); } public Object getValue(Object obj, String fieldname, Object[] vobjs) { Class[] vclas = getTypes(vobjs); return doInvoke(obj, fieldname, vobjs, vclas, "get"); } public Object doStaticGetValue(Class cla, String fieldname) { return doStaticInvoke(cla, "get" + fieldname, null); } public String getClassName(Class cla) { String name = cla.getSimpleName(); name = name.toLowerCase(); return name; } private Object doStaticInvoke(Class cla, String methodname, Object[] vobjs) { Object obj = null; Class[] vclas = getTypes(vobjs); try { Method meth = cla.getDeclaredMethod(methodname, vclas); obj = meth.invoke(null, null); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return obj; } private Object doInvoke(Object obj, String fieldname, Object[] vobjs, Class[] vclas, String type) { Object value = null; String methodname = type + Character.toUpperCase(fieldname.charAt(0)) + fieldname.substring(1); try { Method meth = obj.getClass().getMethod(methodname, vclas); value = meth.invoke(obj, vobjs); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return value; } private Class[] getTypes(Object[] objs) { Class[] vclas = null; if(objs != null) { vclas = new Class[objs.length]; for(int i = 0; i < objs.length; i++) { vclas[i] = objs[i].getClass(); } } return vclas; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -