MethodParameter supports Java 8 Executable/Parameter and validates parameter indexes
Also, equals insists on the same class now, differentiating from SynthesizingMethodParameter. Issue: SPR-14055 Issue: SPR-13456 Issue: SPR-14438
This commit is contained in:
@@ -158,7 +158,7 @@ public class GenericTypeResolverTests {
|
||||
@Test
|
||||
public void getGenericsOnArrayFromParamCannotBeResolved() throws Exception {
|
||||
// SPR-11044
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(
|
||||
WithArrayBase.class.getDeclaredMethod("array", Object[].class), 0);
|
||||
Class<?> resolved = GenericTypeResolver.resolveParameterType(methodParameter, WithArray.class);
|
||||
assertThat(resolved, equalTo((Class<?>) Object[].class));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -25,9 +25,12 @@ import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class MethodParameterTests {
|
||||
|
||||
private Method method;
|
||||
|
||||
private MethodParameter stringParameter;
|
||||
|
||||
private MethodParameter longParameter;
|
||||
@@ -37,12 +40,13 @@ public class MethodParameterTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchMethodException {
|
||||
Method method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
stringParameter = new MethodParameter(method, 0);
|
||||
longParameter = new MethodParameter(method, 1);
|
||||
intReturnType = new MethodParameter(method, -1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEquals() throws NoSuchMethodException {
|
||||
assertEquals(stringParameter, stringParameter);
|
||||
@@ -60,8 +64,8 @@ public class MethodParameterTests {
|
||||
MethodParameter methodParameter = new MethodParameter(method, 0);
|
||||
assertEquals(stringParameter, methodParameter);
|
||||
assertEquals(methodParameter, stringParameter);
|
||||
assertFalse(longParameter.equals(methodParameter));
|
||||
assertFalse(methodParameter.equals(longParameter));
|
||||
assertNotEquals(longParameter, methodParameter);
|
||||
assertNotEquals(methodParameter, longParameter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -73,7 +77,25 @@ public class MethodParameterTests {
|
||||
Method method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
MethodParameter methodParameter = new MethodParameter(method, 0);
|
||||
assertEquals(stringParameter.hashCode(), methodParameter.hashCode());
|
||||
assertTrue(longParameter.hashCode() != methodParameter.hashCode());
|
||||
assertNotEquals(longParameter.hashCode(), methodParameter.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("deprecation")
|
||||
public void testFactoryMethods() {
|
||||
assertEquals(stringParameter, MethodParameter.forMethodOrConstructor(method, 0));
|
||||
assertEquals(longParameter, MethodParameter.forMethodOrConstructor(method, 1));
|
||||
|
||||
assertEquals(stringParameter, MethodParameter.forExecutable(method, 0));
|
||||
assertEquals(longParameter, MethodParameter.forExecutable(method, 1));
|
||||
|
||||
assertEquals(stringParameter, MethodParameter.forParameter(method.getParameters()[0]));
|
||||
assertEquals(longParameter, MethodParameter.forParameter(method.getParameters()[1]));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIndexValidation() {
|
||||
new MethodParameter(method, 2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -214,7 +214,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void forMethodParameter() throws Exception {
|
||||
Method method = Methods.class.getMethod("charSequenceParameter", List.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
|
||||
assertThat(type.getType(), equalTo(method.getGenericParameterTypes()[0]));
|
||||
}
|
||||
@@ -222,7 +222,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void forMethodParameterWithNesting() throws Exception {
|
||||
Method method = Methods.class.getMethod("nested", Map.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
methodParameter.increaseNestingLevel();
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
|
||||
assertThat(type.resolve(), equalTo((Class) Map.class));
|
||||
@@ -233,7 +233,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void forMethodParameterWithNestingAndLevels() throws Exception {
|
||||
Method method = Methods.class.getMethod("nested", Map.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
methodParameter.increaseNestingLevel();
|
||||
methodParameter.setTypeIndexForCurrentLevel(0);
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
|
||||
@@ -782,7 +782,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void resolveTypeVariableFromMethodParameterType() throws Exception {
|
||||
Method method = Methods.class.getMethod("typedParameter", Object.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
|
||||
assertThat(type.resolve(), nullValue());
|
||||
assertThat(type.getType().toString(), equalTo("T"));
|
||||
@@ -791,7 +791,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void resolveTypeVariableFromMethodParameterTypeWithImplementsClass() throws Exception {
|
||||
Method method = Methods.class.getMethod("typedParameter", Object.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
methodParameter.setContainingClass(TypedMethods.class);
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter);
|
||||
assertThat(type.resolve(), equalTo((Class) String.class));
|
||||
@@ -801,7 +801,7 @@ public class ResolvableTypeTests {
|
||||
@Test
|
||||
public void resolveTypeVariableFromMethodParameterTypeWithImplementsType() throws Exception {
|
||||
Method method = Methods.class.getMethod("typedParameter", Object.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
ResolvableType implementationType = ResolvableType.forClassWithGenerics(Methods.class, Integer.class);
|
||||
ResolvableType type = ResolvableType.forMethodParameter(methodParameter, implementationType);
|
||||
assertThat(type.resolve(), equalTo((Class) Integer.class));
|
||||
@@ -893,7 +893,7 @@ public class ResolvableTypeTests {
|
||||
Field basicField = Fields.class.getField("classType");
|
||||
Field field = Fields.class.getField("charSequenceList");
|
||||
Method method = Methods.class.getMethod("charSequenceParameter", List.class);
|
||||
MethodParameter methodParameter = MethodParameter.forMethodOrConstructor(method, 0);
|
||||
MethodParameter methodParameter = MethodParameter.forExecutable(method, 0);
|
||||
assertThat(ResolvableType.forField(basicField).getSource(), equalTo((Object) basicField));
|
||||
assertThat(ResolvableType.forField(field).getSource(), equalTo((Object) field));
|
||||
assertThat(ResolvableType.forMethodParameter(methodParameter).getSource(), equalTo((Object) methodParameter));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -46,78 +46,78 @@ public class SerializableTypeWrapperTests {
|
||||
public void forField() throws Exception {
|
||||
Type type = SerializableTypeWrapper.forField(Fields.class.getField("parameterizedType"));
|
||||
assertThat(type.toString(), equalTo("java.util.List<java.lang.String>"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forMethodParameter() throws Exception {
|
||||
Method method = Methods.class.getDeclaredMethod("method", Class.class, Object.class);
|
||||
Type type = SerializableTypeWrapper.forMethodParameter(MethodParameter.forMethodOrConstructor(method, 0));
|
||||
Type type = SerializableTypeWrapper.forMethodParameter(MethodParameter.forExecutable(method, 0));
|
||||
assertThat(type.toString(), equalTo("java.lang.Class<T>"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forConstructor() throws Exception {
|
||||
Constructor<?> constructor = Constructors.class.getDeclaredConstructor(List.class);
|
||||
Type type = SerializableTypeWrapper.forMethodParameter(MethodParameter.forMethodOrConstructor(constructor, 0));
|
||||
Type type = SerializableTypeWrapper.forMethodParameter(MethodParameter.forExecutable(constructor, 0));
|
||||
assertThat(type.toString(), equalTo("java.util.List<java.lang.String>"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forGenericSuperClass() throws Exception {
|
||||
Type type = SerializableTypeWrapper.forGenericSuperclass(ArrayList.class);
|
||||
assertThat(type.toString(), equalTo("java.util.AbstractList<E>"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forGenericInterfaces() throws Exception {
|
||||
Type type = SerializableTypeWrapper.forGenericInterfaces(List.class)[0];
|
||||
assertThat(type.toString(), equalTo("java.util.Collection<E>"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void forTypeParamters() throws Exception {
|
||||
Type type = SerializableTypeWrapper.forTypeParameters(List.class)[0];
|
||||
assertThat(type.toString(), equalTo("E"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void classType() throws Exception {
|
||||
Type type = SerializableTypeWrapper.forField(Fields.class.getField("classType"));
|
||||
assertThat(type.toString(), equalTo("class java.lang.String"));
|
||||
assertSerialzable(type);
|
||||
assertSerializable(type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void genericArrayType() throws Exception {
|
||||
GenericArrayType type = (GenericArrayType) SerializableTypeWrapper.forField(Fields.class.getField("genericArrayType"));
|
||||
assertThat(type.toString(), equalTo("java.util.List<java.lang.String>[]"));
|
||||
assertSerialzable(type);
|
||||
assertSerialzable(type.getGenericComponentType());
|
||||
assertSerializable(type);
|
||||
assertSerializable(type.getGenericComponentType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parameterizedType() throws Exception {
|
||||
ParameterizedType type = (ParameterizedType) SerializableTypeWrapper.forField(Fields.class.getField("parameterizedType"));
|
||||
assertThat(type.toString(), equalTo("java.util.List<java.lang.String>"));
|
||||
assertSerialzable(type);
|
||||
assertSerialzable(type.getOwnerType());
|
||||
assertSerialzable(type.getRawType());
|
||||
assertSerialzable(type.getActualTypeArguments());
|
||||
assertSerialzable(type.getActualTypeArguments()[0]);
|
||||
assertSerializable(type);
|
||||
assertSerializable(type.getOwnerType());
|
||||
assertSerializable(type.getRawType());
|
||||
assertSerializable(type.getActualTypeArguments());
|
||||
assertSerializable(type.getActualTypeArguments()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeVariableType() throws Exception {
|
||||
TypeVariable<?> type = (TypeVariable<?>) SerializableTypeWrapper.forField(Fields.class.getField("typeVariableType"));
|
||||
assertThat(type.toString(), equalTo("T"));
|
||||
assertSerialzable(type);
|
||||
assertSerialzable(type.getBounds());
|
||||
assertSerializable(type);
|
||||
assertSerializable(type.getBounds());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -125,13 +125,13 @@ public class SerializableTypeWrapperTests {
|
||||
ParameterizedType typeSource = (ParameterizedType) SerializableTypeWrapper.forField(Fields.class.getField("wildcardType"));
|
||||
WildcardType type = (WildcardType) typeSource.getActualTypeArguments()[0];
|
||||
assertThat(type.toString(), equalTo("? extends java.lang.CharSequence"));
|
||||
assertSerialzable(type);
|
||||
assertSerialzable(type.getLowerBounds());
|
||||
assertSerialzable(type.getUpperBounds());
|
||||
assertSerializable(type);
|
||||
assertSerializable(type.getLowerBounds());
|
||||
assertSerializable(type.getUpperBounds());
|
||||
}
|
||||
|
||||
|
||||
private void assertSerialzable(Object source) throws Exception {
|
||||
private void assertSerializable(Object source) throws Exception {
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(bos);
|
||||
oos.writeObject(source);
|
||||
@@ -152,19 +152,19 @@ public class SerializableTypeWrapperTests {
|
||||
public T typeVariableType;
|
||||
|
||||
public List<? extends CharSequence> wildcardType;
|
||||
|
||||
}
|
||||
|
||||
static interface Methods {
|
||||
|
||||
interface Methods {
|
||||
|
||||
<T> List<T> method(Class<T> p1, T p2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class Constructors {
|
||||
|
||||
public Constructors(List<String> p) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
* Copyright 2002-2016 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.core.annotation;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class SynthesizingMethodParameterTests {
|
||||
|
||||
private Method method;
|
||||
|
||||
private SynthesizingMethodParameter stringParameter;
|
||||
|
||||
private SynthesizingMethodParameter longParameter;
|
||||
|
||||
private SynthesizingMethodParameter intReturnType;
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp() throws NoSuchMethodException {
|
||||
method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
stringParameter = new SynthesizingMethodParameter(method, 0);
|
||||
longParameter = new SynthesizingMethodParameter(method, 1);
|
||||
intReturnType = new SynthesizingMethodParameter(method, -1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEquals() throws NoSuchMethodException {
|
||||
assertEquals(stringParameter, stringParameter);
|
||||
assertEquals(longParameter, longParameter);
|
||||
assertEquals(intReturnType, intReturnType);
|
||||
|
||||
assertFalse(stringParameter.equals(longParameter));
|
||||
assertFalse(stringParameter.equals(intReturnType));
|
||||
assertFalse(longParameter.equals(stringParameter));
|
||||
assertFalse(longParameter.equals(intReturnType));
|
||||
assertFalse(intReturnType.equals(stringParameter));
|
||||
assertFalse(intReturnType.equals(longParameter));
|
||||
|
||||
Method method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
MethodParameter methodParameter = new SynthesizingMethodParameter(method, 0);
|
||||
assertEquals(stringParameter, methodParameter);
|
||||
assertEquals(methodParameter, stringParameter);
|
||||
assertNotEquals(longParameter, methodParameter);
|
||||
assertNotEquals(methodParameter, longParameter);
|
||||
|
||||
methodParameter = new MethodParameter(method, 0);
|
||||
assertNotEquals(stringParameter, methodParameter);
|
||||
assertNotEquals(methodParameter, stringParameter);
|
||||
assertNotEquals(longParameter, methodParameter);
|
||||
assertNotEquals(methodParameter, longParameter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() throws NoSuchMethodException {
|
||||
assertEquals(stringParameter.hashCode(), stringParameter.hashCode());
|
||||
assertEquals(longParameter.hashCode(), longParameter.hashCode());
|
||||
assertEquals(intReturnType.hashCode(), intReturnType.hashCode());
|
||||
|
||||
Method method = getClass().getMethod("method", String.class, Long.TYPE);
|
||||
SynthesizingMethodParameter methodParameter = new SynthesizingMethodParameter(method, 0);
|
||||
assertEquals(stringParameter.hashCode(), methodParameter.hashCode());
|
||||
assertNotEquals(longParameter.hashCode(), methodParameter.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryMethods() {
|
||||
assertEquals(stringParameter, SynthesizingMethodParameter.forExecutable(method, 0));
|
||||
assertEquals(longParameter, SynthesizingMethodParameter.forExecutable(method, 1));
|
||||
|
||||
assertEquals(stringParameter, SynthesizingMethodParameter.forParameter(method.getParameters()[0]));
|
||||
assertEquals(longParameter, SynthesizingMethodParameter.forParameter(method.getParameters()[1]));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testIndexValidation() {
|
||||
new SynthesizingMethodParameter(method, 2);
|
||||
}
|
||||
|
||||
|
||||
public int method(String p1, long p2) {
|
||||
return 42;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user