java.lang.Class.getDeclaredField()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(350)

本文整理了Java中java.lang.Class.getDeclaredField()方法的一些代码示例,展示了Class.getDeclaredField()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.getDeclaredField()方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:getDeclaredField

Class.getDeclaredField介绍

[英]Returns a Field object for the field with the given name which is declared in the class represented by this Class.
[中]返回具有给定名称的字段的字段对象,该字段在此类表示的类中声明。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
    inputStream.defaultReadObject();
    try {
      this.field = this.declaringClass.getDeclaredField(this.fieldName);
    }
    catch (Throwable ex) {
      throw new IllegalStateException("Could not find original class structure", ex);
    }
  }
}

代码示例来源:origin: google/guava

static <T> FieldSetter<T> getFieldSetter(final Class<T> clazz, String fieldName) {
 try {
  Field field = clazz.getDeclaredField(fieldName);
  return new FieldSetter<T>(field);
 } catch (NoSuchFieldException e) {
  throw new AssertionError(e); // programmer error
 }
}

代码示例来源:origin: spring-projects/spring-framework

private static Map<?, ?> getSerializableFactoryMap() throws Exception {
  Field field = DefaultListableBeanFactory.class.getDeclaredField("serializableFactories");
  field.setAccessible(true);
  return (Map<?, ?>) field.get(DefaultListableBeanFactory.class);
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
public Map<String, String> getMap(Object target) {
  try {
    Field f = target.getClass().getDeclaredField(mapName);
    return (Map<String, String>) f.get(target);
  }
  catch (Exception ex) {
  }
  return null;
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private static Map<ClassLoader, WebApplicationContext> getCurrentContextPerThreadFromContextLoader() {
  try {
    Field field = ContextLoader.class.getDeclaredField("currentContextPerThread");
    field.setAccessible(true);
    return (Map<ClassLoader, WebApplicationContext>) field.get(null);
  }
  catch (Exception ex) {
    throw new IllegalStateException(ex);
  }
}

代码示例来源:origin: google/guava

private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
  throws Exception {
 // Make sure we are actually running with the expected helper implementation
 Class<?> abstractFutureClass = classLoader.loadClass(AbstractFuture.class.getName());
 Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
 helperField.setAccessible(true);
 assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
}

代码示例来源:origin: google/guava

private void checkHelperVersion(ClassLoader classLoader, String expectedHelperClassName)
  throws Exception {
 // Make sure we are actually running with the expected helper implementation
 Class<?> abstractFutureClass = classLoader.loadClass(AggregateFutureState.class.getName());
 Field helperField = abstractFutureClass.getDeclaredField("ATOMIC_HELPER");
 helperField.setAccessible(true);
 assertEquals(expectedHelperClassName, helperField.get(null).getClass().getSimpleName());
}

代码示例来源:origin: google/guava

@GwtIncompatible // reflection
private static int bucketsOf(HashMap<?, ?> hashMap) throws Exception {
 Field tableField = HashMap.class.getDeclaredField("table");
 tableField.setAccessible(true);
 Object[] table = (Object[]) tableField.get(hashMap);
 // In JDK8, table is set lazily, so it may be null.
 return table == null ? 0 : table.length;
}

代码示例来源:origin: spring-projects/spring-framework

private void assertIsCompiled(Expression expression) {
  try {
    Field field = SpelExpression.class.getDeclaredField("compiledAst");
    field.setAccessible(true);
    Object object = field.get(expression);
    assertNotNull(object);
  }
  catch (Exception ex) {
    fail(ex.toString());
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setUp() throws Exception {
  ExpressionWithConversionTests.typeDescriptorForListOfString = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfString"));
  ExpressionWithConversionTests.typeDescriptorForListOfInteger = new TypeDescriptor(ExpressionWithConversionTests.class.getDeclaredField("listOfInteger"));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void defaultRefreshCheckDelay() throws Exception {
  ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG);
  Advised advised = (Advised) context.getBean("testBean");
  AbstractRefreshableTargetSource targetSource =
      ((AbstractRefreshableTargetSource) advised.getTargetSource());
  Field field = AbstractRefreshableTargetSource.class.getDeclaredField("refreshCheckDelay");
  field.setAccessible(true);
  long delay = ((Long) field.get(targetSource)).longValue();
  assertEquals(5000L, delay);
}

代码示例来源:origin: google/guava

static Element field(String name) throws Exception {
 Element element = new Element(A.class.getDeclaredField(name));
 assertEquals(name, element.getName());
 assertEquals(A.class, element.getDeclaringClass());
 return element;
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void convertArrayToCollectionGenericTypeConversion() throws Exception {
  @SuppressWarnings("unchecked")
  List<Integer> result = (List<Integer>) conversionService.convert(new String[] {"1", "2", "3"}, TypeDescriptor
      .valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
  assertEquals(Integer.valueOf(1), result.get(0));
  assertEquals(Integer.valueOf(2), result.get(1));
  assertEquals(Integer.valueOf(3), result.get(2));
}

代码示例来源:origin: google/guava

public void testWildcardCaptured_field_upperBound() throws Exception {
 TypeToken<Holder<?>> type = new TypeToken<Holder<?>>() {};
 TypeToken<?> matrixType =
   type.resolveType(Holder.class.getDeclaredField("matrix").getGenericType());
 assertEquals(List[].class, matrixType.getRawType());
 assertThat(matrixType.getType()).isNotEqualTo(new TypeToken<List<?>[]>() {}.getType());
}

代码示例来源:origin: google/guava

public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasNoExplicitUpperBound()
  throws Exception {
 TypeToken<Counter<?>> type = new TypeToken<Counter<?>>() {};
 TypeToken<?> fieldType =
   type.resolveType(Counter.class.getDeclaredField("counts").getGenericType());
 Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments();
 assertThat(typeArgs).asList().hasSize(1);
 TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0];
 assertThat(captured.getBounds()).asList().containsExactly(Number.class);
 assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue();
 assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse();
}

代码示例来源:origin: google/guava

public void testWildcardCaptured_typeVariableDeclaresTypeBound_wildcardHasExplicitUpperBound()
  throws Exception {
 TypeToken<Counter<? extends Number>> type = new TypeToken<Counter<? extends Number>>() {};
 TypeToken<?> fieldType =
   type.resolveType(Counter.class.getDeclaredField("counts").getGenericType());
 Type[] typeArgs = ((ParameterizedType) fieldType.getType()).getActualTypeArguments();
 assertThat(typeArgs).asList().hasSize(1);
 TypeVariable<?> captured = (TypeVariable<?>) typeArgs[0];
 assertThat(captured.getBounds()).asList().containsExactly(Number.class);
 assertThat(new TypeToken<List<? extends Number>>() {}.isSupertypeOf(fieldType)).isTrue();
 assertThat(new TypeToken<List<? extends Iterable<?>>>() {}.isSupertypeOf(fieldType)).isFalse();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fieldList() throws Exception {
  TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfString"));
  assertFalse(typeDescriptor.isArray());
  assertEquals(List.class, typeDescriptor.getType());
  assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getType());
  assertEquals("java.util.List<java.lang.String>", typeDescriptor.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fieldComplexTypeDescriptor() throws Exception {
  TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString"));
  assertTrue(typeDescriptor.isArray());
  assertEquals(List.class,typeDescriptor.getElementTypeDescriptor().getType());
  assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals("java.util.List<java.lang.String>[]",typeDescriptor.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fieldListOfListOfString() throws Exception {
  TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("listOfListOfString"));
  assertFalse(typeDescriptor.isArray());
  assertEquals(List.class, typeDescriptor.getType());
  assertEquals(List.class, typeDescriptor.getElementTypeDescriptor().getType());
  assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals("java.util.List<java.util.List<java.lang.String>>", typeDescriptor.toString());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void fieldComplexTypeDescriptor2() throws Exception {
  TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("nestedMapField"));
  assertTrue(typeDescriptor.isMap());
  assertEquals(String.class,typeDescriptor.getMapKeyTypeDescriptor().getType());
  assertEquals(List.class, typeDescriptor.getMapValueTypeDescriptor().getType());
  assertEquals(Integer.class, typeDescriptor.getMapValueTypeDescriptor().getElementTypeDescriptor().getType());
  assertEquals("java.util.Map<java.lang.String, java.util.List<java.lang.Integer>>", typeDescriptor.toString());
}

相关文章

Class类方法