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

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

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

Field.set介绍

[英]Sets the value of the field in the specified object to the value. This reproduces the effect of object.fieldName = value

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.

If the field type is a primitive type, the value is automatically unboxed. If the unboxing fails, an IllegalArgumentException is thrown. If the value cannot be converted to the field type via a widening conversion, an IllegalArgumentException is thrown.
[中]将指定对象中字段的值设置为值。这再现了对象的效果。字段名=值
如果此字段是静态的,则忽略对象参数。否则,如果对象为null,则抛出NullPointerException。如果该对象不是该方法声明类的实例,则会引发IllegalArgumentException。
如果此字段对象正在强制访问控制(请参见AccessibleObject),并且无法从当前上下文访问此字段,则会引发IllegaAccessException。
如果字段类型是基元类型,则该值将自动取消绑定。如果取消装箱失败,将抛出IllegalArgumentException。如果无法通过加宽转换将值转换为字段类型,则会引发IllegalArgumentException。

代码示例

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

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

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

import java.lang.reflect.*;

public class EverythingIsTrue {
  static void setFinalStatic(Field field, Object newValue) throws Exception {
   field.setAccessible(true);

   Field modifiersField = Field.class.getDeclaredField("modifiers");
   modifiersField.setAccessible(true);
   modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);

   field.set(null, newValue);
  }
  public static void main(String args[]) throws Exception {      
   setFinalStatic(Boolean.class.getField("FALSE"), true);

   System.out.format("Everything is %s", false); // "Everything is true"
  }
}

代码示例来源: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: spring-projects/spring-framework

@Override
  public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
    TestBean tb = (TestBean) bean;
    try {
      Field f = TestBean.class.getDeclaredField("name");
      f.setAccessible(true);
      f.set(tb, nameSetOnField);
      return !skipPropertyPopulation;
    }
    catch (Exception ex) {
      fail("Unexpected exception: " + ex);
      // Keep compiler happy about return
      throw new IllegalStateException();
    }
  }
});

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

void set(T instance, int value) {
  try {
   field.set(instance, value);
  } catch (IllegalAccessException impossible) {
   throw new AssertionError(impossible);
  }
 }
}

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

void set(T instance, Object value) {
 try {
  field.set(instance, value);
 } catch (IllegalAccessException impossible) {
  throw new AssertionError(impossible);
 }
}

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

@Override
@Nullable
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
  Field field = ReflectionUtils.findField(obj.getClass(), BEAN_FACTORY_FIELD);
  Assert.state(field != null, "Unable to find generated BeanFactory field");
  field.set(obj, args[0]);
  // Does the actual (non-CGLIB) superclass implement BeanFactoryAware?
  // If so, call its setBeanFactory() method. If not, just exit.
  if (BeanFactoryAware.class.isAssignableFrom(ClassUtils.getUserClass(obj.getClass().getSuperclass()))) {
    return proxy.invokeSuper(obj, args);
  }
  return null;
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    java.sql.Time value = null;
    try {
      java.util.Date date = (java.util.Date) in.readObject();
      if (date != null) value = new java.sql.Time(date.getTime());
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    Object value = null;
    try {
      value = in.readObject(_field.getType());
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    java.sql.Timestamp value = null;
    try {
      java.util.Date date = (java.util.Date) in.readObject();
      if (date != null)
        value = new java.sql.Timestamp(date.getTime());
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    java.sql.Date value = null;
    try {
      java.util.Date date = (java.util.Date) in.readObject();
      if (date != null)
        value = new java.sql.Date(date.getTime());
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

代码示例来源: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,
          isPrimitive(types[1]) ? (Class<?>)types[1] : null
        );
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

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

@Override
  void deserialize(AbstractHessianInput in, Object obj)
    throws IOException {
    String value = null;
    try {
      value = in.readString();
      _field.set(obj, value);
    } catch (Exception e) {
      logDeserializeError(_field, obj, value, e);
    }
  }
}

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

/** Sets the value of the field on the supplied object. */
public void set (Object obj, Object value) throws ReflectionException {
  try {
    field.set(obj, value);
  } catch (IllegalArgumentException e) {
    throw new ReflectionException("Argument not valid for field: " + getName(), e);
  } catch (IllegalAccessException e) {
    throw new ReflectionException("Illegal access to field: " + getName(), e);
  }
}

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

/** Sets the value of the field on the supplied object. */
public void set (Object obj, Object value) throws ReflectionException {
  try {
    field.set(obj, value);
  } catch (IllegalArgumentException e) {
    throw new ReflectionException("Argument not valid for field: " + getName(), e);
  } catch (IllegalAccessException e) {
    throw new ReflectionException("Illegal access to field: " + getName(), e);
  }
}

相关文章

微信公众号

最新文章

更多