通过PropertyDescriptor反映射调用set和get方法
来源:互联网 发布:中国跨境电子商务数据 编辑:程序博客网 时间:2024/06/08 18:10
- package com.zhoushun;
- import java.lang.reflect.Method;
- import java.lang.reflect.Field;
- import java.beans.PropertyDescriptor;
-
- public class PropertyUtil {
- @SuppressWarnings("unchecked")
- public static PropertyDescriptor getPropertyDescriptor(Class clazz, String propertyName) {
- StringBuffer sb = new StringBuffer();
- Method setMethod = null;
- Method getMethod = null;
- PropertyDescriptor pd = null;
- try {
- Field f = clazz.getDeclaredField(propertyName);
- if (f!= null) {
-
- String methodEnd = propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
- sb.append("set" + methodEnd);
- setMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ f.getType() });
- sb.delete(0, sb.length());
- sb.append("get" + methodEnd);
-
- getMethod = clazz.getDeclaredMethod(sb.toString(), new Class[]{ });
-
- pd = new PropertyDescriptor(propertyName, getMethod, setMethod);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
-
- return pd;
- }
-
- @SuppressWarnings("unchecked")
- public static void setProperty(Object obj,String propertyName,Object value){
- Class clazz = obj.getClass();
- PropertyDescriptor pd = getPropertyDescriptor(clazz,propertyName);
- Method setMethod = pd.getWriteMethod();
- try {
- setMethod.invoke(obj, new Object[]{value});
- }catch (Exception e){
- e.printStackTrace();
- }
- }
-
- @SuppressWarnings("unchecked")
- public static Object getProperty(Object obj, String propertyName){
- Class clazz = obj.getClass();
- PropertyDescriptor pd = getPropertyDescriptor(clazz,propertyName);
- Method getMethod = pd.getReadMethod();
- Object value =null ;
- try {
- value = getMethod.invoke(clazz, new Object[]{});
- } catch (Exception e) {
- e.printStackTrace();
- }
- return value;
- }
- }
2
- public boolean setValue(Object objSet, Object objGet)
- throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException
- {
- boolean flag = true;
- Field fields[] = objSet.getClass().getDeclaredFields();
- String value = "";
- String fieldNameGet = "";
- List list = new ArrayList();
- Field afield[];
- int j = (afield = fields).length;
- for(int i = 0; i < j; i++)
- {
- Field field = afield[i];
- String fieldName = field.getName();
- fieldNameGet = xmlToModel(fieldName);
- if(!"error".equals(fieldNameGet))
- {
- PropertyDescriptor pd = new PropertyDescriptor(fieldNameGet, objGet.getClass());
- Method getMethod = pd.getReadMethod();
- value = String.valueOf(getMethod.invoke(objGet, new Object[0]));
- boolean checkResult = returnMessage.checkValue(value, fieldNameGet);
- if(checkResult)
- {
- PropertyDescriptor pd1 = new PropertyDescriptor(fieldName, objSet.getClass());
- Method setMethod = pd1.getWriteMethod();
- setMethod.invoke(objSet, new Object[] {
- field.getType().getConstructor(new Class[] {
- java/lang/String
- }).newInstance(new Object[] {
- value
- })
- });
- } else
- {
- flag = checkResult;
- }
- }
- }
-
- return flag;
- }
3
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- public class ReflectTest {
-
- public static void main(String[] args) throws Exception {
- Class clazz = Class.forName("TaskProvidePropsList");
- Object obj = clazz.newInstance();
- Field[] fields = clazz.getDeclaredFields();
-
- for(Field f : fields) {
- PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
- Method wM = pd.getWriteMethod();
- wM.invoke(obj, 2);
- }
-
- for(Field f : fields) {
- PropertyDescriptor pd = new PropertyDescriptor(f.getName(), clazz);
- Method rM = pd.getReadMethod();
- Integer num = (Integer) rM.invoke(obj);
- System.out.println(num);
- }
- }
- }
0 0