Java反射

Java反射使我们可以很方便的动态获取对象属性和调用对象方法。使用得当,能够使我们的代码很简洁优雅。下面是反射的一些小技巧。

/**
 * 测试用例
 * @author zhanzhengqiang
 */
public class Test {
    public static void main(String[] args) throws ClassNotFoundException, 
        SecurityException, NoSuchFieldException, 
        IllegalArgumentException, IllegalAccessException, 
        NoSuchMethodException, InvocationTargetException{
        // 获取Class字节码
        Class clazz = Class.forName("reflect.fortest.JavaBean");
        
        // 构造JavaBean
        // this.intField = 1;
        // this.stringField = "a";
        // this.stringProField = "b";
        JavaBean javaBean = new JavaBean(1, "pri-a", "pro-b");
        javaBean.stringPubField = "pub-c";
        
        // 获取Public属性值
        Field stringPubField = clazz.getField("stringPubField");
        String stringPubFieldValue = (String) stringPubField.get(javaBean);
        System.out.println("stringPubField=" + stringPubFieldValue);
        
        // 获取Protected属性值
        Field stringProField = clazz.getDeclaredField("stringProField");
        stringProField.setAccessible(true);
        String stringProFieldValue = (String) stringProField.get(javaBean);
        System.out.println("stringProField=" + stringProFieldValue);
        
        // 获取Private属性值
        Field stringPriField = clazz.getDeclaredField("stringField");
        stringPriField.setAccessible(true);
        String stringPriFieldValue = (String) stringPriField.get(javaBean);
        System.out.println("stringField=" + stringPriFieldValue);
        
        Field intPriField = clazz.getDeclaredField("intField");
        intPriField.setAccessible(true);
        int intPriFieldValue = intPriField.getInt(javaBean);
        System.out.println("intField=" + intPriFieldValue);
        
        // 获取静态属性值
        Field staticField = clazz.getField("staticField");
        String staticFieldValue = (String) staticField.get(null);
        System.out.println("staticField=" + staticFieldValue);
        
        // 调用publicMethod()
        Method pubMethod1 = clazz.getMethod("publicMethod", null);
        Object ret1 = pubMethod1.invoke(javaBean, null);
        System.out.println("ret1=" + ret1);
        
        // 调用publicMethod(String)
        Method pubMethod2 = clazz.getMethod("publicMethod", new Class[]{String.class});
        String ret2 = (String) pubMethod2.invoke(javaBean, new Object[]{"StringParam"});
        System.out.println("ret2=" + ret2);
        
        // 调用protectedMethod()
        Method protectedMethod = clazz.getDeclaredMethod("protectedMethod", null);
        protectedMethod.setAccessible(true);
        protectedMethod.invoke(javaBean, null);
        
        // 调用privateMethod()
        Method privateMethod = clazz.getDeclaredMethod("privateMethod", null);
        privateMethod.setAccessible(true);
        privateMethod.invoke(javaBean, null);
        
        // 调用staticMethod()
        Method staticMethod = clazz.getDeclaredMethod("staticMethod", null);
        staticMethod.setAccessible(true);
        staticMethod.invoke(null, null);
    }
}

/**
 * 测试用JavaBean
 * @author zhanzhengqiang
 */
package reflect.fortest;

public class JavaBean {

    private int intField;
    
    private String stringField;
    
    protected String stringProField;
    
    public String stringPubField;

    public static String staticField = "static";
    
    public JavaBean(int intField, String stringField, String stringProField) {
        this.intField = intField;
        this.stringField = stringField;
        this.stringProField = stringProField;
    }
    
    public void publicMethod() {
        System.out.println("publicMethod is invoked");
    }
    
    public String publicMethod(String param) {
        System.out.println("publicMethod(" + param + ") is invoked");
        return "retValue";
    }
    
    protected void protectedMethod() {
        System.out.println("protectedMethod is invoked");
    }
    
    private void privateMethod() {
        System.out.println("privateMethod is invoked");
    }
    
    public static void staticMethod() {
        System.out.println("staticMethod is invoked");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值