java SE - reflect

测试类

package testReflect;

/** 
 * @Package testReflect

 * @ClassName: Hello

 * @Description: TODO()

 * @author andy

 * @date 2013-5-31 上午09:59:29

 */
public class Hello {
    public String name;
    private int id = 1;
    
    public Hello(){
        
    }
    public Hello(String name,int id){
        this.name = name;
        this.id = id;
    }
    
    
    public String getUserName(){
        return "123";
    }


    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }
}
View Code

获得某个类或对象的运行时Class的对象的三种方式

package testReflect;

/** 
 * @Package testReflect

 * @ClassName: InstancingClass

 * @Description: TODO(获得某个类或对象的运行时Class的对象的三种方式)

 * @author andy

 * @date 2013-5-31 上午09:59:05

 */
public class InstancingClass<T> {
    public static void main(String[] args) throws ClassNotFoundException {
        //第一种
        Class<Hello> classType1 = Hello.class;
        //第二种
        Class<?> classType2 = Class.forName("testReflect.Hello");
        //第三种
        Hello hello = new Hello();
        Class<? extends Hello> classType3 = hello.getClass();
        
        System.out.println(classType1 + "\n" +classType2 + "\n" + classType3 + "\n");
        System.out.println(classType1.getName() + "\n" +classType2.getName() + "\n" + classType3.getName() + "\n");
        System.out.println(classType1.getSimpleName() + "\n" +classType2.getSimpleName() + "\n" + classType3.getSimpleName() + "\n");
    }
}
View Code

不用new关键字实例化对象的三种方法

package testReflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/** 
 * @Package testReflect

 * @ClassName: InstancingObject

 * @Description: TODO(不用new关键字实例化对象的三种方法)

 * @author andy

 * @date 2013-5-31 上午10:08:34

 */
public class InstancingObject {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        Hello hello0 = Hello.class.newInstance();
        
        Class<Hello> classType = Hello.class;
        
        //构造函数没有参数时,第一种等价于第二种
        //第一种
        Hello hello1 = (Hello) classType.newInstance();
        
        //第二种
        Constructor<Hello> con = classType.getConstructor();
        Hello hello2 = (Hello) con.newInstance();
        
        //第三种
        Constructor<Hello> con2 = classType.getConstructor(new Class[]{String.class, int.class});
        Hello hello3 =  (Hello)con2.newInstance(new Object[]{"123",123});
        
        System.out.println(hello0.getUserName());
        System.out.println(hello1.getUserName());
        System.out.println(hello2.getUserName());
        System.out.println(hello3.getUserName());
    }
}
View Code

通过反射对字段的操作

package testReflect;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;

/** 
 * @Package testReflect

 * @ClassName: AccessFieldByReflect

 * @Description: TODO(通过反射对字段的操作)

 * @author andy

 * @date 2013-5-31 上午10:48:13

 */
public class AccessFieldByReflect {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
        Hello hello = Hello.class.newInstance();
        Field[] fields = hello.getClass().getDeclaredFields();
        //遍历获取所有的字段
        for(Field field : fields){
            //System.out.println(field);
            System.out.println(field.getName());
        }
        System.out.println("没有修改前:" + hello.getName());
        System.out.println("没有修改前:" + hello.getId());
        for(Field field : fields){
            if("id".equals(field.getName())){
                //允许访问所有Field、Method 和 Constructor 私有元素
                field.setAccessible(true);
                //指定只允许访问的Field
                AccessibleObject[] accessParam = {field};
                Field.setAccessible(accessParam,true);
                //如果那个字段是int就setint,是double就setdouble
                field.setInt(hello, 123);
            }else{
                //字段是字符串
                field.set(hello, "张三");
            }
        }
        
        System.out.println("通过反射修改后:" + hello.getId());
        System.out.println("通过反射修改后:" + hello.getName());
    }
}
View Code

通过反射对方法的操作

package testReflect;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/** 
 * @Package testReflect

 * @ClassName: AccessMethodByReflect

 * @Description: TODO(通过反射对方法的操作)

 * @author andy

 * @date 2013-5-31 上午11:25:39

 */
public class AccessMethodByReflect {
    public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, SecurityException {
        Hello hello = Hello.class.newInstance();
        Method[] methods = hello.getClass().getDeclaredMethods();
        //遍历获取所有的字段
        for(Method method : methods){
            //System.out.println(field);
            System.out.println(method.getName());
        }
        System.out.println("没有修改前:" + hello.getName());
        System.out.println("没有修改前:" + hello.getId());
        for(Method method : methods){
            if("setId".equals(method.getName())){
                //允许访问所有Field、Method 和 Constructor 私有元素
                method.setAccessible(true);
                //只允许访问指定的Method
                AccessibleObject[] accessParam = {method};
                Method.setAccessible(accessParam,true);
            }else if("setName".equals(method.getName())){
                method.invoke(hello, "张三");
            }
        }
        
        String obj = (String) hello.getClass().getDeclaredMethod("getName").invoke(hello); 
        System.out.println("通过反射getName方法返回来的值:" + obj);
        System.out.println("通过反射修改后:" + hello.getId());
        System.out.println("通过反射修改后:" + hello.getName());
        
    }
}
View Code

 

 

 

Integer.TYPE 返回的int,即返回的是它们所对应的原生数据class对象(所有的包装类都是这样的),

Integer.class 返回的是它所对应的Class对象 java.lang.Integer

 

 

posted on 2013-05-31 14:51  lovebeauty  阅读(353)  评论(0编辑  收藏  举报

导航