java反射简单demo


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

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

		return sb.toString();
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值