- package cn.test;
-
-
- import java.lang.reflect.Array;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
-
-
-
-
-
-
-
-
-
-
- public class TestReflection {
-
-
-
-
-
-
-
-
- public Object getProperty(Object owner, String fieldName) throws Exception {
- Class ownerClass = owner.getClass();
-
- Field field = ownerClass.getField(fieldName);
-
- Object property = field.get(owner);
-
- return property;
- }
-
-
-
-
-
-
-
-
-
- public Object getStaticProperty(String className, String fieldName)
- throws Exception {
- Class ownerClass = Class.forName(className);
-
- Field field = ownerClass.getField(fieldName);
-
- Object property = field.get(ownerClass);
-
- return property;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Object invokeMethod(Object owner, String methodName, Object[] args)
- throws Exception {
-
- Class ownerClass = owner.getClass();
-
- Class[] argsClass = new Class[args.length];
-
- for (int i = 0, j = args.length; i < j; i++) {
- argsClass[i] = args[i].getClass();
- }
-
- Method method = ownerClass.getMethod(methodName, argsClass);
-
- return method.invoke(owner, args);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Object invokeStaticMethod(String className, String methodName,
- Object[] args) throws Exception {
- Class ownerClass = Class.forName(className);
-
- Class[] argsClass = new Class[args.length];
-
- for (int i = 0, j = args.length; i < j; i++) {
- argsClass[i] = args[i].getClass();
- }
-
- Method method = ownerClass.getMethod(methodName, argsClass);
-
- return method.invoke(null, args);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- public Object newInstance(String className, Object[] args) throws Exception {
- Class newoneClass = Class.forName(className);
-
- Class[] argsClass = new Class[args.length];
-
- for (int i = 0, j = args.length; i < j; i++) {
- argsClass[i] = args[i].getClass();
- }
-
- Constructor cons = newoneClass.getConstructor(argsClass);
-
- return cons.newInstance(args);
-
- }
-
-
-
-
-
-
-
-
-
- public boolean isInstance(Object obj, Class cls) {
- return cls.isInstance(obj);
- }
-
-
-
-
-
-
-
- public Object getByArray(Object array, int index) {
- return Array.get(array,index);
- }
- }
- package cn.test;
-
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.ArrayList;
-
- import cn.IpUtils.IpBean;
-
- public class TestObject {
-
-
-
-
-
-
-
- public ArrayList array2bean(ArrayList list, Class cla) {
- ArrayList result = new ArrayList();
- int filed_len = cla.getDeclaredFields().length;
- System.out.println(":"+cla.getDeclaredFields().length);
- for (int i = 0; i < list.size(); i++) {
- Object[] o = (Object[]) list.get(i);
- int length = filed_len < o.length ? filed_len : o.length;
- try {
- result.add(cla.newInstance());
- for (int j = 0; j < length; j++) {
- Method m = null;
- String mName =cla.getDeclaredFields()[j].getName();
- mName = mName.substring(0, 1).toUpperCase()+ mName.substring(1);
- mName = "set" + mName;
- m = cla.getMethod(mName, new Class[] { String.class });
-
- m.invoke(result.get(i), new Object[] { o[j] == null ? "": o[j].toString() });
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return result;
- }
-
-
-
-
-
-
-
- public String getObjectToString(Object obj) throws Exception{
- Class cla=obj.getClass();
- Method[] ma=cla.getDeclaredMethods();
- Method method=null;
- String methodName=null;
- Object returnValue=null;
- for(int i=0;i<ma.length;i++){
- method=ma[i];
- methodName=method.getName();
- if(methodName.indexOf("get")==0){
- returnValue=method.invoke(obj, null);
- System.out.print(methodName+"::");
- System.out.println(returnValue==null?"":returnValue.toString());
- }
- }
- return "";
- }
-
-
-
-
-
-
-
-
-
-
-
- public String getAttributeValue(Object obj) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
- Class cla=obj.getClass();
- Field[] fds=cla.getDeclaredFields();
- Field field=null;
- String fieldName=null;
- String methodName=null;
- Method method=null;
- Object returnValue=null;
- for(int i=0;i<fds.length;i++){
- field=fds[i];
- fieldName=field.getName();
- methodName="get"+fieldName.substring(0, 1).toUpperCase()+fieldName.substring(1);
- method=cla.getDeclaredMethod(methodName, null);
- returnValue=method.invoke(obj, null);
- System.out.print(methodName+"::");
- System.out.println(returnValue==null?"":returnValue.toString());
- }
-
- return "";
- }
-
-
-
-
-
- public static void main(String[] args) throws Exception {
-
- TestObject to=new TestObject();
- IpBean ib=new IpBean();
- ib.setFrom("from1");
- ib.setPosition("position1");
- ib.setTo("to1");
- ib.setId(10);
- to.getObjectToString(ib);
-
- }
-
- }
posted on
2010-05-09 22:07
huihui-热带鱼
阅读(
2929)
评论()
收藏
举报