mirror of https://github.com/apache/lucene.git
SOLR-9161: SolrPluginUtils.invokeSetters now accommodates setter variants
This commit is contained in:
parent
3d22ea3492
commit
50658dd93d
|
@ -337,6 +337,8 @@ Other Changes
|
||||||
|
|
||||||
* SOLR-9110: Move JoinFromCollection- SubQueryTransformer- BlockJoinFacet- Distrib Tests to SolrCloudTestCase (Mikhail Khludnev)
|
* SOLR-9110: Move JoinFromCollection- SubQueryTransformer- BlockJoinFacet- Distrib Tests to SolrCloudTestCase (Mikhail Khludnev)
|
||||||
|
|
||||||
|
* SOLR-9161: SolrPluginUtils.invokeSetters now accommodates setter variants (Christine Poerschke)
|
||||||
|
|
||||||
================== 6.0.1 ==================
|
================== 6.0.1 ==================
|
||||||
(No Changes)
|
(No Changes)
|
||||||
|
|
||||||
|
|
|
@ -1066,8 +1066,8 @@ public class SolrPluginUtils {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
String setterName = "set" + String.valueOf(Character.toUpperCase(key.charAt(0))) + key.substring(1);
|
String setterName = "set" + String.valueOf(Character.toUpperCase(key.charAt(0))) + key.substring(1);
|
||||||
try {
|
try {
|
||||||
final Method method = findSetter(clazz, setterName, key);
|
|
||||||
final Object val = entry.getValue();
|
final Object val = entry.getValue();
|
||||||
|
final Method method = findSetter(clazz, setterName, key, val.getClass());
|
||||||
method.invoke(bean, val);
|
method.invoke(bean, val);
|
||||||
} catch (InvocationTargetException | IllegalAccessException e1) {
|
} catch (InvocationTargetException | IllegalAccessException e1) {
|
||||||
throw new RuntimeException("Error invoking setter " + setterName + " on class : " + clazz.getName(), e1);
|
throw new RuntimeException("Error invoking setter " + setterName + " on class : " + clazz.getName(), e1);
|
||||||
|
@ -1075,10 +1075,14 @@ public class SolrPluginUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Method findSetter(Class<?> clazz, String setterName, String key) {
|
private static Method findSetter(Class<?> clazz, String setterName, String key, Class<?> paramClazz) {
|
||||||
for (Method m : clazz.getMethods()) {
|
try {
|
||||||
if (m.getName().equals(setterName) && m.getParameterTypes().length == 1) {
|
return clazz.getMethod(setterName, new Class<?>[] { paramClazz });
|
||||||
return m;
|
} catch (NoSuchMethodException e) {
|
||||||
|
for (Method m : clazz.getMethods()) {
|
||||||
|
if (m.getName().equals(setterName) && m.getParameterTypes().length == 1) {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new RuntimeException("No setter corrresponding to '" + key + "' in " + clazz.getName());
|
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());
|
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 */
|
/** macro */
|
||||||
public String pe(CharSequence s) {
|
public String pe(CharSequence s) {
|
||||||
return SolrPluginUtils.partialEscape(s).toString();
|
return SolrPluginUtils.partialEscape(s).toString();
|
||||||
|
|
Loading…
Reference in New Issue