2012-05-26 11 views
5

私はBeanを持っていますが、リストなしでBeanのすべてのプロパティをリストする方法はありますか?Beanのプロパティをリストする方法

いくつかのBeanは、便利なToString()メソッドをオーバーライドしています。このメソッドをオーバーライドしない豆はどうですか?

+0

のtoString()メソッドです。 Beanプロパティではありません。あなたは正確に何をしたいですか? java.lang.Class javadocをブラウズしましたか? –

+0

これがEclipse内にあり、生成されたコードに満足している場合は、Eclipseに自動生成させることを検討してください。右クリック - >ソースメニューを見てください。 –

答えて

2

リフレクションを使用できます。 (ブールゲッターを覚えている「isProperty」である)

コードは次のように見ることができクラスから宣言されたフィールドを取り、彼らはセッターとゲッターを持っている場合、それは彼らがプライベートチェックです:

List<String> properties = new ArrayList<String>(); 
Class<?> cl = MyBean.class; 

// check all declared fields 
for (Field field : cl.getDeclaredFields()) { 

    // if field is private then look for setters/getters 
    if (Modifier.isPrivate(field.getModifiers())) { 

     // changing 1st letter to upper case 
     String name = field.getName(); 
     String upperCaseName = name.substring(0, 1).toUpperCase() 
       + name.substring(1); 
     // and have getter and setter 
     try { 
      String simpleType = field.getType().getSimpleName(); 
      //for boolean property methods should be isProperty and setProperty(propertyType) 
      if (simpleType.equals("Boolean") || simpleType.equals("boolean")) { 
       if ((cl.getDeclaredMethod("is" + upperCaseName) != null) 
         && (cl.getDeclaredMethod("set" + upperCaseName, 
           field.getType()) != null)) { 
        properties.add(name); 
       } 
      } 
      //for not boolean property methods should be getProperty and setProperty(propertyType) 
      else { 
       if ((cl.getDeclaredMethod("get" + upperCaseName) != null) 
         && (cl.getDeclaredMethod("set" + upperCaseName, 
           field.getType()) != null)) { 
        properties.add(name); 
       } 
      } 
     } catch (NoSuchMethodException | SecurityException e) { 
      // if there is no method nothing bad will happen 
     } 
    } 
} 
for (String property:properties) 
    System.out.println(property); 
+1

それはたくさんのコードのようです。どのような方法でもプロセスを簡素化できますか? –

+1

'java.bean'パッケージには簡単なクラスがいくつかありますが、使いたいかどうかはわかりません。例えば、あなたは 'class Person {private int height; public getHeight(){..} public setHeight(int h){..} boolean isTall(){戻り高さ> 180;}} '?私のアプローチを使用すると、 'height'しか得られませんが、これらのヘルパークラスを使用する場合、すべてのクラスに' getClass() 'メソッドがあるので、' height''tall'だけでなく 'class'も取得します。 – Pshemo

+0

ありがとう、私は、GSONからの直列化解除後にBeanのすべてのプロパティを解析することに興味があります。これは私のAndroidアプリ用です。 –

4

次のようにあなたがBeanIntrospection経由BeanInfoを使用することができます。

Object o = new MyBean(); 
try { 
    BeanInfo bi = Introspector.getBeanInfo(MyBean.class); 
    PropertyDescriptor[] pds = bi.getPropertyDescriptors(); 
    for (int i=0; i<pds.length; i++) { 
     // Get property name 
     String propName = pds[i].getName(); 

     // Get the value of prop1 
     Expression expr = new Expression(o, "getProp1", new Object[0]); 
     expr.execute(); 
     String s = (String)expr.getValue(); 
    } 
    // class, prop1, prop2, PROP3 
} catch (java.beans.IntrospectionException e) { 
} 

それとも、次のいずれかの方法を用いた反射法で行くことができた:

  1. は、通過するすべてのパラメータなしのgetXXX()メソッドを取得します。 getDeclaredMethodsをトラバースしてそれらをトラバースします
  2. getDeclaredFields()を使用してすべてのフィールドを取得し、それらをトラバースします(気にする場合はBean仕様に準拠しません)
0

あなたは、Beanクラスを変更する必要があることをせずに、Beanクラスを伴うことがあり、クラスBeanInfoに興味があるかもしれません。これは、多くのGUIビルダーで、Beanのプロパティーを表示するために使用されますが、GUI以外の用途もあります。

関連する問題