本文整理了Java中java.lang.Class.getField()
方法的一些代码示例,展示了Class.getField()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getField()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getField
[英]Returns a Field object which represents the public field with the given name. This method first searches the class C represented by this Class, then the interfaces implemented by C and finally the superclasses of C.
[中]返回一个Field对象,该对象表示具有给定名称的公共字段。该方法首先搜索由该类表示的类C,然后搜索由C实现的接口,最后搜索C的超类。
代码示例来源:origin: spring-projects/spring-framework
protected ResolvableType getGenericApplicationEventType(String fieldName) {
try {
return ResolvableType.forField(TestEvents.class.getField(fieldName));
}
catch (NoSuchFieldException ex) {
throw new IllegalStateException("No such field on Events '" + fieldName + "'");
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void isAssignableMapKeyValueTypes() throws Exception {
assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
assertTrue(new TypeDescriptor(getClass().getField("notGenericMap")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("notGenericMap"))));
assertFalse(new TypeDescriptor(getClass().getField("isAssignableMapKeyValueTypes")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
assertTrue(TypeDescriptor.valueOf(Map.class).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
}
代码示例来源:origin: google/guava
public void testInnerClassWithParameterizedOwner() throws Exception {
Type fieldType = ParameterizedOuter.class.getField("field").getGenericType();
assertEquals(
fieldType, TypeToken.of(ParameterizedOuter.class).resolveType(fieldType).getType());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void convertMapToMap() throws Exception {
Map<String, String> foo = new HashMap<>();
foo.put("1", "BAR");
foo.put("2", "BAZ");
@SuppressWarnings("unchecked")
Map<Integer, FooEnum> map = (Map<Integer, FooEnum>) conversionService.convert(foo,
TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap")));
assertEquals(FooEnum.BAR, map.get(1));
assertEquals(FooEnum.BAZ, map.get(2));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void wildcardType() throws Exception {
ParameterizedType typeSource = (ParameterizedType) SerializableTypeWrapper.forField(Fields.class.getField("wildcardType"));
WildcardType type = (WildcardType) typeSource.getActualTypeArguments()[0];
assertThat(type.toString(), equalTo("? extends java.lang.CharSequence"));
assertSerializable(type);
assertSerializable(type.getLowerBounds());
assertSerializable(type.getUpperBounds());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testWildcardMap() throws Exception {
Map<String, String> input = new LinkedHashMap<>();
input.put("key", "value");
Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
assertEquals(input, converted);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void paramaterizedType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("parameterizedType"));
assertThat(type.getType(), instanceOf(ParameterizedType.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void arrayClassType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("arrayClassType"));
assertThat(type.getType(), instanceOf(Class.class));
assertThat(((Class) type.getType()).isArray(), equalTo(true));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveArrayClassType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("arrayClassType"));
assertThat(type.resolve(), equalTo((Class) List[].class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testStringToEnumSet() throws Exception {
assertEquals(EnumSet.of(Foo.BAR), conversionService.convert("BAR", TypeDescriptor.valueOf(String.class),
new TypeDescriptor(getClass().getField("enumSet"))));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void getGenericOfGenericByIndexes() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringListList"));
assertThat(type.getGeneric(0, 0).getType(), equalTo((Type) String.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void resolveWildcardLowerBounds() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("wildcardSuperType"));
assertThat(type.getGeneric().resolve(), equalTo((Class) Number.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void emptyMapDifferentTargetImplType() throws Exception {
Map<String, String> map = new HashMap<>();
TypeDescriptor sourceType = TypeDescriptor.forObject(map);
TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget"));
assertTrue(conversionService.canConvert(sourceType, targetType));
@SuppressWarnings("unchecked")
LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType);
assertEquals(map, result);
assertEquals(LinkedHashMap.class, result.getClass());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void wildcardType() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("wildcardType"));
assertThat(type.getType(), instanceOf(ParameterizedType.class));
assertThat(type.getGeneric().getType(), instanceOf(WildcardType.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void getGenericByIndex() throws Exception {
ResolvableType type = ResolvableType.forField(Fields.class.getField("stringIntegerMultiValueMap"));
assertThat(type.getGeneric(0).getType(), equalTo((Type) String.class));
assertThat(type.getGeneric(1).getType(), equalTo((Type) Integer.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void serialize() throws Exception {
testSerialization(ResolvableType.forClass(List.class));
testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")));
testSerialization(ResolvableType.forMethodParameter(Methods.class.getMethod("charSequenceParameter", List.class), 0));
testSerialization(ResolvableType.forMethodReturnType(Methods.class.getMethod("charSequenceReturn")));
testSerialization(ResolvableType.forConstructorParameter(Constructors.class.getConstructor(List.class), 0));
testSerialization(ResolvableType.forField(Fields.class.getField("charSequenceList")).getGeneric());
ResolvableType deserializedNone = testSerialization(ResolvableType.NONE);
assertThat(deserializedNone, sameInstance(ResolvableType.NONE));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void elementTypePreserveContext() throws Exception {
TypeDescriptor desc = new TypeDescriptor(getClass().getField("listPreserveContext"));
assertEquals(Integer.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getType());
List<Integer> value = new ArrayList<>(3);
desc = desc.elementTypeDescriptor(value);
assertEquals(Integer.class, desc.getElementTypeDescriptor().getType());
assertNotNull(desc.getAnnotation(FieldAnnotation.class));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testSpr11219() throws Exception {
ResolvableType type = ResolvableType.forField(BaseProvider.class.getField("stuff"), BaseProvider.class);
assertTrue(type.getNested(2).isAssignableFrom(ResolvableType.forClass(BaseImplementation.class)));
assertEquals("java.util.Collection<org.springframework.core.ResolvableTypeTests$IBase<?>>", type.toString());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void fieldScalar() throws Exception {
TypeDescriptor typeDescriptor = new TypeDescriptor(getClass().getField("fieldScalar"));
assertFalse(typeDescriptor.isPrimitive());
assertFalse(typeDescriptor.isArray());
assertFalse(typeDescriptor.isCollection());
assertFalse(typeDescriptor.isMap());
assertEquals(Integer.class, typeDescriptor.getType());
assertEquals(Integer.class, typeDescriptor.getObjectType());
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void fieldMap() throws Exception {
TypeDescriptor desc = new TypeDescriptor(TypeDescriptorTests.class.getField("fieldMap"));
assertTrue(desc.isMap());
assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType());
assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
}
内容来源于网络,如有侵权,请联系作者删除!