`

java反射demo

 
阅读更多

/**
 * 
 */
package *;

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

import *.Member;

/**
 * @author thinkpad
 *
 */
public class ReflectListToString {

	/**
	 * @param args
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 */
	public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {

		List<Member> members=new ArrayList<Member>(0);
		Member m=new Member(1l);
		m.setAccount("aa");
		m.setBankName("fdaf");
		members.add(m);
		members.add(m);
		StringBuffer sb=new StringBuffer();
		for(Member m1:members ){
			String name=m.getClass().getSimpleName().substring(0,1).toLowerCase()+m.getClass().getSimpleName().substring(1);
			Class classType=m.getClass();
			Field[] fields= m1.getClass().getDeclaredFields();
			
			for(Field field:fields){
				String fieldName=field.getName();   
	            String stringLetter=fieldName.substring(0, 1).toUpperCase();   
	               
	            //获得相应属性的getXXX和setXXX方法名称   
	            String getName="get"+stringLetter+fieldName.substring(1);   
	            String setName="set"+stringLetter+fieldName.substring(1);   
	               
	            //获取相应的方法   
	            Method getMethod=classType.getMethod(getName, new Class[]{});   
	            Method setMethod=classType.getMethod(setName, new Class[]{field.getType()});   
	               
	            //调用源对象的getXXX()方法   
	            Object value=getMethod.invoke(m, new Object[]{});
	            String p=name+"."+fieldName+"="+value;
	            sb.append(p);
	            sb.append("&amp;");
			}
			
		}
		System.out.println(sb.toString());
	}
	
	/**
	 * @param args
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static <T> String convertListToParam(List<T> list) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
		StringBuffer sb=new StringBuffer();
		for(T t:list ){
			String name=t.getClass().getSimpleName().substring(0,1).toLowerCase()+t.getClass().getSimpleName().substring(1);
			Class classType=t.getClass();
			Field[] fields= t.getClass().getDeclaredFields();
			
			for(Field field:fields){
				String fieldName=field.getName();   
	            String stringLetter=fieldName.substring(0, 1).toUpperCase();   
	               
	            //获得相应属性的getXXX和setXXX方法名称   
	            String getName="get"+stringLetter+fieldName.substring(1);   
	            //String setName="set"+stringLetter+fieldName.substring(1);   
	               
	            //获取相应的方法   
				Method getMethod=classType.getMethod(getName, new Class[]{});   
				//Method setMethod=classType.getMethod(setName, new Class[]{field.getType()});   
	               
	            //调用源对象的getXXX()方法   
	            Object value=getMethod.invoke(t, new Object[]{});   
	            String p=name+"."+fieldName+"="+value;
	            sb.append(p);
	            sb.append("&amp;");
			}
		}
		
		return sb.toString();
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics