---
title: "java反射demo"
author: "charsli"
date: "2013-05-25T00:53:05Z"
tags: []
categories: ["编程语言"]
slug: "java-demo"
draft: false
lastmod: "2013-05-25T00:53:05Z"
id: 1874946
url: "https://charsli.iteye.com/blog/1874946"
---

\[code="java"] /\** * \*/ 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 members=new ArrayList(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 String convertListToParam(List 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(); } } \[/code]