Browse Source

Use bridge methods in ReflectiveMethodResolver

Update ReflectiveMethodResolver to consider bridge methods.

Issue: SPR-10210
pull/231/merge
Phillip Webb 12 years ago
parent
commit
634284e1fd
  1. 34
      spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java
  2. 10
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java
  3. 23
      spring-expression/src/test/java/org/springframework/expression/spel/spr10210/A.java
  4. 20
      spring-expression/src/test/java/org/springframework/expression/spel/spr10210/D.java
  5. 24
      spring-expression/src/test/java/org/springframework/expression/spel/spr10210/comp/B.java
  6. 20
      spring-expression/src/test/java/org/springframework/expression/spel/spr10210/infra/C.java

34
spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectiveMethodResolver.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,13 +19,16 @@ package org.springframework.expression.spel.support; @@ -19,13 +19,16 @@ package org.springframework.expression.spel.support;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.expression.AccessException;
@ -37,7 +40,6 @@ import org.springframework.expression.MethodResolver; @@ -37,7 +40,6 @@ import org.springframework.expression.MethodResolver;
import org.springframework.expression.TypeConverter;
import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;
import org.springframework.util.CollectionUtils;
/**
* Reflection-based {@link MethodResolver} used by default in
@ -92,25 +94,16 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -92,25 +94,16 @@ public class ReflectiveMethodResolver implements MethodResolver {
try {
TypeConverter typeConverter = context.getTypeConverter();
Class<?> type = (targetObject instanceof Class ? (Class<?>) targetObject : targetObject.getClass());
Method[] methods = getMethods(type, targetObject);
List<Method> methods = new ArrayList<Method>(Arrays.asList(getMethods(type, targetObject)));
// If a filter is registered for this type, call it
MethodFilter filter = (this.filters != null ? this.filters.get(type) : null);
if (filter != null) {
List<Method> methodsForFiltering = new ArrayList<Method>();
for (Method method: methods) {
methodsForFiltering.add(method);
}
List<Method> methodsFiltered = filter.filter(methodsForFiltering);
if (CollectionUtils.isEmpty(methodsFiltered)) {
methods = NO_METHODS;
}
else {
methods = methodsFiltered.toArray(new Method[methodsFiltered.size()]);
}
methods = filter.filter(methods);
}
Arrays.sort(methods, new Comparator<Method>() {
// Sort methods into a sensible order
Collections.sort(methods, new Comparator<Method>() {
public int compare(Method m1, Method m2) {
int m1pl = m1.getParameterTypes().length;
int m2pl = m2.getParameterTypes().length;
@ -118,6 +111,14 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -118,6 +111,14 @@ public class ReflectiveMethodResolver implements MethodResolver {
}
});
// Resolve any bridge methods
for (int i = 0; i < methods.size(); i++) {
methods.set(i, BridgeMethodResolver.findBridgedMethod(methods.get(i)));
}
// Remove duplicate methods (possible due to resolved bridge methods)
methods = new ArrayList<Method>(new LinkedHashSet<Method>(methods));
Method closeMatch = null;
int closeMatchDistance = Integer.MAX_VALUE;
int[] argsToConvert = null;
@ -125,9 +126,6 @@ public class ReflectiveMethodResolver implements MethodResolver { @@ -125,9 +126,6 @@ public class ReflectiveMethodResolver implements MethodResolver {
boolean multipleOptions = false;
for (Method method : methods) {
if (method.isBridge()) {
continue;
}
if (method.getName().equals(name)) {
Class<?>[] paramTypes = method.getParameterTypes();
List<TypeDescriptor> paramDescriptors = new ArrayList<TypeDescriptor>(paramTypes.length);

10
spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat; @@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
@ -1736,6 +1737,14 @@ public class SpelReproTests extends ExpressionTestCase { @@ -1736,6 +1737,14 @@ public class SpelReproTests extends ExpressionTestCase {
assertThat(fromClass, is("interfaceValue"));
}
@Test
public void SPR_10210() throws Exception {
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("bridgeExample", new org.springframework.expression.spel.spr10210.D());
Expression parseExpression = parser.parseExpression("#bridgeExample.bridgetMethod()");
parseExpression.getValue(context);
}
public static class BooleanHolder {
private Boolean simpleProperty = true;
@ -1796,4 +1805,5 @@ public class SpelReproTests extends ExpressionTestCase { @@ -1796,4 +1805,5 @@ public class SpelReproTests extends ExpressionTestCase {
public static class StaticFinalImpl2 extends AbstractStaticFinal {
}
}

23
spring-expression/src/test/java/org/springframework/expression/spel/spr10210/A.java

@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.spr10210;
import org.springframework.expression.spel.spr10210.comp.B;
import org.springframework.expression.spel.spr10210.infra.C;
abstract class A extends B<C> {
}

20
spring-expression/src/test/java/org/springframework/expression/spel/spr10210/D.java

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.spr10210;
public class D extends A {
}

24
spring-expression/src/test/java/org/springframework/expression/spel/spr10210/comp/B.java

@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.spr10210.comp;
import java.io.Serializable;
import org.springframework.expression.spel.spr10210.infra.C;
public class B<T extends C> implements Serializable {
}

20
spring-expression/src/test/java/org/springframework/expression/spel/spr10210/infra/C.java

@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.expression.spel.spr10210.infra;
public interface C {
}
Loading…
Cancel
Save