Add getSource() to ResolvableType & TypeDescriptor
Add getSource() method to ResolvableType and TypeDescriptor allowing access to the underlying source field or method parameter when possible. Primarily added to provide access to additional type information or meta-data that alternative JVM languages may provide. Issue: SPR-10887
This commit is contained in:
@@ -786,6 +786,21 @@ public class ResolvableTypeTests {
|
||||
assertThat(ResolvableType.forClass(List.class, ListOfGenericArray.class).toString(), equalTo("java.util.List<java.util.List<java.lang.String>[]>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSource() throws Exception {
|
||||
Class<?> classType = MySimpleInterfaceType.class;
|
||||
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);
|
||||
assertThat(ResolvableType.forField(basicField).getSource(), equalTo((Object) basicField));
|
||||
assertThat(ResolvableType.forField(field).getSource(), equalTo((Object) field));
|
||||
assertThat(ResolvableType.forMethodParameter(methodParameter).getSource(), equalTo((Object) methodParameter));
|
||||
assertThat(ResolvableType.forMethodParameter(method, 0).getSource(), equalTo((Object) methodParameter));
|
||||
assertThat(ResolvableType.forClass(classType).getSource(), equalTo((Object) classType));
|
||||
assertThat(ResolvableType.forClass(classType).getSuperType().getSource(), equalTo((Object) classType.getGenericSuperclass()));
|
||||
}
|
||||
|
||||
private void assertFieldToStringValue(String field, String expected) throws Exception {
|
||||
ResolvableType type = ResolvableType.forField(Fields.class.getField(field));
|
||||
assertThat("field " + field + " toString", type.toString(), equalTo(expected));
|
||||
@@ -1033,7 +1048,7 @@ public class ResolvableTypeTests {
|
||||
assertThat(forClass, not(equalTo(forFieldWithImplementation)));
|
||||
|
||||
assertThat(forFieldDirect, equalTo(forFieldDirect));
|
||||
assertThat(forFieldDirect, equalTo(forFieldViaType));
|
||||
assertThat(forFieldDirect, not(equalTo(forFieldViaType)));
|
||||
assertThat(forFieldDirect, not(equalTo(forFieldWithImplementation)));
|
||||
}
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
@@ -34,7 +35,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
@@ -932,4 +932,12 @@ public class TypeDescriptorTests {
|
||||
assertThat(typeDescriptor.getMapValueTypeDescriptor(), nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getSource() throws Exception {
|
||||
Field field = getClass().getField("fieldScalar");
|
||||
MethodParameter methodParameter = new MethodParameter(getClass().getMethod("testParameterPrimitive", int.class), 0);
|
||||
assertThat(new TypeDescriptor(field).getSource(), equalTo((Object) field));
|
||||
assertThat(new TypeDescriptor(methodParameter).getSource(), equalTo((Object) methodParameter));
|
||||
assertThat(TypeDescriptor.valueOf(Integer.class).getSource(), equalTo((Object) Integer.class));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user