Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 8F45A200B11 for ; Mon, 13 Jun 2016 15:00:15 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 8DC4D160A3C; Mon, 13 Jun 2016 13:00:15 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B7BD0160A19 for ; Mon, 13 Jun 2016 15:00:14 +0200 (CEST) Received: (qmail 71738 invoked by uid 500); 13 Jun 2016 13:00:13 -0000 Mailing-List: contact commits-help@lucene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@lucene.apache.org Delivered-To: mailing list commits@lucene.apache.org Received: (qmail 71729 invoked by uid 99); 13 Jun 2016 13:00:13 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 13 Jun 2016 13:00:13 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B4D8CDFB74; Mon, 13 Jun 2016 13:00:13 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cpoerschke@apache.org To: commits@lucene.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: lucene-solr:master: SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants Date: Mon, 13 Jun 2016 13:00:13 +0000 (UTC) archived-at: Mon, 13 Jun 2016 13:00:15 -0000 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 Authored: Mon Jun 13 13:05:08 2016 +0100 Committer: Christine Poerschke 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 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();