You can use reflection. Take the declared fields from the class, these are private checks if they have setters and getters (remember that boolean getter is isproperty)
The code might look like this:
List<String> properties = new ArrayList<String>();
Class<?> cl = MyBean.class;
for (Field field : cl.getDeclaredFields()) {
if (Modifier.isPrivate(field.getModifiers())) {
String name = field.getName();
String upperCaseName = name.substring(0, 1).toUpperCase()
+ name.substring(1);
try {
String simpleType = field.getType().getSimpleName();
if (simpleType.equals("Boolean") || simpleType.equals("boolean")) {
if ((cl.getDeclaredMethod("is" + upperCaseName) != null)
&& (cl.getDeclaredMethod("set" + upperCaseName,
field.getType()) != null)) {
properties.add(name);
}
}
else {
if ((cl.getDeclaredMethod("get" + upperCaseName) != null)
&& (cl.getDeclaredMethod("set" + upperCaseName,
field.getType()) != null)) {
properties.add(name);
}
}
} catch (NoSuchMethodException | SecurityException e) {
}
}
}
for (String property:properties)
System.out.println(property);
source
share