package main.java.reflection.model; import java.util.ArrayList; import java.util.List; public class Demo { private String id; private String name; private int age; private List<String> list = new ArrayList<String>(); public Demo(){} public Demo(String id, String name, int age, List<String> list) { super(); this.id = id; this.name = name; this.age = age; this.list = list; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } @Override public String toString() { return "[id="+id+";name="+name+";age="+age+"]"; } }
package main.java.reflection; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.List; import main.java.reflection.model.Demo; public class TestDemo { public static void main(String[] args) throws Exception { TestDemo td = new TestDemo(); td.test(); td.testField(); td.testMethodSet(); td.testMethodGet(); td.test2(); td.test4(); } /** * Title:test * Description:范型基础 */ public void test() { Demo d= new Demo(); System.out.println(d.getClass().getName()); System.out.println("count of Demo's constructor:"+d.getClass().getConstructors().length); for (Constructor<?> c : d.getClass().getDeclaredConstructors()) { System.out.println(c); } System.out.println("所以方法:"); for (Method m: d.getClass().getMethods()) { //所以的方法(包括继承过来的) System.out.println(m.getName()); } System.out.println("自定义的方法:"); for (Method m: d.getClass().getDeclaredMethods()) { //自定义的方法 System.out.println(m.getName()); } System.out.println("属性:"); for (Field f : d.getClass().getDeclaredFields()) { if(f.getType().toString().endsWith("java.lang.String")){ System.out.println("..."+f.getType()+":"+Modifier.toString(f.getModifiers())); }; System.out.println(f.getName()+":"+f.getType().getSimpleName()+"("+f.getType()+")"); } } /** * Title:testField * Description:属性赋值 */ public void testField() throws Exception { Class<?> demo = Class.forName("main.java.reflection.model.Demo"); Object obj = demo.newInstance(); Field field = demo.getDeclaredField("name"); field.setAccessible(true); field.set(obj, "xiao"); System.out.println(field.get(obj)); } /** * Title:testMethodSet * Description:利用反射执行对象的set方法 */ public void testMethodSet() throws Exception { Class<?> demo = Class.forName("main.java.reflection.model.Demo"); Demo obj = (Demo) demo.newInstance(); Method m = demo.getMethod("setId",String.class); m.invoke(obj, "11"); System.out.println(obj.getId()); } /** * Title:testMethodGet * Description:利用反射执行对象的get方法 */ public void testMethodGet() throws Exception { Class<?> demo = Class.forName("main.java.reflection.model.Demo"); Demo obj = (Demo) demo.newInstance(); Method m = demo.getMethod("getId"); if(m.invoke(obj)== null); System.out.println("...."); } /** * Title:test2 * Description:将对象中为String类型的属性对应的数据全部去掉首尾空格 */ public void test2() throws Exception{ List<Demo> dList = new ArrayList<Demo>(); List<String> strList = new ArrayList<String>(); dList.add(new Demo(" 1 "," Java ",11,strList)); dList.add(new Demo("2 "," Ruby",22,strList)); dList.add(new Demo(" 3","Ada ",33,strList)); for (Demo demo : dList) { System.out.println(demo.toString()); } for (Demo demo : dList) { Method m = null; String tmp = ""; for (Field f : demo.getClass().getDeclaredFields()) { if(f.getType().toString().endsWith("java.lang.String")){ m = demo.getClass().getMethod("get"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1)); if(m.invoke(demo) != null) tmp = m.invoke(demo).toString(); else tmp = ""; m = demo.getClass().getMethod("set"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1),String.class); m.invoke(demo, tmp.trim()); }; } } for (Demo demo : dList) { System.out.println(demo.toString()); } } /** * Title:test4 * Description:使用范型将对象中为String类型的属性对应的数据全部去掉首尾空格 */ public void test4() throws Exception{ CommonBean<Demo> cb = new CommonBean<Demo>(); List<CommonBean<Demo>> dList = new ArrayList<CommonBean<Demo>>(); List<String> strList = new ArrayList<String>(); dList.add(new CommonBean<Demo>(new Demo(" 1 "," Java ",11,strList))); dList.add(new CommonBean<Demo>(new Demo("2 ",null,22,strList))); dList.add(new CommonBean<Demo>(new Demo(" 3","Ada ",33,strList))); dList = cb.getListOfStringTrim(dList); for (CommonBean<Demo> cm : dList) { System.out.println(cm.getObj().toString()); } } }
package main.java.reflection; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.List; public class CommonBean<T> { T obj; public CommonBean(){ } public CommonBean(T obj) { this.obj = obj; } public T getObj() { return obj; } public void setObj(T obj) { this.obj = obj; } public List<CommonBean<T>> getListOfStringTrim(List<CommonBean<T>> list) throws Exception{ T obj = null; for (CommonBean<T> cm : list) { obj = cm.getObj(); Method m = null; String tmp = ""; for (Field f : obj.getClass().getDeclaredFields()) { if(f.getType().toString().endsWith("java.lang.String")){ m = obj.getClass().getMethod("get"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1)); if(m.invoke(obj) != null) tmp = m.invoke(obj).toString(); else tmp = ""; m = obj.getClass().getMethod("set"+f.getName().substring(0,1).toUpperCase()+f.getName().substring(1),String.class); m.invoke(obj, tmp.trim()); }; } } return list; } }