「Java」PropertyUtils.describeメソッドでクラスBeanの全フィールドを取得するサンプル
構文
1.Set<K> keySet()
このマップに含まれるキーのSetビューを返します。セットはマップと連動しているので、マップに対する変更はセットに反映され、
また、セットに対する変更はマップに反映されます。
2.PropertyUtils.describe()
BeanをMapに変換します。
使用例
1.EmpDataクラスの定義
package com.arkgame.study.it;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import org.apache.commons.beanutils.PropertyUtils;
public class EmpData implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private Double price;
// constructor
public EmpData(String userName, Double price) {
this.userName = userName;
this.price = price;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Iterator<?> getNames(EmpData obj) {
try {
return PropertyUtils.describe(obj).keySet().iterator();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
}
package com.arkgame.study.it;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import org.apache.commons.beanutils.PropertyUtils;
public class EmpData implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private Double price;
// constructor
public EmpData(String userName, Double price) {
this.userName = userName;
this.price = price;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Iterator<?> getNames(EmpData obj) {
try {
return PropertyUtils.describe(obj).keySet().iterator();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
}
package com.arkgame.study.it; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.Iterator; import org.apache.commons.beanutils.PropertyUtils; public class EmpData implements Serializable { private static final long serialVersionUID = 1L; private String userName; private Double price; // constructor public EmpData(String userName, Double price) { this.userName = userName; this.price = price; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public Iterator<?> getNames(EmpData obj) { try { return PropertyUtils.describe(obj).keySet().iterator(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } }
2.getNames()メソッドでクラスの全フィールドを取得
package com.arkgame.study.it;
import java.util.Iterator;
public class EmpDataBeanCopy {
public static void main(String[] args) {
EmpData empData = new EmpData("employee", 21.35);
Iterator<?> prop;
prop = empData.getNames(empData);
System.out.println("クラスのフィールド下記:");
while (prop.hasNext()) {
System.out.println(prop.next());
}
}
}
package com.arkgame.study.it;
import java.util.Iterator;
public class EmpDataBeanCopy {
public static void main(String[] args) {
EmpData empData = new EmpData("employee", 21.35);
Iterator<?> prop;
prop = empData.getNames(empData);
System.out.println("クラスのフィールド下記:");
while (prop.hasNext()) {
System.out.println(prop.next());
}
}
}
package com.arkgame.study.it; import java.util.Iterator; public class EmpDataBeanCopy { public static void main(String[] args) { EmpData empData = new EmpData("employee", 21.35); Iterator<?> prop; prop = empData.getNames(empData); System.out.println("クラスのフィールド下記:"); while (prop.hasNext()) { System.out.println(prop.next()); } } }
3.クラスのフィールド下記:
price
userName