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

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

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

Method.isAccessible介绍

暂无

代码示例

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

public boolean isAccessible () {
  return method.isAccessible();
}

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

public boolean isAccessible () {
  return method.isAccessible();
}

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

/**
 * 设置方法为可访问
 * 
 * @param method 方法
 * @return 方法
 */
public static Method setAccessible(Method method) {
  if (null != method && false == method.isAccessible()) {
    method.setAccessible(true);
  }
  return method;
}

代码示例来源:origin: looly/hutool

/**
 * 设置方法为可访问
 * 
 * @param method 方法
 * @return 方法
 */
public static Method setAccessible(Method method) {
  if (null != method && false == method.isAccessible()) {
    method.setAccessible(true);
  }
  return method;
}

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

@Override
  public Object run() {
    if (!m.isAccessible()) {
      m.setAccessible(true);
    }
    return m;
  }
};

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

@Override
  public Object run() {
    if (!m.isAccessible()) {
      m.setAccessible(true);
    }
    return m;
  }
};

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

public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) {
  try {
    // backward compatibility to spring 2.0.1
    Method method = applicationContext.getClass().getMethod("addApplicationListener", ApplicationListener.class);
    method.invoke(applicationContext, listener);
    return true;
  } catch (Throwable t) {
    if (applicationContext instanceof AbstractApplicationContext) {
      try {
        // backward compatibility to spring 2.0.1
        Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", ApplicationListener.class);
        if (!method.isAccessible()) {
          method.setAccessible(true);
        }
        method.invoke(applicationContext, listener);
        return true;
      } catch (Throwable t2) {
        // ignore
      }
    }
  }
  return false;
}

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

public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) {
  try {
    // backward compatibility to spring 2.0.1
    Method method = applicationContext.getClass().getMethod("addApplicationListener", ApplicationListener.class);
    method.invoke(applicationContext, listener);
    return true;
  } catch (Throwable t) {
    if (applicationContext instanceof AbstractApplicationContext) {
      try {
        // backward compatibility to spring 2.0.1
        Method method = AbstractApplicationContext.class.getDeclaredMethod("addListener", ApplicationListener.class);
        if (!method.isAccessible()) {
          method.setAccessible(true);
        }
        method.invoke(applicationContext, listener);
        return true;
      } catch (Throwable t2) {
        // ignore
      }
    }
  }
  return false;
}

代码示例来源:origin: springside/springside4

/**
 * 改变private/protected的方法为可访问,尽量不进行改变,避免JDK的SecurityManager抱怨。
 */
public static void makeAccessible(Method method) {
  if (!method.isAccessible() && (!Modifier.isPublic(method.getModifiers())
      || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))) {
    method.setAccessible(true);
  }
}

代码示例来源:origin: mrniko/netty-socketio

private void makeAccessible(Method method) {
  if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
      && !method.isAccessible()) {
    method.setAccessible(true);
  }
}

代码示例来源:origin: changmingxie/tcc-transaction

public static void makeAccessible(Method method) {
  if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers())) && !method.isAccessible()) {
    method.setAccessible(true);
  }
}

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

private static Object invoke(Method method, Object instance) {
  try {
    if (!method.isAccessible()) {
      setAccessible(method);
    }
    return method.invoke(instance);
  } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
    throw new RuntimeException(
        "Error checking value of member method " + method.getName() + " on " + method.getDeclaringClass(), e);
  }
}

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

private static Object invoke(Method method, Object instance) {
  try {
    if (!method.isAccessible()) {
      setAccessible(method);
    }
    return method.invoke(instance);
  } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
    throw new RuntimeException(
        "Error checking value of member method " + method.getName() + " on " + method.getDeclaringClass(), e);
  }
}

代码示例来源:origin: prestodb/presto

Object readResolve() {
  Class<?> clazz = _serialization.clazz;
  try {
    Method m = clazz.getDeclaredMethod(_serialization.name,
        _serialization.args);
    // 06-Oct-2012, tatu: Has "lost" its security override, may need to force back
    if (!m.isAccessible()) {
      ClassUtil.checkAndFixAccess(m, false);
    }
    return new AnnotatedMethod(null, m, null, null);
  } catch (Exception e) {
    throw new IllegalArgumentException("Could not find method '"+_serialization.name
          +"' from Class '"+clazz.getName());
  }
}

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

Object readResolve() {
  Class<?> clazz = _serialization.clazz;
  try {
    Method m = clazz.getDeclaredMethod(_serialization.name,
        _serialization.args);
    // 06-Oct-2012, tatu: Has "lost" its security override, may need to force back
    if (!m.isAccessible()) {
      ClassUtil.checkAndFixAccess(m, false);
    }
    return new AnnotatedMethod(null, m, null, null);
  } catch (Exception e) {
    throw new IllegalArgumentException("Could not find method '"+_serialization.name
          +"' from Class '"+clazz.getName());
  }
}

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

throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onreturn callback config , but no such " + (onReturnMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
if (!onReturnMethod.isAccessible()) {
  onReturnMethod.setAccessible(true);

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

throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a onreturn callback config , but no such " + (onReturnMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
if (!onReturnMethod.isAccessible()) {
  onReturnMethod.setAccessible(true);

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

private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
  final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
  if (asyncMethodInfo == null) {
    return;
  }
  final Method onInvokeMethod = asyncMethodInfo.getOninvokeMethod();
  final Object onInvokeInst = asyncMethodInfo.getOninvokeInstance();
  if (onInvokeMethod == null && onInvokeInst == null) {
    return;
  }
  if (onInvokeMethod == null || onInvokeInst == null) {
    throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a oninvoke callback config , but no such " + (onInvokeMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
  }
  if (!onInvokeMethod.isAccessible()) {
    onInvokeMethod.setAccessible(true);
  }
  Object[] params = invocation.getArguments();
  try {
    onInvokeMethod.invoke(onInvokeInst, params);
  } catch (InvocationTargetException e) {
    fireThrowCallback(invoker, invocation, e.getTargetException());
  } catch (Throwable e) {
    fireThrowCallback(invoker, invocation, e);
  }
}

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

private void fireInvokeCallback(final Invoker<?> invoker, final Invocation invocation) {
  final ConsumerMethodModel.AsyncMethodInfo asyncMethodInfo = getAsyncMethodInfo(invoker, invocation);
  if (asyncMethodInfo == null) {
    return;
  }
  final Method onInvokeMethod = asyncMethodInfo.getOninvokeMethod();
  final Object onInvokeInst = asyncMethodInfo.getOninvokeInstance();
  if (onInvokeMethod == null && onInvokeInst == null) {
    return;
  }
  if (onInvokeMethod == null || onInvokeInst == null) {
    throw new IllegalStateException("service:" + invoker.getUrl().getServiceKey() + " has a oninvoke callback config , but no such " + (onInvokeMethod == null ? "method" : "instance") + " found. url:" + invoker.getUrl());
  }
  if (!onInvokeMethod.isAccessible()) {
    onInvokeMethod.setAccessible(true);
  }
  Object[] params = invocation.getArguments();
  try {
    onInvokeMethod.invoke(onInvokeInst, params);
  } catch (InvocationTargetException e) {
    fireThrowCallback(invoker, invocation, e.getTargetException());
  } catch (Throwable e) {
    fireThrowCallback(invoker, invocation, e);
  }
}

相关文章

微信公众号

最新文章

更多