mirror of https://github.com/apache/lucene.git
SOLR-9161: change SolrPluginUtils.invokeSetters implementation to accommodate setter variants
This commit is contained in:
parent
9719105e7c
commit
9be5b98eb3
|
@ -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.
|
||||
|
|
|
@ -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,12 +1078,23 @@ 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) {
|
||||
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());
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue