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

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

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

Method.setAccessible介绍

暂无

代码示例

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

public void setAccessible (boolean accessible) {
  method.setAccessible(accessible);
}

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

public static int getNextId(int type) throws Exception {
    if (method == null) {
      method = TraceObject.class.getDeclaredMethod("getNextId", int.class);
      method.setAccessible(true);
    }

    return (Integer) method.invoke(null, type);
  }
}

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

@Override
  public Object run() {
    try {
      // Invoke the helper to load the native library, if succeed, then the native
      // library belong to the specified ClassLoader.
      Method method = helper.getMethod("loadLibrary", String.class, boolean.class);
      method.setAccessible(true);
      return method.invoke(null, name, absolute);
    } catch (Exception e) {
      return e;
    }
  }
});

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

/** {@link BoundObjectTable#releaseMe} just cannot work the way we need it to. */
private void release() {
  try {
    Method release = BoundObjectTable.Table.class.getDeclaredMethod("release", String.class);
    release.setAccessible(true);
    release.invoke(boundObjectTable, boundId);
  } catch (Exception x) {
    LOG.log(Level.WARNING, "failed to unbind " + boundId, x);
  }
}

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

/**
 * Make the given method 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 method the method to make accessible
 * @see java.lang.reflect.Method#setAccessible
 */
@SuppressWarnings("deprecation")  // on JDK 9
public static void makeAccessible(Method method) {
  if ((!Modifier.isPublic(method.getModifiers()) ||
      !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
    method.setAccessible(true);
  }
}

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

private Subscriber(EventBus bus, Object target, Method method) {
 this.bus = bus;
 this.target = checkNotNull(target);
 this.method = method;
 method.setAccessible(true);
 this.executor = bus.executor();
}

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

public static void callInjectViews(Object activity) {
    try {
      Class<?> viewMembersInjectorClass = Class.forName("roboguice.inject.ViewListener$ViewMembersInjector");
      Method injectViewsMethod = viewMembersInjectorClass.getDeclaredMethod("injectViews", Object.class);
      injectViewsMethod.setAccessible(true);
      injectViewsMethod.invoke(null, activity);
    } catch (ClassNotFoundException | InvocationTargetException | IllegalArgumentException | IllegalAccessException | SecurityException | NoSuchMethodException e) {
      throw new RuntimeException("Could not invoke RoboGuice method!", e);
    }
  }
}

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

private void initMethod() {
  Method[] methodsToExport = null;
  methodsToExport = this.serviceInterfaceClass.getMethods();
  for (Method method : methodsToExport) {
    method.setAccessible(true);
    List<ProviderMethodModel> methodModels = methods.get(method.getName());
    if (methodModels == null) {
      methodModels = new ArrayList<ProviderMethodModel>(1);
      methods.put(method.getName(), methodModels);
    }
    methodModels.add(new ProviderMethodModel(method, serviceName));
  }
}

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

private void initMethod() {
  Method[] methodsToExport = null;
  methodsToExport = this.serviceInterfaceClass.getMethods();
  for (Method method : methodsToExport) {
    method.setAccessible(true);
    List<ProviderMethodModel> methodModels = methods.get(method.getName());
    if (methodModels == null) {
      methodModels = new ArrayList<ProviderMethodModel>(1);
      methods.put(method.getName(), methodModels);
    }
    methodModels.add(new ProviderMethodModel(method, serviceName));
  }
}

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

public long getFixtureAddrB(Contact contact) {
  try {
    long addr =getContactAddr(contact);
    
    Method getFixtureB = contact.getClass().getDeclaredMethod("jniGetFixtureB", long.class);
    getFixtureB.setAccessible(true);
    return (Long) getFixtureB.invoke(contact, addr);
  } catch (Exception e) {
    e.printStackTrace();
    return 0;
  }
}

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

public long getFixtureAddrA(Contact contact) {
  try {
    long addr = getContactAddr(contact);
    
    Method getFixtureA = contact.getClass().getDeclaredMethod("jniGetFixtureA", long.class);
    getFixtureA.setAccessible(true);
    return (Long) getFixtureA.invoke(contact, addr);
  } catch (Exception e) {
    e.printStackTrace();
    return 0;
  }
}

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

private static Method init() {
    try {
      Method m = ClassLoader.class.getDeclaredMethod("findResource", String.class);
      m.setAccessible(true);
      return m;
    } catch (NoSuchMethodException e) {
      throw (Error)new NoSuchMethodError().initCause(e);
    }
  }
}

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

public static Map<String, Method> getBeanPropertyReadMethods(Class cl) {
    Map<String, Method> properties = new HashMap<String, Method>();
    for (; cl != null; cl = cl.getSuperclass()) {
      Method[] methods = cl.getDeclaredMethods();
      for (Method method : methods) {
        if (isBeanPropertyReadMethod(method)) {
          method.setAccessible(true);
          String property = getPropertyNameFromBeanReadMethod(method);
          properties.put(property, method);
        }
      }
    }

    return properties;
  }
}

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

public static Map<String, Method> getBeanPropertyReadMethods(Class cl) {
    Map<String, Method> properties = new HashMap<String, Method>();
    for (; cl != null; cl = cl.getSuperclass()) {
      Method[] methods = cl.getDeclaredMethods();
      for (Method method : methods) {
        if (isBeanPropertyReadMethod(method)) {
          method.setAccessible(true);
          String property = getPropertyNameFromBeanReadMethod(method);
          properties.put(property, method);
        }
      }
    }

    return properties;
  }
}

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

private Throwable tryInternalFastPathGetFailure(Future<?> future) throws Exception {
  Method tryInternalFastPathGetFailureMethod =
    abstractFutureClass.getDeclaredMethod("tryInternalFastPathGetFailure");
  tryInternalFastPathGetFailureMethod.setAccessible(true);
  return (Throwable) tryInternalFastPathGetFailureMethod.invoke(future);
 }
}

代码示例来源:origin: org.mockito/mockito-core

@Override
public Object invoke() throws Throwable {
  if (!Modifier.isPublic(origin.getDeclaringClass().getModifiers() & origin.getModifiers())) {
    origin.setAccessible(true);
  }
  selfCallInfo.set(instanceRef.get());
  return tryInvoke(origin, instanceRef.get(), arguments);
}

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

/**
 * Verifies that {@code method} produces a {@link NullPointerException} or {@link
 * UnsupportedOperationException} when the parameter in position {@code paramIndex} is null. If
 * this parameter is marked nullable, this method does nothing.
 *
 * @param instance the instance to invoke {@code method} on, or null if {@code method} is static
 */
public void testMethodParameter(
  @Nullable final Object instance, final Method method, int paramIndex) {
 method.setAccessible(true);
 testParameter(instance, invokable(instance, method), paramIndex, method.getDeclaringClass());
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
public static <E extends Enum<E>> void checkEnum(Class<E> enumClass) {
  try {
    Method m = enumClass.getMethod("values");
    m.setAccessible(true);
    Method e = enumClass.getMethod("valueOf", String.class);
    m.setAccessible(true);
    for (Enum<E> o : (Enum<E>[])m.invoke(null)) {
      assertSame(o, e.invoke(null, o.name()));
    }
  } catch (Throwable ex) {
    throw ExceptionHelper.wrapOrThrow(ex);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void hashSetCallableEnum() {
  // inlined TestHelper.checkEnum due to access restrictions
  try {
    Method m = Functions.HashSetCallable.class.getMethod("values");
    m.setAccessible(true);
    Method e = Functions.HashSetCallable.class.getMethod("valueOf", String.class);
    e.setAccessible(true);
    for (Enum<HashSetCallable> o : (Enum<HashSetCallable>[])m.invoke(null)) {
      assertSame(o, e.invoke(null, o.name()));
    }
  } catch (Throwable ex) {
    throw ExceptionHelper.wrapOrThrow(ex);
  }
}

代码示例来源:origin: ReactiveX/RxJava

@SuppressWarnings("unchecked")
@Test
public void naturalComparatorEnum() {
  // inlined TestHelper.checkEnum due to access restrictions
  try {
    Method m = Functions.NaturalComparator.class.getMethod("values");
    m.setAccessible(true);
    Method e = Functions.NaturalComparator.class.getMethod("valueOf", String.class);
    e.setAccessible(true);
    for (Enum<NaturalComparator> o : (Enum<NaturalComparator>[])m.invoke(null)) {
      assertSame(o, e.invoke(null, o.name()));
    }
  } catch (Throwable ex) {
    throw ExceptionHelper.wrapOrThrow(ex);
  }
}

相关文章

微信公众号

最新文章

更多