From 9be5b98eb3ca85b7597f96dc9a42551fe3051d4d Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Mon, 13 Jun 2016 13:05:08 +0100 Subject: [PATCH] SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants --- solr/CHANGES.txt | 3 ++ .../org/apache/solr/util/SolrPluginUtils.java | 25 +++++++++++++---- .../apache/solr/util/SolrPluginUtilsTest.java | 28 +++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index c5bbc32372d..570cee2f99e 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -42,6 +42,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 ================== Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. diff --git a/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java b/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java index 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.java index 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 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();