java.lang.reflect.Field类的使用及代码示例

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

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

Field介绍

[英]This class represents a field. Information about the field can be accessed, and the field's value can be accessed dynamically.
[中]此类表示一个字段。可以访问有关字段的信息,并且可以动态访问字段的值。

代码示例

代码示例来源: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: apache/incubator-dubbo

public static Map<String, Field> getBeanPropertyFields(Class cl) {
  Map<String, Field> properties = new HashMap<String, Field>();
  for (; cl != null; cl = cl.getSuperclass()) {
    Field[] fields = cl.getDeclaredFields();
    for (Field field : fields) {
      if (Modifier.isTransient(field.getModifiers())
          || Modifier.isStatic(field.getModifiers())) {
        continue;
      }
      field.setAccessible(true);
      properties.put(field.getName(), field);
    }
  }
  return properties;
}

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

@Override
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
  Class<?> referenceClass = field.getType();
  referenceBean = buildReferenceBean(reference, referenceClass);
  ReflectionUtils.makeAccessible(field);
  field.set(bean, referenceBean.getObject());
}

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

public FieldTypeProvider(Field field) {
  this.fieldName = field.getName();
  this.declaringClass = field.getDeclaringClass();
  this.field = field;
}

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

public static boolean isPublicInstanceField(Field field) {
  return Modifier.isPublic(field.getModifiers())
      && !Modifier.isStatic(field.getModifiers())
      && !Modifier.isFinal(field.getModifiers())
      && !field.isSynthetic();
}

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

/**
 * Make the given field accessible, explicitly setting it accessible if
 * necessary. The {@code setAccessible(true)} method is only called
 * when actually necessary, to avoid unnecessary conflicts with a JVM
 * SecurityManager (if active).
 * @param field the field to make accessible
 * @see java.lang.reflect.Field#setAccessible
 */
@SuppressWarnings("deprecation")  // on JDK 9
public static void makeAccessible(Field field) {
  if ((!Modifier.isPublic(field.getModifiers()) ||
      !Modifier.isPublic(field.getDeclaringClass().getModifiers()) ||
      Modifier.isFinal(field.getModifiers())) && !field.isAccessible()) {
    field.setAccessible(true);
  }
}

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

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

/**
 * Resets MBeanServerFactory and ManagementFactory to a known consistent state.
 * This involves releasing all currently registered MBeanServers and resetting
 * the platformMBeanServer to null.
 */
public static void resetMBeanServers() throws Exception {
  for (MBeanServer server : MBeanServerFactory.findMBeanServer(null)) {
    MBeanServerFactory.releaseMBeanServer(server);
  }
  Field field = ManagementFactory.class.getDeclaredField("platformMBeanServer");
  field.setAccessible(true);
  field.set(null, null);
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    Object value = null;
    try {
      Type[] types = ((ParameterizedType)_field.getGenericType()).getActualTypeArguments();
      value = in.readObject(_field.getType(),
        isPrimitive(types[0]) ? (Class<?>)types[0] : null
      );
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

代码示例来源:origin: stackoverflow.com

Field field = targetClass.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(object, value);

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

public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass) {
  try {
    Field field = getDeclaredField(clazz, fieldName);
    if (!field.isAccessible()) {
      field.setAccessible(true);
    }
    return field.getAnnotation(annotationClass);
  } catch (NoSuchFieldException e) {
    return null;
  }
}

代码示例来源:origin: skylot/jadx

private void readAndroidRStyleClass() {
  try {
    Class<?> rStyleCls = Class.forName(ANDROID_R_STYLE_CLS);
    for (Field f : rStyleCls.getFields()) {
      styleMap.put(f.getInt(f.getType()), f.getName());
    }
  } catch (Exception th) {
    LOG.error("Android R class loading failed", th);
  }
}

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

@Override
 public int compare(Field left, Field right) {
  return left.getName().compareTo(right.getName());
 }
};

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

static void logDeserializeError(Field field, Object obj, Object value,
  Throwable e)
  throws IOException {
  String fieldName = (field.getDeclaringClass().getName()
    + "." + field.getName());
  if (e instanceof HessianFieldException)
    throw (HessianFieldException) e;
  else if (e instanceof IOException)
    throw new HessianFieldException(fieldName + ": " + e.getMessage(), e);
  if (value != null)
    throw new HessianFieldException(fieldName + ": " + value.getClass().getName() + " (" + value + ")"
      + " cannot be assigned to '" + field.getType().getName() + "'", e);
  else
    throw new HessianFieldException(fieldName + ": " + field.getType().getName() + " cannot be assigned from null", e);
}

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

private boolean isTransient(Class c, Field field) {
  if (Modifier.isTransient(field.getModifiers()))
    return true;
  while (c.getName().indexOf("$") >= 0) {
    c = c.getSuperclass(); // patch fuer reallive queries, kontraktor spore
  }
  if ( field.getName().startsWith("this$") && c.getAnnotation(AnonymousTransient.class) != null )
    return true;
  return (c.getAnnotation(Transient.class) != null && field.getAnnotation(Serialize.class) == null);
}

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

protected void storeIndex(Field field, Object me, Object arg) {
  if (field.getAnnotation(RIndex.class) != null) {
    NamingScheme namingScheme = objectBuilder.getNamingScheme(me.getClass().getSuperclass());
    String indexName = namingScheme.getIndexName(me.getClass().getSuperclass(), field.getName());
    RSetMultimap<Object, Object> map = redisson.getSetMultimap(indexName, namingScheme.getCodec());
    map.put(arg, ((RLiveObject) me).getLiveObjectId());
  }
}

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

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

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

private FieldSetter(Field field) {
 this.field = field;
 field.setAccessible(true);
}

相关文章

微信公众号

最新文章

更多