commons-beanutils-1.7.0.zipをダウンロードします
commons-beanutils.jar commons-beanutils-core.jar commons-beanutils-bean-collections.jarをクラスパスに加えます
package com.snail; public class HumanInfoBean { private String name; private String age; private String sex; private HumanInfoBean[] children; public HumanInfoBean() { super(); } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public HumanInfoBean[] getChildren() { return children; } public void setChildren(HumanInfoBean[] children) { this.children = children; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String sayHello( String str ){ return "Hello " + str; } }
package com.snail; import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException; public class BeanUtilExam { private BeanUtilExam() { super(); } public static void main(String[] args) { HumanInfoBean parent = new HumanInfoBean(); try { PropertyUtils.setProperty(parent, "name", "taro"); PropertyUtils.setProperty(parent, "age", "30"); PropertyUtils.setProperty(parent, "sex", "male"); System.out.println(PropertyUtils.getProperty(parent, "name")); System.out.println(PropertyUtils.getProperty(parent, "age")); System.out.println(PropertyUtils.getProperty(parent, "sex")); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
taro 30 male
package com.snail; import org.apache.commons.beanutils.PropertyUtils; import java.lang.reflect.InvocationTargetException; public class BeanUtilExam { private BeanUtilExam() { super(); } /** * @param args */ public static void main(String[] args) { HumanInfoBean parent = new HumanInfoBean(); HumanInfoBean child1 = new HumanInfoBean(); HumanInfoBean child2 = new HumanInfoBean(); HumanInfoBean child3 = new HumanInfoBean(); parent.setChildren(new HumanInfoBean[] { child1, child2, child3 }); try { PropertyUtils.setProperty(parent, "children[0].name", "hanako"); PropertyUtils.setProperty(parent, "children[1].name", "miyuki"); PropertyUtils.setProperty(parent, "children[2].name", "tomoko"); System.out.println(PropertyUtils.getProperty(parent,"children[0].name")); System.out.println(PropertyUtils.getProperty(parent,"children[1].name")); System.out.println(PropertyUtils.getProperty(parent,"children[2].name")); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
hanako miyuki tomoko
package com.snail; import java.lang.reflect.InvocationTargetException; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; public class BeanUtilExam { private BeanUtilExam() { super(); } public static void main(String[] args) { HumanInfoBean human = new HumanInfoBean(); human.setAge("10"); human.setName("jiro"); human.setSex("male"); try { Map map = PropertyUtils.describe(human); System.out.println(map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
{sex=male, age=10, class=class com.snail.HumanInfoBean, name=jiro, children=null}
package com.snail; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; public class BeanUtilExam { private BeanUtilExam() { super(); } public static void main(String[] args) { Map<String,String> parent = new HashMap<String,String>(); try { PropertyUtils.setProperty(parent, "name", "taro"); PropertyUtils.setProperty(parent, "age", "30"); PropertyUtils.setProperty(parent, "sex", "male"); System.out.println(PropertyUtils.getProperty(parent, "name")); System.out.println(PropertyUtils.getProperty(parent, "age")); System.out.println(PropertyUtils.getProperty(parent, "sex")); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
taro 30 male
package com.snail; import java.lang.reflect.InvocationTargetException; import org.apache.commons.beanutils.MethodUtils; public class BeanUtilExam { private BeanUtilExam() { super(); } /** * @param args */ public static void main(String[] args) { try { Object parent = Class.forName("com.snail.HumanInfoBean").newInstance(); System.out.println(MethodUtils.invokeMethod(parent, "sayHello", new Object[] { "World" }, new Class[] { String.class })); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
Hello World
Class class = Class.forName( "${クラス名}" ); Constructor constructor = class.getConstructor( new Class[]{ String.class , Integer.class } ); Object object = constructor.newInstance("ALPHA",1);
http://jakarta.apache.org/commons/beanutils/