Java 反射机制示例讲解

本文深入介绍了Java反射API的使用方法,包括如何获取对象的属性、调用方法、创建实例等核心功能。通过示例代码展示了反射机制的强大能力,适用于希望深入了解Java内部机制的开发者。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

public class Reflection {
   
   /**
     * 返回某个对象的公共属性
     *
     * @param owner, fieldName
     * @return 该属性对象
     * @throws Exception
     */
    public Object getProperty(Object owner, String fieldName) throws Exception {
        // 首先,得到该对象的Class
        Class ownerClass = owner.getClass();
       
        // 通过Class得到类声明的属性
        Field field = ownerClass.getField(fieldName);
       
        // 通过对象得到该属性的实例,如果这个属性是非公有的,这里会报IllegalAccessException
        Object property = field.get(owner);

        return property;
    }

    /**
     * 返回某个对象的静态公共属性
     *
     * @param className   类名
     * @param fieldName   属性名
     * @return 该属性对象
     * @throws Exception
     */
    public Object getStaticProperty(String className, String fieldName)
            throws Exception {
        // 得到类的Class
        Class ownerClass = Class.forName(className);
       
        // 通过Class得到类声明的属性
        Field field = ownerClass.getField(fieldName);
       
        // 因为该属性是静态的,所以直接从类的Class里取
        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)
            throws Exception {

        Class ownerClass = owner.getClass();
       
        // 配置参数的Class数组,作为寻找Method的条件
        Class[] argsClass = null;
        if (args != null && args.length > 0) {
         argsClass = new Class[args.length];
 
         for (int i = 0, j = args.length; i < j; i++) {
             argsClass[i] = args[i].getClass();
         }
        }
 
        // 通过Method名和参数的Class数组得到要执行的Method
        Method method = ownerClass.getMethod(methodName, argsClass);

       

        // 执行该Method,invoke方法的参数是执行这个方法的对象,和参数数组。

        //返回值是Object,也既是该方法的返回值
        return method.invoke(owner, args);
    }


      /**
     * 执行某类的静态方法
     *
     * @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;
        if (args != null && args.length > 0) {
            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);
       
        // invoke的一个参数是null,因为这是静态方法,不需要借助实例运行
        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 = null;
        if (args != null && args.length > 0) {
         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 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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值