Fixed regression in SpEL's constructor resolution
Issue: SPR-11348
This commit is contained in:
parent
8faf01f56d
commit
86fc2dd556
|
|
@ -68,6 +68,7 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
|
||||||
});
|
});
|
||||||
|
|
||||||
Constructor<?> closeMatch = null;
|
Constructor<?> closeMatch = null;
|
||||||
|
Constructor<?> matchRequiringConversion = null;
|
||||||
|
|
||||||
for (Constructor<?> ctor : ctors) {
|
for (Constructor<?> ctor : ctors) {
|
||||||
Class<?>[] paramTypes = ctor.getParameterTypes();
|
Class<?>[] paramTypes = ctor.getParameterTypes();
|
||||||
|
|
@ -93,13 +94,24 @@ public class ReflectiveConstructorResolver implements ConstructorResolver {
|
||||||
if (matchInfo.isExactMatch()) {
|
if (matchInfo.isExactMatch()) {
|
||||||
return new ReflectiveConstructorExecutor(ctor);
|
return new ReflectiveConstructorExecutor(ctor);
|
||||||
}
|
}
|
||||||
else if (matchInfo.isCloseMatch() || matchInfo.isMatchRequiringConversion()) {
|
else if (matchInfo.isCloseMatch()) {
|
||||||
closeMatch = ctor;
|
closeMatch = ctor;
|
||||||
}
|
}
|
||||||
|
else if (matchInfo.isMatchRequiringConversion()) {
|
||||||
|
matchRequiringConversion = ctor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return (closeMatch != null ? new ReflectiveConstructorExecutor(closeMatch) : null);
|
if (closeMatch != null) {
|
||||||
|
return new ReflectiveConstructorExecutor(closeMatch);
|
||||||
|
}
|
||||||
|
else if (matchRequiringConversion != null) {
|
||||||
|
return new ReflectiveConstructorExecutor(matchRequiringConversion);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (EvaluationException ex) {
|
catch (EvaluationException ex) {
|
||||||
throw new AccessException("Failed to resolve constructor", ex);
|
throw new AccessException("Failed to resolve constructor", ex);
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,10 @@ import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -1571,7 +1574,7 @@ public class SpelReproTests extends ExpressionTestCase {
|
||||||
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
|
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
|
||||||
StandardEvaluationContext context = new StandardEvaluationContext();
|
StandardEvaluationContext context = new StandardEvaluationContext();
|
||||||
Object target = new GenericImplementation();
|
Object target = new GenericImplementation();
|
||||||
TypedValue value = accessor.read(context, target , "property");
|
TypedValue value = accessor.read(context, target, "property");
|
||||||
assertEquals(Integer.class, value.getTypeDescriptor().getType());
|
assertEquals(Integer.class, value.getTypeDescriptor().getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1711,8 +1714,7 @@ public class SpelReproTests extends ExpressionTestCase {
|
||||||
Method method = XYZ.class.getMethod("values");
|
Method method = XYZ.class.getMethod("values");
|
||||||
Object value = method.invoke(target, arguments);
|
Object value = method.invoke(target, arguments);
|
||||||
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
|
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
|
||||||
}
|
} catch (Exception ex) {
|
||||||
catch (Exception ex) {
|
|
||||||
throw new AccessException(ex.getMessage(), ex);
|
throw new AccessException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1752,16 +1754,32 @@ public class SpelReproTests extends ExpressionTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOperatorEq_SPR9194() {
|
public void SPR_9194() {
|
||||||
TestClass2 one = new TestClass2("abc");
|
TestClass2 one = new TestClass2("abc");
|
||||||
TestClass2 two = new TestClass2("abc");
|
TestClass2 two = new TestClass2("abc");
|
||||||
Map<String,TestClass2> map = new HashMap<String,TestClass2>();
|
Map<String, TestClass2> map = new HashMap<String, TestClass2>();
|
||||||
map.put("one",one);
|
map.put("one", one);
|
||||||
map.put("two",two);
|
map.put("two", two);
|
||||||
|
|
||||||
SpelExpressionParser parser = new SpelExpressionParser();
|
SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
Expression classNameExpression = parser.parseExpression("['one'] == ['two']");
|
Expression expr = parser.parseExpression("['one'] == ['two']");
|
||||||
assertTrue(classNameExpression.getValue(map,Boolean.class));
|
assertTrue(expr.getValue(map, Boolean.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void SPR_11348() {
|
||||||
|
Collection<String> coll = new HashSet<String>();
|
||||||
|
coll.add("one");
|
||||||
|
coll.add("two");
|
||||||
|
coll = Collections.unmodifiableCollection(coll);
|
||||||
|
|
||||||
|
SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
|
Expression expr = parser.parseExpression("new java.util.ArrayList(#root)");
|
||||||
|
Object value = expr.getValue(coll);
|
||||||
|
assertTrue(value instanceof ArrayList);
|
||||||
|
ArrayList list = (ArrayList) value;
|
||||||
|
assertEquals("one", list.get(0));
|
||||||
|
assertEquals("two", list.get(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue