Refactored some code

This commit is contained in:
Patrick Gotthard 2013-11-16 07:02:20 +01:00
parent 90fca03a98
commit 990ca83787

View file

@ -134,31 +134,45 @@ public class BeanIntrospector {
} }
private static Map<String, PropertyDescriptor> getPDs(final Method[] methods, final boolean setters) throws IntrospectionException { private static Map<String, PropertyDescriptor> getPDs(final Method[] methods, final boolean setters) throws IntrospectionException {
final Map<String, PropertyDescriptor> pds = new HashMap<String, PropertyDescriptor>(); final Map<String, PropertyDescriptor> pds = new HashMap<String, PropertyDescriptor>();
for (final Method method : methods) { for (final Method method : methods) {
String pName = null;
PropertyDescriptor pDescriptor = null; String propertyName = null;
if ((method.getModifiers() & Modifier.PUBLIC) != 0) { PropertyDescriptor propertyDescriptor = null;
final int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0) {
final String methodName = method.getName();
final Class<?> returnType = method.getReturnType();
final int nrOfParameters = method.getParameterTypes().length;
if (setters) { if (setters) {
if (method.getName().startsWith(SETTER) && method.getReturnType() == void.class && method.getParameterTypes().length == 1) { if (methodName.startsWith(SETTER) && returnType == void.class && nrOfParameters == 1) {
pName = Introspector.decapitalize(method.getName().substring(3)); propertyName = Introspector.decapitalize(methodName.substring(3));
pDescriptor = new PropertyDescriptor(pName, null, method); propertyDescriptor = new PropertyDescriptor(propertyName, null, method);
} }
} else { } else {
if (method.getName().startsWith(GETTER) && method.getReturnType() != void.class && method.getParameterTypes().length == 0) { if (methodName.startsWith(GETTER) && returnType != void.class && nrOfParameters == 0) {
pName = Introspector.decapitalize(method.getName().substring(3)); propertyName = Introspector.decapitalize(methodName.substring(3));
pDescriptor = new PropertyDescriptor(pName, method, null); propertyDescriptor = new PropertyDescriptor(propertyName, method, null);
} else if (method.getName().startsWith(BOOLEAN_GETTER) && method.getReturnType() == boolean.class && method.getParameterTypes().length == 0) { } else if (methodName.startsWith(BOOLEAN_GETTER) && returnType == boolean.class && nrOfParameters == 0) {
pName = Introspector.decapitalize(method.getName().substring(2)); propertyName = Introspector.decapitalize(methodName.substring(2));
pDescriptor = new PropertyDescriptor(pName, method, null); propertyDescriptor = new PropertyDescriptor(propertyName, method, null);
} }
} }
} }
if (pName != null) {
pds.put(pName, pDescriptor); if (propertyName != null) {
pds.put(propertyName, propertyDescriptor);
} }
} }
return pds; return pds;
} }
private static List<PropertyDescriptor> merge(final Map<String, PropertyDescriptor> getters, final Map<String, PropertyDescriptor> setters) private static List<PropertyDescriptor> merge(final Map<String, PropertyDescriptor> getters, final Map<String, PropertyDescriptor> setters)