动态获取字段名或方法

public interface Adapter<T> {

	public abstract void setBean(T bean);

	public abstract T getBean();

	public abstract Method[] getBeanMethods();

	public abstract Field[] getBeanFields();

	public abstract Object invoke(Method m, Object... args);

	public abstract List getBeanGetterMethodList();

	public abstract List getBeanSetterMethodList();

	public abstract void process();
	
	public Excessive process(String getID,String getTitle);
	
	
}


public abstract class AbstractAdapter<T> implements Adapter<T> {

	protected T bean;
	
	private List<BeanProperties> beanGetterMethodList = null;

	private List<BeanProperties> beanSetterMethodList = null;


	public AbstractAdapter(T bean) {
	   this.bean = bean;
	}


	public Method[] getBeanMethods() {
	   return bean.getClass().getDeclaredMethods();
	}


	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;
	   }
	}


	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();
	     }
	    }
	   }
	}


	public List getBeanGetterMethodList() {
	   if (beanGetterMethodList == null) {
	    beanGetterMethodList = new ArrayList<BeanProperties>();
	    fillGetterMethod();
	   }
	   return beanGetterMethodList;
	}


	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();
	    }
	   }
	}


	private String firstWordToUpper(String word) {
	   String first = word.substring(0, 1);
	   return first.toUpperCase() + word.substring(1);
	}


	public List getBeanSetterMethodList() {
	   if (beanSetterMethodList == null) {
	    beanSetterMethodList = new ArrayList<BeanProperties>();
	    filSetterMethod();
	   }
	   return beanSetterMethodList;
	}


	public void setBean(T bean) {
	   this.bean = bean;
	}


	public T getBean() {
	   return bean;
	}
}


public class AdapterTest<T> extends AbstractAdapter<T> {
	public AdapterTest(T bean) {
		super(bean);
	}

	public void process() {
		List list = getBeanGetterMethodList();
		Iterator it = list.iterator();
		while (it.hasNext()) {
			BeanProperties bp = (BeanProperties) it.next();
			
		}
		it = getBeanSetterMethodList().iterator();
		while (it.hasNext()) {
			BeanProperties bp = (BeanProperties) it.next();
			if (bp.getFieldType().getName().endsWith("int"))
				
			if (bp.getFieldType().getName().endsWith("String"))
				
		}
		it = list.iterator();
		while (it.hasNext()) {
			BeanProperties bp = (BeanProperties) it.next();
			try {
				Object test = invoke(bp.getMethod(), null);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Excessive process(String getID, String getTitle) {
		List list = getBeanGetterMethodList();
		Iterator it = list.iterator();
		Excessive ex = new Excessive();
		try {
			while (it.hasNext()) {
				BeanProperties bp = (BeanProperties) it.next();
				if (bp.getMethodName().equals(getID)) {
					ex.setExID(invoke(bp.getMethod(), null).toString());
				}
				if (bp.getMethodName().equals(getTitle)) {
					ex.setTitle(invoke(bp.getMethod(), null).toString());
				}
			}
			return ex;
		} catch (Exception e) {
			return ex;
		}
	}

}


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;
}
}


public class Excessive implements java.io.Serializable{
  
	private static final long serialVersionUID = 1L;
    private String exID;
    private String title;

	public String getExID() {
		return exID;
	}

	public void setExID(String exID) {
		this.exID = exID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}


public List getBusinessTypeList(HttpServletRequest request) {
		String language = request.getLocale().toString().split("_")[0];
		language = language.substring(0, 1).toUpperCase()+language.substring(1);
		String HQL ="from HoDictionary hod where hod.dictId=8";
		List list = queryByHQL(HQL);
		List li = new ArrayList();
		AdapterTest hod = null;
		for(int i=0; i<list.size(); i++ ){
			try{
				HoDictionary code = (HoDictionary)list.get(i);
				  hod = new AdapterTest(code);
			}catch(Exception e){
				break;
			}
			Excessive ss = hod.process("getLemmaId", "get"+language+"Lemma");
			li.add(ss);
		}
		return li;
	}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值