我有一个豆子,有没有办法 列出豆子所有的财产 没有列表一个一个?
有些豆子超过 ToString () 方法, 方便。 豆子如何不超越此方法?
我有一个豆子,有没有办法 列出豆子所有的财产 没有列表一个一个?
有些豆子超过 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) {
}
也可以使用下列方法之一,采用反射法:
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的用途 。
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...