JAVA反射机制的应用场景

原文链接:http://blog.csdn.net/sun5769675/article/details/48787357



  1. 大家是不是经常遇到一种情况,比如两个对象,Member和MemberView,很多时候我们都有可能进行相互转换,那么我们常用的方法就是,把其中一个中的值挨个get出来,然后再挨个set到另一个中去,接下来我介绍的这种方法就可以解决这种问题造成的困扰,多的不说,看下面代码(ReflectionDemo类的test1()方法):
 Member类:
package test;

public class Member {
	private String  username;

    private String  password;

    private String  truename;

    private String  nickname;

    private String  gender;

    private int     age;

    @Override
    public String toString(){
        return (username==null?"":"username:" + username) +

                (password==null?"":" password:" + password) +

                (truename==null?"":" truename:" + truename) +

                (nickname==null?"":" nickname:" + nickname) +

                (gender==null?"":" gender:" + gender) +

                " age:" + age;
    }
}

MemberView类:
package test;

public class MemberView {
	private String  username;

    private String  password;

    private String  truename;

    private String  nickname;

    private String  gender;

    private int     age;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTruename() {
		return truename;
	}

	public void setTruename(String truename) {
		this.truename = truename;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public int getAge() {
		return age;
	}

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

 ReflectionDemo类:
package test;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Java反射机制测试
 * @author Tomorrow
 *
 */
public class ReflectionDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//测试1
		test1();	

		//测试2
		test2();
	}
	
	public static void test1(){
		MemberView fromObject = new MemberView();
        fromObject.setUsername("sunchenbin");
        fromObject.setPassword("111111");
        fromObject.setAge(21);
        fromObject.setGender("男");
        fromObject.setNickname("墨白");
        String[] fields = new String[]{}; // 没有设置属性,默认去对比两个对象
        //String[] fields = new String[]{"username","password","gender"};
        try{
            // 将一个对象转换成另一个对象,并把指定的属性值传递给这个对象,
            // 如果不指定默认去匹配两个对象的属性,存在则赋值
            Member member = (Member) constructObject(fromObject,new Member(),fields);
            System.out.println(member.toString());
            
           // 执行结果:username:sunchenbin password:111111 nickname:墨白 gender:男 age:21
            
        }catch (Exception e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
	}
	
	public static void test2(){
		String className = "test.ReflectionDemo";
	    String methodName = "eachOrtherToAdd";
	    String[] paramTypes = new String[]{"Integer","Double","int"};
	    String[] paramValues = new String[]{"1","4.3321","5"};
	
	    // 动态加载对象并执行方法
	    initLoadClass(className, methodName, paramTypes, paramValues);
	}
	
	private static Object constructObject(Object fromObject,Object toObject,String[] fields) throws Exception{
		// 数据源的class
		Class fromClass = fromObject.getClass();
		// 目标的class
		Class toClass = toObject.getClass();
		
		for(String field:fields){
			try{
				// 获取fromClass的Field
				Field fromField= fromClass.getDeclaredField(field);
				fromField.setAccessible(true);			
				// 从fromClass中获取属性的值
				Object fromValue = fromField.get(fromObject);
				
				// 获取toClass的Field
				Field toField = toClass.getDeclaredField(field);
				toField.setAccessible(true);
				// 将fromClass中该属性的值设置给toClass中的该属性
				toField.set(toObject, fromValue);
			}catch (Exception e) {
				System.out.println(field+"属性不存在");
				// TODO: handle exception
			}
			
		}

		// 如果没有传递属性过来,那么默认对比from和to中的属性,存在的进行赋值操作
        if(fields.length == 0){
            Field[] fromDeclaredFields = fromClass.getDeclaredFields();
            Field[] toDeclaredFields = toClass.getDeclaredFields();
            List fromList = new ArrayList();
            List toList = new ArrayList();

            // 取出from中所有field
            for (Field field : fromDeclaredFields){
                field.setAccessible(true);
                fromList.add(field.getName());
            }

            // 取出to中所有field
            for (Field field : toDeclaredFields){
                field.setAccessible(true);
                toList.add(field.getName());
            }

            // 循环from属性list
            for (String name : fromList){

                // to中是否包含该属性
                if(toList.contains(name)){

                    // 包含先进行取值
                    Field fromDeclaredField = fromClass.getDeclaredField(name);
                    fromDeclaredField.setAccessible(true);
                    Object value = fromDeclaredField.get(fromObject);

                    // 进行赋值操作
                    Field toDeclaredField = toClass.getDeclaredField(name);
                    toDeclaredField.setAccessible(true);
                    toDeclaredField.set(toObject, value);
                }
            }
        }

        return toObject;
	}
	
	public double eachOrtherToAdd(Integer one,Double two,Integer three){
        return one+two+three;
    }
	
	@SuppressWarnings("rawtypes")
    private static void initLoadClass(String className,String methodName,String[] paramTypes,String[] paramValues){
        try{
            // 根据calssName得到class对象
            Class cls = Class.forName(className);

            // 实例化对象
            Object obj = cls.newInstance();

            // 根据参数类型数组得到参数类型的Class数组
            Class[] parameterTypes = constructTypes(paramTypes);

            // 得到方法
            Method method = cls.getMethod(methodName, parameterTypes);

            // 根据参数类型数组和参数值数组得到参数值的obj数组
            Object[] parameterValues = constructValues(paramTypes,paramValues);

            // 执行这个方法并返回obj值
            Object returnValue = method.invoke(obj, parameterValues);

            System.out.println("测试结果2:"+returnValue);

        }catch (Exception e){
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static Object[] constructValues(String[] paramTypes,String[] paramValues){
        Object[] obj = new Object[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++){
            if(paramTypes[i] != null && !paramTypes[i].trim().equals("")){
                if ("Integer".equals(paramTypes[i]) || "int".equals(paramTypes[i])){
                    obj[i] = Integer.parseInt(paramValues[i]);
                }else if ("Double".equals(paramTypes[i]) || "double".equals(paramTypes[i])){
                    obj[i] = Double.parseDouble(paramValues[i]);
                }else if ("Float".equals(paramTypes[i]) || "float".equals(paramTypes[i])){
                    obj[i] = Float.parseFloat(paramValues[i]);
                }else{
                    obj[i] = paramTypes[i];
                }
            }
        }
        return obj;
    }

    @SuppressWarnings("rawtypes")
    private static Class[] constructTypes(String[] paramTypes){
        Class[] cls = new Class[paramTypes.length];
        for (int i = 0; i < paramTypes.length; i++){
            if(paramTypes[i] != null && !paramTypes[i].trim().equals("")){
                if ("Integer".equals(paramTypes[i]) || "int".equals(paramTypes[i])){
                    cls[i] = Integer.class;
                }else if ("Double".equals(paramTypes[i]) || "double".equals(paramTypes[i])){
                    cls[i] = Double.class;
                }else if ("Float".equals(paramTypes[i]) || "float".equals(paramTypes[i])){
                    cls[i] = Float.class;
                }else{
                    cls[i] = String.class;
                }
            }
        }
        return cls;
    }
}
	


2. 还有一种场合,程序执行时,要动态加载某个类并执行方法,上面的test2()方法。

详解就不说了,说起java的反射机制,基本就会想到4个类,Field、Constractor、Method、Class,依次是分别封装了反射的属性、构造函数、方法,剩下的Class就不多说了,基本掌握这的应用就OK了,至于业务场景还有很多。


你可能感兴趣的:(java)