---
title: "动态获取字段名或方法"
author: "blaiu"
date: "2009-08-04T08:53:27Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-12-05T02:30:04Z"
id: 439053
url: "https://blaiu.iteye.com/blog/439053"
---

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

```