反射获取设置值

package other;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Date;
import java.sql.Timestamp;

public class StuReflect {

    public static void main(String[] args) {
             
            try { 
                Stu stu1 = new Stu();
                stu1.setName("lisi");
                stu1.setFname("aaa");
                Object object = stu1;
                
                method(object, "name", "get", "");
                method(object, "name", "set", "ws");
                method(object, "name", "get", "");
                method(object, "fname", "get", "");
                } catch (Exception e) {
                e.printStackTrace();
            }
    }
    
    /**
     * 
     * @param model   反射对象
     * @param name    反射域
     * @param method set/get
     * @param value    重新赋值
     * @return
     */
    public static Object method(Object model,String name,String method,Object value) {
        try {
                Method m = null;
                Field field =  model.getClass().getDeclaredField(name);
                // 获得属性的类型
                String type = field.getGenericType().toString();
                // 可以访问私有属性
                field.setAccessible(true);
                name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1).toUpperCase());
                // 如果是类类型,前面包含class,后面跟类名
                if (type.equals("class java.lang.String")) {
                    if ("get".equals(method)) {
                        m = model.getClass().getMethod(method + name);
                        // get方法获取属性
                        System.out.println(m.invoke(model)); 
                        return (String)m.invoke(model);
                    }else if ("set".equals(method)) {
                        m = model.getClass().getMethod(method + name,String.class);
                        // set方法获取属性
                        m.invoke(model,(String)value);
                    }
                }
                if (type.equals("class java.lang.Float")) {
                    if ("get".equals(method)) {
                        m = model.getClass().getMethod(method + name);
                        // get方法获取属性
                        return (Float)m.invoke(model);
                    }else if ("set".equals(method)) {
                        m = model.getClass().getMethod(method + name,String.class);
                        // set方法获取属性
                        m.invoke(model,(Float)value);
                    }
                }
                if (type.equals("class java.lang.Date")) {
                    if ("get".equals(method)) {
                        m = model.getClass().getMethod(method + name);
                        // get方法获取属性
                        return (Date)m.invoke(model);
                    }else if ("set".equals(method)) {
                        m = model.getClass().getMethod(method + name,String.class);
                        // set方法获取属性
                        m.invoke(model,(Date)value);
                    }
                }
                if (type.equals("class java.lang.Timestamp")) {
                    if ("get".equals(method)) {
                        m = model.getClass().getMethod(method + name);
                        // get方法获取属性
                        return (Timestamp)m.invoke(model);
                    }else if ("set".equals(method)) {
                        m = model.getClass().getMethod(method + name,String.class);
                        // set方法获取属性
                        m.invoke(model,(Timestamp)value);
                    }
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

}
package other;

public class Stu {

    
    private String  name;
    
    private String fname;

    private Integer age;

    private Double  height;

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Double getHeight() {
        return height;
    }

    public void setHeight(Double height) {
        this.height = height;
    }
    
}

 

posted @ 2018-01-31 00:59  aLa神灯  阅读(297)  评论(0编辑  收藏  举报