`

反射通用类

    博客分类:
  • java
阅读更多


import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/***
 * @java反射通用类
 */
public class Reflection {
 /**    
     * 得到某个对象的公共属性public    
     * @param owner, fieldName    
     * @return 该属性对象    
     * @throws Exception    
     */     
    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;      
    } 
   
    /***
     *
     * @param owner
     * @param fieldName
     * @param value
     * @throws Exception
     */
    public void setProperty(Object owner,String fieldName,Object value){
        try {
            Class<?> ownerClass = owner.getClass();      
            Field field = ownerClass.getField(fieldName);    
   field.set(owner, value);
  } catch (Exception e) {
   
  }  
    }
     
    /**    
     * 得到某类的静态公共属性    
     * @param className   类名    
     * @param fieldName   属性名    
     * @return 该属性对象    
     * @throws Exception    
     */     
    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;      
    }      
    
    /**    
     * 执行某对象方法 参数 
     * @param owner 对象  
     * @param methodName  方法名  
     * @param args  参数 参数不能为空 
     * @return 方法返回值    
     * @throws Exception    
     */     
    public Object invokeMethod(Object owner, String methodName, Object args){ 
     Class<?>  ownerClass = owner.getClass();
        Method method = null;
        Object o = null;
  try {
   method = ownerClass.getMethod(methodName, args.getClass());
  } catch (Exception e1) {
   e1.printStackTrace();
  } 
  try {
   o = method.invoke(owner,args);
  } catch (Exception e) {
   e.printStackTrace();
  }
        return o;      
    }
     
    /**    
     * 执行某对象方法 执行方法时候,有参数就传数组 没有时候就传null 
     * @param owner 对象  
     * @param methodName  方法名  
     * @param args  参数  
     * @return 方法返回值    
     * @throws Exception    
     */     
    public Object invokeMethods(Object owner, String methodName, Object[] args)      
            throws Exception {      
     Class<?>  ownerClass = owner.getClass();
     Class<?>[] argsClass = null;
     int len = args.length;
     if(args!=null){
      argsClass = new Class[len]; 
            for(int i = 0, j = len; i < j; i++) {      
                argsClass[i] = args[i].getClass();      
            }
     }
        Method method = null;
  try {
   method = ownerClass.getMethod(methodName, argsClass);
  } catch (RuntimeException e1) {
   e1.printStackTrace();
  } 
        Object o = null;
  try {
   o = method.invoke(owner, args);
  } catch (Exception e) {
  }
        return o;      
    }      
     
    /**    
     * 执行某类的静态方法    
     * @param className  类名    
     * @param methodName  方法名  
     * @param args  参数数组  
     * @return 执行方法返回的结果    
     * @throws Exception    
     */     
    public Object invokeStaticMethod(String className, String methodName,      
            Object[] args) throws Exception {      
        Class<?>  ownerClass = Class.forName(className);      
        Class<?>[] argsClass = null;
        int len = args.length;
        if(args!=null){
         argsClass = new Class[len];
            for (int i = 0, j = len; i < j; i++) {      
                argsClass[i] = args[i].getClass();      
            }
        }
        Method method = ownerClass.getMethod(methodName, argsClass);      
        return method.invoke(null, args);      
    }      
     
    /**    
     * 新建实例    
     * @param className  类名  
     * @param args   多个构造函数的参数  
     * @return 新建的实例    
     * @throws Exception    
     */     
    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);      
     
    } 
   
    /**    
     * 新建实例    
     * @param className  类名  
     * @param args   一个构造函数的参数  
     * @return 新建的实例    
     * @throws Exception    
     */     
    public Object newInstance(String className) {
     Object obj = null;
        try {
   Class<?>  newoneClass = Class.forName(className);      
   Constructor<?>  cons  = newoneClass.getConstructor();
   obj = cons.newInstance();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return obj;
     
    }
   
    /***
     * 新建一个数组对象
     * @param cla 要生成数组的Class对象
     * @param length 数组的大小
     * @return
     */
    public Object[] newArray(Class<?> cla ,int length){
     return (Object[])Array.newInstance(cla, length);
    }
          
    /**    
     * 是不是某个类的实例    
     * @param obj 实例    
     * @param cls 类    
     * @return 如果 obj 是此类的实例,则返回 true    
     */     
    public boolean isInstance(Object obj, Class<?>  cls) {      
        return cls.isInstance(obj);      
    }      
          
    /**    
     * 得到数组中的某个元素    
     * @param array 数组    
     * @param index 索引    
     * @return 返回指定数组对象中索引组件的值    
     */     
    public Object getByArray(Object array, int index) {     
        return Array.get(array,index);      
    }
}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics