反射获取属性和方法

@Entity
@Table
public class CoursewarePackage {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;
    
    private String name;
    
    @Lob
    private String description;
    
    @ManyToOne
    private User creater;
    
    private int clickNum;
    
    @ManyToMany(fetch =FetchType.LAZY)
    private List<Property> properties;
    
    private String  picturePath;
    
    private String realPicturePath;

	/**
	 * @return the picturePath
	 */
	public String getPicturePath() {
		return picturePath;
	}

	/**
	 * @param picturePath the picturePath to set
	 */
	public void setPicturePath(String picturePath) {
		this.picturePath = picturePath;
	}

	public List<Property> getProperties() {
		return properties;
	}

	public void setProperties(List<Property> properties) {
		this.properties = properties;
	}

	/**
	 * @return the realPicturePath
	 */
	public String getRealPicturePath() {
		return realPicturePath;
	}

	/**
	 * @param realPicturePath the realPicturePath to set
	 */
	public void setRealPicturePath(String realPicturePath) {
		this.realPicturePath = realPicturePath;
	}
	
	public User getCreater() {
        return creater;
    }

    public void setCreater(User creater) {
        this.creater = creater;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

	/**
	 * @return the clickNum
	 */
	public int getClickNum() {
		return clickNum;
	}

	public void setClickNum(int clickNum) {
		this.clickNum = clickNum;
	}
}


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

import org.junit.Test;

import com.boventech.learning.entity.CoursewarePackage;
import com.boventech.learning.entity.Property;
import com.boventech.learning.util.ReflectionUtils;
import com.google.common.collect.Lists;


public class ReflectionUtilsTest {

	@Test
	public void testGetDeclaredMethod() { 
		CoursewarePackage coursewarePackage = getCoursewarePackage();
		
		Method getId = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getId") ;
		System.out.println(getId.getName());
		
		Method getClickNum = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getClickNum") ;
		System.out.println(getClickNum.getName());
		
		Method getDescription = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getDescription") ;
		System.out.println(getDescription.getName());
		
		Method getName = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getName") ;
		System.out.println(getName.getName());
		
		Method getPicturePath = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getPicturePath") ;
		System.out.println(getPicturePath.getName());
		
		Method getRealPicturePath = ReflectionUtils.getDeclaredMethod(coursewarePackage, "getRealPicturePath") ;
		System.out.println(getRealPicturePath.getName());
		
		System.out.println("----------------------------------------------------");
		
		
	}
	
	/**
	 * 通过属性名获取get方法得到的属性值
	 */
	@Test  
	public void testGetDeclaredField() {
		CoursewarePackage coursewarePackage = getCoursewarePackage();
		
		System.out.println("id = " + ReflectionUtils.getFieldValue(coursewarePackage, "id"));  
		System.out.println("clickNum = " + ReflectionUtils.getFieldValue(coursewarePackage, "clickNum"));
		System.out.println("description = " + ReflectionUtils.getFieldValue(coursewarePackage, "description"));
		System.out.println("name = " + ReflectionUtils.getFieldValue(coursewarePackage, "name"));
		System.out.println("picturePath = " + ReflectionUtils.getFieldValue(coursewarePackage, "picturePath"));
		System.out.println("realPicturePath" + ReflectionUtils.getFieldValue(coursewarePackage, "realPicturePath"));
		
		System.out.println("----------------------------------------------------");
	}
	
	/**
	 * set
	 */
	@Test  
	public void testSetFieldValue() {  
		CoursewarePackage coursewarePackage = getCoursewarePackage();
		
		System.out.println("id = " + ReflectionUtils.getFieldValue(coursewarePackage, "id"));  
		System.out.println("clickNum = " + ReflectionUtils.getFieldValue(coursewarePackage, "clickNum"));
		System.out.println("description = " + ReflectionUtils.getFieldValue(coursewarePackage, "description"));
		System.out.println("name = " + ReflectionUtils.getFieldValue(coursewarePackage, "name"));
		System.out.println("picturePath = " + ReflectionUtils.getFieldValue(coursewarePackage, "picturePath"));
		System.out.println("realPicturePath" + ReflectionUtils.getFieldValue(coursewarePackage, "realPicturePath"));
		
		System.out.println("----------------------------------------------------");
		
		ReflectionUtils.setFieldValue(coursewarePackage, "id", (long)2) ; 
		ReflectionUtils.setFieldValue(coursewarePackage, "clickNum", 2) ; 
		ReflectionUtils.setFieldValue(coursewarePackage, "description", "----") ; 
		ReflectionUtils.setFieldValue(coursewarePackage, "name", "....") ; 
		ReflectionUtils.setFieldValue(coursewarePackage, "picturePath", "/....") ; 
		ReflectionUtils.setFieldValue(coursewarePackage, "realPicturePath", "/...") ;
		
		
		System.out.println("id = " + ReflectionUtils.getFieldValue(coursewarePackage, "id"));  
		System.out.println("clickNum = " + ReflectionUtils.getFieldValue(coursewarePackage, "clickNum"));
		System.out.println("description = " + ReflectionUtils.getFieldValue(coursewarePackage, "description"));
		System.out.println("name = " + ReflectionUtils.getFieldValue(coursewarePackage, "name"));
		System.out.println("picturePath = " + ReflectionUtils.getFieldValue(coursewarePackage, "picturePath"));
		System.out.println("realPicturePath" + ReflectionUtils.getFieldValue(coursewarePackage, "realPicturePath"));
		
	}
	
	
	protected CoursewarePackage getCoursewarePackage(){
		
		CoursewarePackage coursewarePackage = new CoursewarePackage();
		coursewarePackage.setId((long) 1);
		coursewarePackage.setClickNum(1);
		coursewarePackage.setDescription("描述");
		coursewarePackage.setName("名称");
		coursewarePackage.setPicturePath("/upload");
		coursewarePackage.setRealPicturePath("/upload2");
		Property property = new Property();
		property.setId((long)1);
		property.setName("name");
		property.setSortCode("sortCode");
		property.setSortId(1);
		List<Property> properties = Lists.newArrayList();
		properties.add(property);
		coursewarePackage.setProperties(properties);
		return coursewarePackage;
	}

}

public class ReflectionUtils {

	/**
	 * 循环向上转型, 获取对象的 DeclaredMethod
	 * @param object : 子类对象
	 * @param methodName : 父类中的方法名
	 * @param parameterTypes : 父类中的方法参数类型
	 * @return 父类中的方法对象
	 */
	
	public static Method getDeclaredMethod(Object object, String methodName, Class<?> ... parameterTypes){
		Method method = null ;
		
		for(Class<?> clazz = object.getClass() ; clazz != Object.class ; clazz = clazz.getSuperclass()) {
			try {
				method = clazz.getDeclaredMethod(methodName, parameterTypes) ;
				return method ;
			} catch (Exception e) {
				//这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。
				//如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
			
			}
		}
		
		return null;
	}
	
	/**
	 * 直接调用对象方法, 而忽略修饰符(private, protected, default)
	 * @param object : 子类对象
	 * @param methodName : 父类中的方法名
	 * @param parameterTypes : 父类中的方法参数类型
	 * @param parameters : 父类中的方法参数
	 * @return 父类中方法的执行结果
	 */
	
	public static Object invokeMethod(Object object, String methodName, Class<?> [] parameterTypes,
			Object [] parameters) {
		//根据 对象、方法名和对应的方法参数 通过反射 调用上面的方法获取 Method 对象
		Method method = getDeclaredMethod(object, methodName, parameterTypes) ;
		
		//抑制Java对方法进行检查,主要是针对私有方法而言
		method.setAccessible(true) ;
		
			try {
				if(null != method) {
					
					//调用object 的 method 所代表的方法,其方法的参数是 parameters
					return method.invoke(object, parameters) ;
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		
		return null;
	}

	/**
	 * 循环向上转型, 获取对象的 DeclaredField
	 * @param object : 子类对象
	 * @param fieldName : 父类中的属性名
	 * @return 父类中的属性对象
	 */
	
	public static Field getDeclaredField(Object object, String fieldName){
		Field field = null ;
		
		Class<?> clazz = object.getClass() ;
		
		for(; clazz != Object.class ; clazz = clazz.getSuperclass()) {
			try {
				field = clazz.getDeclaredField(fieldName) ;
				return field ;
			} catch (Exception e) {
				//这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。
				//如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了
				
			} 
		}
	
		return null;
	}	
	
	/**
	 * 直接设置对象属性值, 忽略 private/protected 修饰符, 也不经过 setter
	 * @param object : 子类对象
	 * @param fieldName : 父类中的属性名
	 * @param value : 将要设置的值
	 */
	
	public static void setFieldValue(Object object, String fieldName, Object value){
	
		//根据 对象和属性名通过反射 调用上面的方法获取 Field对象
		Field field = getDeclaredField(object, fieldName) ;
		
		//抑制Java对其的检查
		field.setAccessible(true) ;
		
		try {
			//将 object 中 field 所代表的值 设置为 value
			 field.set(object, value) ;
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 直接读取对象的属性值, 忽略 private/protected 修饰符, 也不经过 getter
	 * @param object : 子类对象
	 * @param fieldName : 父类中的属性名
	 * @return : 父类中的属性值
	 */
	
	public static Object getFieldValue(Object object, String fieldName){
		
		//根据 对象和属性名通过反射 调用上面的方法获取 Field对象
		Field field = getDeclaredField(object, fieldName) ;
		
		//抑制Java对其的检查
		field.setAccessible(true) ;
		
		try {
			//获取 object 中 field 所代表的属性值
			return field.get(object) ;
			
		} catch(Exception e) {
			e.printStackTrace() ;
		}
		
		return null;
	}
}


你可能感兴趣的:(反射获取属性和方法)