关于Java反射破坏封装的问题

今天看ITEYE,一片关于Java反射破话封装的问题,摘录如下:

一般情况下,我们并不能对类的私有字段进行操作,利用反射也不例外,但有的时候,例如要序列化的时候,我们又必须有能力去处理这些字段,这时候,我们就需要调用AccessibleObject上的setAccessible()方法来允许这种访问,而由于反射类中的Field,Method和Constructor继承自AccessibleObject,因此,通过在这些类上调用setAccessible()方法,我们可以实现对这些字段的操作。但有的时候这将会成为一个安全隐患,为此,我们可以启用java.security.manager来判断程序是否具有调用setAccessible()的权限。默认情况下,内核API和扩展目录的代码具有该权限,而类路径或通过URLClassLoader加载的应用程序不拥有此权限 。

 

package com.dream.reflection;   
  
/**  
 * Created by IntelliJ IDEA.  
 * User: Zhong Gang  
 * Date: 11-9-24  
 * Time: 下午7:02  
 */  
public class User {   
    private Integer id;   
    private int number;   
    private String name = "zhong_gang";   
  
    private User(Integer id) {   
        this.id = id;   
    }   
  
    private User(int number) {   
        this.number = number;   
    }   
  
    protected User(String name) {   
        this.name = name;   
    }   
  
    private User(Integer id, String name) {   
        this.id = id;   
        this.name = name;   
    }   
  
    public User() {   
  
    }   
  
    public String upperName() {   
        return name.toUpperCase();   
    }   
  
    public Integer getId() {   
        return id;   
    }   
  
    public String getName() {   
        return name;   
    }   
  
    public int getNumber() {   
        return number;   
    }   
}  

 

package com.dream.reflection;   
  
import junit.framework.TestCase;   
  
import java.lang.reflect.Constructor;   
import java.lang.reflect.Field;   
import java.lang.reflect.Method;   
  
/**  
 * Created by IntelliJ IDEA.  
 * User: Zhong Gang  
 * Date: 11-9-24  
 * Time: 下午7:03  
 */  
public class UserRelectionTest extends TestCase {   
  
    public void testIntegerReflection() throws Exception {   
        Class<User> clazz = User.class;   
        Constructor<User> constructor = clazz.getDeclaredConstructor(Integer.class);   
        constructor.setAccessible(true);   
        User user = constructor.newInstance(Integer.valueOf("1"));   
        assertEquals(Integer.valueOf("1"), user.getId());   
    }   
  
    public void testIntReflection() throws Exception {   
        Class<User> clazz = User.class;   
        Constructor<User> constructor = clazz.getDeclaredConstructor(int.class);   
        constructor.setAccessible(true);   
        User user = constructor.newInstance(1);   
        assertEquals(1, user.getNumber());   
    }   
  
    public void testStringReflection() throws Exception {   
        Class<User> clazz = User.class;   
        Constructor<User> constructor = clazz.getDeclaredConstructor(String.class);   
//        constructor.setAccessible(true);   
        User user = constructor.newInstance("ZhongGang");   
        assertEquals("ZhongGang", user.getName());   
    }   
  
    public void testFiledReflection() throws Exception {   
        Class<User> clazz = User.class;   
        Field id = clazz.getDeclaredField("id");   
        id.setAccessible(true);   
        User user = new User();   
        id.set(user, Integer.valueOf("3"));   
        assertEquals(Integer.valueOf("3"), user.getId());   
    }   
  
    public void testMethodReflection() throws Exception {   
        Class<User> clazz = User.class;   
        Method method = clazz.getDeclaredMethod("upperName");   
//        method.setAccessible(true);   
        User user = new User();   
        String invoke = (String) method.invoke(user);   
        assertEquals("ZHONG_GANG", invoke);   
    }   
}  

 

另一篇关于反射int.class 与 Integer.class的:

 

http://www.iteye.com/problems/72276

你可能感兴趣的:(java反射)