Apache Solr open-source search software
SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants
Christine Poerschke038fe9378dab
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 2146539395a..c886fd07c83 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -67,6 +67,9 @@ Bug Fixes* SOLR-9199: ZkController#publishAndWaitForDownStates logic is inefficient (Hrishikesh Gadre)+* SOLR-9161: Change SolrPluginUtils.invokeSetters implementation to accommodate setter variants.+ (Christine Poerschke, Steve Rowe, Uwe Schindler)+================== 6.1.0 ==================Upgrading from Solr any prior releasediff --git a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.javaindex ecd48eb6b5a..1e5a1835a61 100644--- a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java+++ b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java@@ -16,6 +16,10 @@*/package org.apache.solr.util;+import java.beans.BeanInfo;+import java.beans.IntrospectionException;+import java.beans.Introspector;+import java.beans.MethodDescriptor;import java.io.IOException;import java.lang.invoke.MethodHandles;import java.lang.reflect.InvocationTargetException;@@ -1065,8 +1069,8 @@ public class SolrPluginUtils {String key = entry.getKey();String setterName = "set" + String.valueOf(Character.toUpperCase(key.charAt(0))) + key.substring(1);try {- final Method method = findSetter(clazz, setterName, key);final Object val = entry.getValue();+ final Method method = findSetter(clazz, setterName, key, val.getClass());method.invoke(bean, val);} catch (InvocationTargetException | IllegalAccessException e1) {throw new RuntimeException("Error invoking setter " + setterName + " on class : " + clazz.getName(), e1);@@ -1074,10 +1078,21 @@ public class SolrPluginUtils {}}- private static Method findSetter(Class<?> clazz, String setterName, String key) {- for (Method m : clazz.getMethods()) {- if (m.getName().equals(setterName) && m.getParameterTypes().length == 1) {- return m;+ private static Method findSetter(Class<?> clazz, String setterName, String key, Class<?> paramClazz) {+ BeanInfo beanInfo;+ try {+ beanInfo = Introspector.getBeanInfo(clazz);+ } catch (IntrospectionException ie) {+ throw new RuntimeException("Error getting bean info for class : " + clazz.getName(), ie);+ }+ for (final boolean matchParamClazz: new boolean[]{true, false}) {+ for (final MethodDescriptor desc : beanInfo.getMethodDescriptors()) {+ final Method m = desc.getMethod();+ final Class<?> p[] = m.getParameterTypes();+ if (m.getName().equals(setterName) && p.length == 1 &&+ (!matchParamClazz || paramClazz.equals(p[0]))) {+ return m;+ }}}throw new RuntimeException("No setter corrresponding to '" + key + "' in " + clazz.getName());diff --git a/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java b/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.javaindex 33e9291c703..fc506807630 100644--- a/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java+++ b/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java@@ -455,6 +455,34 @@ public class SolrPluginUtilsTest extends SolrTestCaseJ4 {assertEquals(3, q.build().getMinimumNumberShouldMatch());}+ private class InvokeSettersTestClass {+ private float aFloat = random().nextFloat();+ public float getAFloat() {+ return aFloat;+ }+ public void setAFloat(float aFloat) {+ this.aFloat = aFloat;+ }+ public void setAFloat(String aFloat) {+ this.aFloat = Float.parseFloat(aFloat);+ }+ }++ @Test+ public void testInvokeSetters() {+ final Float theFloat = new Float(random().nextFloat());+ implTestInvokeSetters(theFloat, theFloat);+ implTestInvokeSetters(theFloat, theFloat.toString());+ }++ public void implTestInvokeSetters(final Float theFloat, final Object theFloatObject) {+ final InvokeSettersTestClass bean = new InvokeSettersTestClass();+ final Map<String,Object> initArgs = new HashMap<>();+ initArgs.put("aFloat", theFloatObject);+ SolrPluginUtils.invokeSetters(bean, initArgs.entrySet());+ assertEquals(bean.getAFloat(), theFloat.floatValue(), 0.0);+ }+/** macro */public String pe(CharSequence s) {return SolrPluginUtils.partialEscape(s).toString();