/**
* 这个是接口类
*/
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();
}
}