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

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

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

Field.setAccessible介绍

暂无

代码示例

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

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

代码示例来源:origin: alibaba/druid

public FieldInfo(Field field, String columnName, Field hashFor, String hashForType){
  this.field = field;
  this.columnName = columnName;
  this.hashFor = hashFor;
  this.hashForType = hashForType;
  field.setAccessible(true);
  if (hashFor != null) {
    hashFor.setAccessible(true);
  }
}

代码示例来源:origin: jenkinsci/jenkins

public AntWithFindResourceClassLoader(ClassLoader parent, boolean parentFirst) {
  super(parent, parentFirst);
  try {
    Field $pathComponents = AntClassLoader.class.getDeclaredField("pathComponents");
    $pathComponents.setAccessible(true);
    pathComponents = (Vector)$pathComponents.get(this);
  } catch (NoSuchFieldException | IllegalAccessException e) {
    throw new Error(e);
  }
}

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

public long getContactAddr(Contact contact) {
    try {
      Field addrField = contact.getClass().getDeclaredField("addr");
      addrField.setAccessible(true);
      return addrField.getLong(contact);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

@java.lang.SuppressWarnings("unchecked")
private static void setCurrentRequest(RequestImpl request) {
  try {
    Field field = Stapler.class.getDeclaredField("CURRENT_REQUEST");
    field.setAccessible(true);
    ((ThreadLocal<RequestImpl>) field.get(null)).set(request);
  } catch (Exception x) {
    LOG.log(Level.WARNING, "cannot mock Stapler request", x);
  }
}

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

private static @Nullable Field getInheritableThreadLocalsField() {
 try {
  Field inheritableThreadLocals = Thread.class.getDeclaredField("inheritableThreadLocals");
  inheritableThreadLocals.setAccessible(true);
  return inheritableThreadLocals;
 } catch (Throwable t) {
  logger.log(
    Level.INFO,
    "Couldn't access Thread.inheritableThreadLocals. Reference finalizer threads will "
      + "inherit thread local values.");
  return null;
 }
}

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

/**
 * 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: 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);
  }
}

相关文章

微信公众号

最新文章

更多