English 中文(简体)
如何列出豆的属性
原标题:How to list the bean s properties
  • 时间:2012-05-26 09:03:39
  •  标签:
  • java

我有一个豆子,有没有办法 列出豆子所有的财产 没有列表一个一个?

有些豆子超过 ToString () 方法, 方便。 豆子如何不超越此方法?

最佳回答

您可以使用反射。 从课堂上取取声明的字段, 它们是私人检查是否有设置器和抓取器( 记住布林拿取器是“ Property ” ) 。

代码可以像这样看 :

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);
问题回答

http://docs.oracle.com/javase/7/docs/api/java/beans/BeanInfo.html" rel=“noreferr”>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. Get all no-parameter getXXX() methods through getDeclaredMethods and traverse them
  2. Get all fields using getDeclaredFields() and traverse them (Not compliant with Bean spec, if you care about it)

http://commons.apache.org/lang/api-2.5/org/pache/commons/lang/buildinger/ReflectionTo StringBuilder.html" rel=“nofollow” >ReflectionTostringBuilder

您可能会对< a href=> http://docs. oracle.com/javase/7/docs/api/java/beans/ BeanInfo.html' 感兴趣, 这个班级可能伴随豆类, 无需改变豆类。 许多图形用户界面构建者使用它来显示豆的属性, 但也有其非GUI的用途 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签