You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by cp...@apache.org on 2016/06/13 13:00:13 UTC

lucene-solr:master: SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants

Repository: lucene-solr
Updated Branches:
  refs/heads/master 95c7e6d71 -> 038fe9378


SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/038fe937
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/038fe937
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/038fe937

Branch: refs/heads/master
Commit: 038fe9378dab18d0e16b34c26dc802c6560e77e7
Parents: 95c7e6d
Author: Christine Poerschke <cp...@apache.org>
Authored: Mon Jun 13 13:05:08 2016 +0100
Committer: Christine Poerschke <cp...@apache.org>
Committed: Mon Jun 13 13:05:08 2016 +0100

----------------------------------------------------------------------
 solr/CHANGES.txt                                |  3 +++
 .../org/apache/solr/util/SolrPluginUtils.java   | 25 +++++++++++++----
 .../apache/solr/util/SolrPluginUtilsTest.java   | 28 ++++++++++++++++++++
 3 files changed, 51 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/038fe937/solr/CHANGES.txt
----------------------------------------------------------------------
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index 2146539..c886fd0 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 release

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/038fe937/solr/core/src/java/org/apache/solr/util/SolrPluginUtils.java
----------------------------------------------------------------------
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 ecd48eb..1e5a183 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());

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/038fe937/solr/core/src/test/org/apache/solr/util/SolrPluginUtilsTest.java
----------------------------------------------------------------------
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 33e9291..fc50680 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();