From bddd13dcd108b78a639025d01d4dd4d12afacbb2 Mon Sep 17 00:00:00 2001 From: Christopher Chianelli Date: Mon, 25 Jul 2022 10:21:03 -0400 Subject: [PATCH] Use seperate map for isGetters in PropertyUtils 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). --- .../deployment/recording/PropertyUtils.java | 33 +++++-------------- .../recording/TestJavaBeanWithBoolean.java | 5 +++ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java b/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java index 4ee55eeca0d0d9..02d3cf4dd5153b 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java @@ -22,32 +22,10 @@ public Property[] apply(Class type) { List ret = new ArrayList<>(); Method[] methods = type.getMethods(); - List 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 getters = new HashMap<>(); + Map isGetters = new HashMap<>(); Map 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); @@ -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 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])); diff --git a/core/deployment/src/test/java/io/quarkus/deployment/recording/TestJavaBeanWithBoolean.java b/core/deployment/src/test/java/io/quarkus/deployment/recording/TestJavaBeanWithBoolean.java index 14bb00f1cf59db..eed909521f6a12 100644 --- a/core/deployment/src/test/java/io/quarkus/deployment/recording/TestJavaBeanWithBoolean.java +++ b/core/deployment/src/test/java/io/quarkus/deployment/recording/TestJavaBeanWithBoolean.java @@ -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; }