堕落不需要理由,需要的只是借口。

刚毕业的菜鸟,写点东西记录下,万一忘了。

通过反射访问对象的私有变量和执行私有方法

 
package reflection.domain;

/**
 * <学生域>
 * 
 * @author snowday88
 */
public class Student
{
    private Integer id = 1;
    
    private String name = "wangguobo";
    
    public Student()
    {
    }
    
    public Student(Integer id, String name)
    {
        this.id = id;
        this.name = name;
    }
    
    private int getId()
    {
        return this.id;
    }
    
    private String sayHello()
    {
        return this.name;
    }
}

package reflection.refleftionDemo;

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

import org.junit.Test;

import reflection.domain.Student;

/**
 * <Test>
 * 
 * @author snowday88
 */
@SuppressWarnings("unchecked")
public class ReflectionTest
{
    
    // 访问私有变量
    @Test
    public void accessPrivateField() throws Exception
    {
        // 方式一 访问私有变量
        Class clazz = Class.forName("reflection.domain.Student");
        // 创建对象
        Object obj = clazz.newInstance();
        getValue(clazz, obj);
        System.out.println("------------------------");
        
        // 方式二
        Student student = new Student();
        Class clzz = student.getClass();
        getValue(clzz, student);
        System.out.println("-------------------------");
        // 方式三
        Student stu = new Student();
        Class c = Student.class;
        getValue(c, stu);
        System.out.println("-------------end---------");
    }
    
    // 访问私有方法
    @Test
    public void accessPrivateMethod() throws Exception
    {
        System.out.println("调用私有方法");
        Class clazz = Class.forName("reflection.domain.Student");
        Student student = (Student)clazz.newInstance();
        
        Method[] methods = clazz.getDeclaredMethods();
        for (Method m : methods)
        {
            m.setAccessible(true);
            Object object = m.invoke(student, new Object[] {});
            
            System.out.println(m.getName());
            System.out.println(object == null ? "" : object.toString());
            System.out.println("------------------------------------------");
        }
    }
    
    // 根据有参数的构造方法构造对象
    @Test
    public void getInstance() throws Exception
    {
        Class clazz = Student.class;
        // 得到有参数的构造方法
        Constructor<Student> con =
            clazz.getConstructor(new Class[] {java.lang.Integer.class,
                java.lang.String.class});
        Student s = con.newInstance(new Object[] {3, "gbwang"});
        
        getValue(s.getClass(), s);
    }
    
    /**
     * 访问私有变量值
     */
    private static void getValue(Class clazz, Object obj)
        throws IllegalAccessException
    {
        Field[] fields = clazz.getDeclaredFields();
        
        for (Field field : fields)
        {
            field.setAccessible(true);
            System.out.println(field.get(obj));
        }
    }
    
}

阅读更多
个人分类: 反射
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭