Skip to content

Commit

Permalink
Use seperate map for isGetters in PropertyUtils
Browse files Browse the repository at this point in the history
Using a seperate map removes the need to have a seperate
loop for identifing the actual getter method. It also
make the logic more clear (first try "get" getter, then try "is"
getter).
  • Loading branch information
Christopher-Chianelli committed Jul 25, 2022
1 parent 399d311 commit bddd13d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,10 @@ public Property[] apply(Class<?> type) {
List<Property> ret = new ArrayList<>();
Method[] methods = type.getMethods();

List<Method> checkedMethodList = new ArrayList<>();

for (Method method : methods) {
if (method.getName().startsWith("get")) {
checkedMethodList.add(method);
} else if (method.getName().startsWith("is")) {
boolean hasDuplicate = false;
for (Method maybeDuplicate : methods) {
// prefer get method over is method
if (maybeDuplicate.getName().equals("get" + method.getName().substring(2))) {
hasDuplicate = true;
break;
}
}

if (!hasDuplicate) {
checkedMethodList.add(method);
}
} else if (method.getName().startsWith("set")) {
checkedMethodList.add(method);
}
}

Map<String, Method> getters = new HashMap<>();
Map<String, Method> isGetters = new HashMap<>();
Map<String, Method> setters = new HashMap<>();
for (Method i : checkedMethodList) {
for (Method i : methods) {
if (i.getName().startsWith("get") && i.getName().length() > 3 && i.getParameterCount() == 0
&& i.getReturnType() != void.class) {
String name = Character.toLowerCase(i.getName().charAt(3)) + i.getName().substring(4);
Expand All @@ -60,16 +38,21 @@ public Property[] apply(Class<?> type) {
} else if (i.getName().startsWith("is") && i.getName().length() > 3 && i.getParameterCount() == 0
&& (i.getReturnType() == boolean.class || i.getReturnType() == Boolean.class)) {
String name = Character.toLowerCase(i.getName().charAt(2)) + i.getName().substring(3);
getters.put(name, i);
isGetters.put(name, i);
} else if (i.getName().startsWith("set") && i.getName().length() > 3 && i.getParameterCount() == 1) {
String name = Character.toLowerCase(i.getName().charAt(3)) + i.getName().substring(4);
setters.put(name, i);
}
}

Set<String> names = new HashSet<>(getters.keySet());
names.addAll(isGetters.keySet());
names.addAll(setters.keySet());
for (String i : names) {
Method get = getters.get(i);
if (get == null) {
get = isGetters.get(i); // If there is no "get" getter, use the "is" getter
}
Method set = setters.get(i);
if (get == null) {
ret.add(new Property(i, get, set, set.getParameterTypes()[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public Boolean isBoxedBoolWithIsGetter() {
return boxedBoolWithIsGetter;
}

// this is not actually a getter (takes a parameter)
public Boolean getBoxedBoolWithIsGetter(String parameter) {
return !boxedBoolWithIsGetter;
}

public void setBoxedBoolWithIsGetter(Boolean boxedBoolWithIsGetter) {
this.boxedBoolWithIsGetter = boxedBoolWithIsGetter;
}
Expand Down

0 comments on commit bddd13d

Please sign in to comment.