「Java」PropertyUtils.describeメソッドでクラスBeanの全フィールドを取得する方法

説明
describe(bean)
戻り値:Map
bean内の各値をMapに入れて返します。
Javaコード
1.Userクラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
public class User {
private Integer age = null;
private String userId = null;
private String addr = null;
public User(Integer age, String userId, String addr) {
super();
this.age = age;
this.userId = userId;
this.addr = addr;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.arkgame.study; public class User { private Integer age = null; private String userId = null; private String addr = null; public User(Integer age, String userId, String addr) { super(); this.age = age; this.userId = userId; this.addr = addr; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } }
package com.arkgame.study;

public class User {
      private Integer age = null;
      private String userId = null;
      private String addr = null;

      public User(Integer age, String userId, String addr) {
            super();
            this.age = age;
            this.userId = userId;
            this.addr = addr;
      }

      public Integer getAge() {
            return age;
      }

      public void setAge(Integer age) {
            this.age = age;
      }

      public String getUserId() {
            return userId;
      }

      public void setUserId(String userId) {
            this.userId = userId;
      }

      public String getAddr() {
            return addr;
      }

      public void setAddr(String addr) {
            this.addr = addr;
      }

}

2.PropertyUtils.describe()でBeanのフィールドを取得

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
public class DescibeMapDemo {
public static void main(String[] args)
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
User userA = new User(21, "user001", "this is a test addr1");
User userB = new User(31, "user002", "this is a test addr2");
System.out.println("**********オブジェクトuserA結果************");
Map<String, Object> mpA = (Map<String, Object>) PropertyUtils.describe(userA);
for (Map.Entry<String, Object> tt : mpA.entrySet()) {
System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue());
}
System.out.println("**********オブジェクトuserB結果************");
Map<String, Object> mpB = (Map<String, Object>) PropertyUtils.describe(userB);
for (Map.Entry<String, Object> tt : mpB.entrySet()) {
System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue());
}
}
}
package com.arkgame.study; import java.lang.reflect.InvocationTargetException; import java.util.Map; import org.apache.commons.beanutils.PropertyUtils; public class DescibeMapDemo { public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { User userA = new User(21, "user001", "this is a test addr1"); User userB = new User(31, "user002", "this is a test addr2"); System.out.println("**********オブジェクトuserA結果************"); Map<String, Object> mpA = (Map<String, Object>) PropertyUtils.describe(userA); for (Map.Entry<String, Object> tt : mpA.entrySet()) { System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue()); } System.out.println("**********オブジェクトuserB結果************"); Map<String, Object> mpB = (Map<String, Object>) PropertyUtils.describe(userB); for (Map.Entry<String, Object> tt : mpB.entrySet()) { System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue()); } } }
package com.arkgame.study;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

public class DescibeMapDemo {

      public static void main(String[] args)
                  throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
            User userA = new User(21, "user001", "this is a test addr1");
            User userB = new User(31, "user002", "this is a test addr2");

            System.out.println("**********オブジェクトuserA結果************");
            Map<String, Object> mpA = (Map<String, Object>) PropertyUtils.describe(userA);
            for (Map.Entry<String, Object> tt : mpA.entrySet()) {
                  System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue());
            }
            System.out.println("**********オブジェクトuserB結果************");
            Map<String, Object> mpB = (Map<String, Object>) PropertyUtils.describe(userB);
            for (Map.Entry<String, Object> tt : mpB.entrySet()) {
                  System.out.println("属性: " + tt.getKey() + " 値:" + tt.getValue());
            }

      }

}

実行結果

**********オブジェクトuserA結果************
属性: addr 値:this is a test addr1
属性: userId 値:user001
属性: age 値:21
**********オブジェクトuserB結果************
属性: addr 値:this is a test addr2
属性: userId 値:user002
属性: age 値:31

Java

Posted by arkgame