java.lang.reflect.Field.get()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.1k)|赞(0)|评价(0)|浏览(286)

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

Field.get介绍

[英]Returns the value of the field in the specified object. This reproduces the effect of object.fieldName

If the type of this field is a primitive type, the field value is automatically boxed.

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
[中]返回指定对象中字段的值。这再现了对象的效果。字段名
如果此字段的类型是基元类型,则字段值将自动装箱。
如果此字段是静态的,则忽略对象参数。否则,如果对象为null,则抛出NullPointerException。如果该对象不是该方法声明类的实例,则会引发IllegalArgumentException。
如果此字段对象正在强制访问控制(请参见AccessibleObject),并且无法从当前上下文访问此字段,则会引发IllegaAccessException。

代码示例

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

public RuntimeTestWalker(ShadowMatch shadowMatch) {
  try {
    ReflectionUtils.makeAccessible(residualTestField);
    this.runtimeTest = (Test) residualTestField.get(shadowMatch);
  }
  catch (IllegalAccessException ex) {
    throw new IllegalStateException(ex);
  }
}

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

@Override
@Nullable
public Object getValue() throws Exception {
  try {
    ReflectionUtils.makeAccessible(this.field);
    return this.field.get(getWrappedInstance());
  }
  catch (IllegalAccessException ex) {
    throw new InvalidPropertyException(getWrappedClass(),
        this.field.getName(), "Field is not accessible", ex);
  }
}

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

protected int getVarType(ReflectionVar v) {
    try {
      ReflectionUtils.makeAccessible(varTypeField);
      return (Integer) varTypeField.get(v);
    }
    catch (IllegalAccessException ex) {
      throw new IllegalStateException(ex);
    }
  }
}

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

static void assertConstantNameMatchesString(
  Class<?> clazz,
  ImmutableBiMap<String, String> specialCases,
  ImmutableSet<String> uppercaseAcronyms)
  throws IllegalAccessException {
 for (Field field : relevantFields(clazz)) {
  assertEquals(
    upperToHttpHeaderName(field.getName(), specialCases, uppercaseAcronyms), field.get(null));
 }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  void serialize(AbstractHessianOutput out, Object obj, Field field)
      throws IOException {
    String value = null;
    try {
      value = (String) field.get(obj);
    } catch (IllegalAccessException e) {
      log.log(Level.FINE, e.toString(), e);
    }
    out.writeString(value);
  }
}

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

@GwtIncompatible // reflection
public void testConstants_charset() throws Exception {
 for (Field field : getConstantFields()) {
  Optional<Charset> charset = ((MediaType) field.get(null)).charset();
  if (field.getName().endsWith("_UTF_8")) {
   assertThat(charset).hasValue(UTF_8);
  } else {
   assertThat(charset).isAbsent();
  }
 }
}

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

@Override
 public sun.misc.Unsafe run() throws Exception {
  Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
  for (java.lang.reflect.Field f : k.getDeclaredFields()) {
   f.setAccessible(true);
   Object x = f.get(null);
   if (k.isInstance(x)) {
    return k.cast(x);
   }
  }
  throw new NoSuchFieldError("the Unsafe");
 }
});

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

public ClassLoader getThrowawayClassLoader() {
  try {
    ClassLoader loader = this.cloneConstructor.newInstance(getClassLoader());
    // Clear out the transformers (copied as well)
    List<?> list = (List<?>) this.transformerList.get(loader);
    list.clear();
    return loader;
  }
  catch (InvocationTargetException ex) {
    throw new IllegalStateException("WebSphere CompoundClassLoader constructor failed", ex.getCause());
  }
  catch (Throwable ex) {
    throw new IllegalStateException("Could not construct WebSphere CompoundClassLoader", ex);
  }
}

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

@Override
 public MediaType apply(Field input) {
  try {
   return (MediaType) input.get(null);
  } catch (Exception e) {
   throw new RuntimeException(e);
  }
 }
});

代码示例来源: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

@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: 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: 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

@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: spring-projects/spring-framework

@Test
public void isDefaultJndiEnvironmentAvailableFalse() throws Exception {
  Field builderField = NamingManager.class.getDeclaredField("initctx_factory_builder");
  builderField.setAccessible(true);
  Object oldBuilder = builderField.get(null);
  builderField.set(null, null);
  try {
    assertThat(JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable(), equalTo(false));
  }
  finally {
    builderField.set(null, oldBuilder);
  }
}

代码示例来源:origin: libgdx/libgdx

/** Returns the value of the field on the supplied object. */
public Object get (Object obj) throws ReflectionException {
  try {
    return field.get(obj);
  } catch (IllegalArgumentException e) {
    throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e);
  } catch (IllegalAccessException e) {
    throw new ReflectionException("Illegal access to field: " + getName(), e);
  }
}

代码示例来源:origin: libgdx/libgdx

/** Returns the value of the field on the supplied object. */
public Object get (Object obj) throws ReflectionException {
  try {
    return field.get(obj);
  } catch (IllegalArgumentException e) {
    throw new ReflectionException("Object is not an instance of " + getDeclaringClass(), e);
  } catch (IllegalAccessException e) {
    throw new ReflectionException("Illegal access to field: " + getName(), e);
  }
}

相关文章

微信公众号

最新文章

更多