Java 设计模式 动态调用 JavaBean 的 setter 和 getter

/**
* 这个是接口类
*/

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

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* <p>
* Created Time:2008-9-13 上午01:48:41
* </p>
*
*
* @version 1.0
*/
public interface Adapter<T> {
/**
* 设置 JavaBean
*
*/
public abstract void setBean(T bean);

/**
* 取得 JavaBean 实体
*
* @return
*/
public abstract T getBean();

/**
* 取得 JavaBean 的全部方法
*
* @return
*/
public abstract Method[] getBeanMethods();

/**
* 取得 JavaBean 的字段
*
* @return
*/
public abstract Field[] getBeanFields();

/**
* 方法代理
*
* @param bean
* @param args
*/
public abstract Object invoke(Method m, Object... args);

/**
* 取得 JavaBean 的 getter 方法的容器类
*
* @return
*/
public abstract List getBeanGetterMethodList();

/**
* 取得 JavaBean 的 setter 方法的容器类
*
* @return
*/
public abstract List getBeanSetterMethodList();

/**
* 处理这个 JavaBean 的过程
*
*/
public abstract void process();
}

/**
* 这个是接口抽象类
*/

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
* <p>
* Title:继承这个抽象类的类就可以写实际的 process 处理过程了
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* <p>
* Created Time:2008-9-13 上午01:54:36
* </p>
*
*
* @version 1.0
*/
public abstract class AbstractAdapter<T> implements Adapter<T> {
/**
* 使用正则表达式来过滤掉方法中多于的东西
*/
// private static Pattern p = Pattern.compile("//w+//.");
// private static Pattern lp = Pattern.compile("[a-z]+//w");
/**
* JavaBean 实体
*/
protected T bean;

// 方法属性容器
private List<BeanProperties> beanGetterMethodList = null;

private List<BeanProperties> beanSetterMethodList = null;

/**
* 构造一个方法
*/
public AbstractAdapter(T bean) {
   this.bean = bean;
}

/**
* 取得 JavaBean 的方法
*/
public Method[] getBeanMethods() {
   return bean.getClass().getDeclaredMethods();
}

/**
* 取得 JavaBean 的字段
*/
public Field[] getBeanFields() {
   return bean.getClass().getDeclaredFields();
}

/**
* 方法的代理
*/
public Object invoke(Method m, Object... args) {
   try {
    return m.invoke(bean, args);
   } catch (InvocationTargetException ite) {
    ite.printStackTrace();
    return null;
   } catch (IllegalAccessException iae) {
    iae.printStackTrace();
    return null;
   }
}

/**
* 获取该类的 getter 方法
*/
private void fillGetterMethod() {
   for (Method m : getBeanMethods()) {
    if (m.getName().startsWith("get")) {
     try {
      BeanProperties bp = new BeanProperties();
      bp.setMethod(bean.getClass().getMethod(m.getName(), null));
      bp.setMethodName(m.getName());
      bp.setReturnType(m.getReturnType());
      bp.setFieldType(m.getReturnType());
      beanGetterMethodList.add(bp);
     } catch (NoSuchMethodException e) {
      e.printStackTrace();
     }
    }
   }
}

/**
* 取得 JavaBean 的 getter 方法的具体属性
*/
public List getBeanGetterMethodList() {
   if (beanGetterMethodList == null) {
    beanGetterMethodList = new ArrayList<BeanProperties>();
    fillGetterMethod();
   }
   return beanGetterMethodList;
}

/**
* 获取该类的 setter 方法
*/
private void filSetterMethod() {
   for (Field f : getBeanFields()) {
    try {
     BeanProperties bp = new BeanProperties();
     bp.setMethodName("set" + firstWordToUpper(f.getName()));
     bp.setMethod(getBean().getClass().getMethod(bp.getMethodName(),
       f.getType()));
     bp.setReturnType(null);
     bp.setFieldType(f.getType());
     beanSetterMethodList.add(bp);
    } catch (NoSuchMethodException e) {
     e.printStackTrace();
    }
   }
}

/**
* 将字母的第一个字符转换成大写
*
* @param word
* @return
*/
private String firstWordToUpper(String word) {
   String first = word.substring(0, 1);
   return first.toUpperCase() + word.substring(1);
}

/**
* 取得 JavaBean 的 setter 方法的具体属性
*/
public List getBeanSetterMethodList() {
   if (beanSetterMethodList == null) {
    beanSetterMethodList = new ArrayList<BeanProperties>();
    filSetterMethod();
   }
   return beanSetterMethodList;
}

/**
* 设置要处理的 JavaBean
*/
public void setBean(T bean) {
   this.bean = bean;
}

/**
* 取得 JavaBean 实体
*/
public T getBean() {
   return bean;
}
}

/**
* 这个是 JavaBean 方法的属性类
*/

import java.lang.reflect.Method;

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* <p>
* Created Time:2008-9-13 上午02:08:27
* </p>
*
*
* @version 1.0
*/
public class BeanProperties {
/**
* 返回类型
*/
private Class returnType;

/**
* 方法
*/
private Method method;

/**
* 方法名字
*/
private String methodName;

/**
* 字段的类型
*/
private Class fieldType;

public Method getMethod() {
   return method;
}

public void setMethod(Method method) {
   this.method = method;
}

public String getMethodName() {
   return methodName;
}

public void setMethodName(String methodName) {
   this.methodName = methodName;
}

public Class getReturnType() {
   return returnType;
}

public void setReturnType(Class returnType) {
   this.returnType = returnType;
}

public Class getFieldType() {
   return fieldType;
}

public void setFieldType(Class fieldType) {
   this.fieldType = fieldType;
}
}

/**
* 测试类
*/

import java.util.Iterator;
import java.util.List;

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* <p>
* Created Time:2008-9-13 上午03:01:50
* </p>
*
*
* @version 1.0
*/
public class AdapterTest<T> extends AbstractAdapter<T> {
public AdapterTest(T bean) {
   super(bean);
}

public void process() {
   List list = getBeanGetterMethodList();
   System.out.println("*** 修改前 ***");
   Iterator it = list.iterator();
   while (it.hasNext()) {
    BeanProperties bp = (BeanProperties) it.next();
    System.out.println("动态调用 JavaBean 的 " + bp.getMethodName()
      + " 方法的测试:" + invoke(bp.getMethod(), null));
   }

   System.out.println("*** 修改 ***");
   it = getBeanSetterMethodList().iterator();
   while (it.hasNext()) {
    BeanProperties bp = (BeanProperties) it.next();
    if (bp.getFieldType().getName().endsWith("int"))
     System.out.println("动态调用 JavaBean 的 " + bp.getMethodName()
       + " 方法的测试:" + invoke(bp.getMethod(), 369));
    if (bp.getFieldType().getName().endsWith("String"))
     System.out.println("动态调用 JavaBean 的 " + bp.getMethodName()
       + " 方法的测试:" + invoke(bp.getMethod(), "修改了属性"));
   }

   System.out.println("*** 修改后 ***");
   it = list.iterator();
   while (it.hasNext()) {
    BeanProperties bp = (BeanProperties) it.next();
    try {
     Object test = invoke(bp.getMethod(), null);
     System.out.println("动态调用 JavaBean 的 " + bp.getMethodName()
       + " 方法的测试:" + test);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
}
}

/**
* 测试类主函数
*/

import test.com.liangst.setter.Product;

/**
* <p>
* Title:
* </p>
*
* <p>
* Description:
* </p>
*
* <p>
* Copyright (c) 2008
* </p>
*
* <p>
* Company:
* </p>
*
* <p>
* Created Time:2008-9-13 上午03:11:54
* </p>
*
*
* @version 1.0
*/
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
   Product p = new Product();
   p.setId(123);
   p.setName("测试商品");
   p.setDescn("测试商品的描述");
   Adapter<Product> a = new AdapterTest<Product>(p);
   a.process();
   a.getBeanSetterMethodList();
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值