Java获取类中的所有方法

news/2024/5/19 5:44:18 标签: Method, 反射

一、获取当前类定义的所有方法,不包括父类和接口的

class.getDeclaredMethods()

  会返回当前类定义的所有方法(包括私有的、静态的、抽象的),但是不会返回接口和父类中定义的方法

二、获取当前类所有的public方法,包括父类和接口的

class.getMethods()

  会返回当前类所有的public方法,包括接口和父类中定义的public方法。

三、获取当前类中所有的方法,包括父类和接口的

private Method[] getClassMethods(Class<?> cls) {
    Map<String, Method> uniqueMethods = new HashMap<String, Method>();
    Class<?> currentClass = cls;
    while (currentClass != null && currentClass != Object.class) {
      addUniqueMethods(uniqueMethods, currentClass.getDeclaredMethods());

      //获取接口中的所有方法
      Class<?>[] interfaces = currentClass.getInterfaces();
      for (Class<?> anInterface : interfaces) {
        addUniqueMethods(uniqueMethods, anInterface.getMethods());
      }
      //获取父类,继续while循环
      currentClass = currentClass.getSuperclass();
    }

    Collection<Method> methods = uniqueMethods.values();

    return methods.toArray(new Method[methods.size()]);
  }

  private void addUniqueMethods(Map<String, Method> uniqueMethods, Method[] methods) {
    for (Method currentMethod : methods) {
      if (!currentMethod.isBridge()) {
      //获取方法的签名,格式是:返回值类型#方法名称:参数类型列表
        String signature = getSignature(currentMethod);
        //检查是否在子类中已经添加过该方法,如果在子类中已经添加过,则表示子类覆盖了该方法,无须再向uniqueMethods集合中添加该方法了
        if (!uniqueMethods.containsKey(signature)) {
          if (canControlMemberAccessible()) {
            try {
              currentMethod.setAccessible(true);
            } catch (Exception e) {
              // Ignored. This is only a final precaution, nothing we can do.
            }
          }
          uniqueMethods.put(signature, currentMethod);
        }
      }
    }
  }

  private String getSignature(Method method) {
    StringBuilder sb = new StringBuilder();
    Class<?> returnType = method.getReturnType();
    if (returnType != null) {
      sb.append(returnType.getName()).append('#');
    }
    sb.append(method.getName());
    Class<?>[] parameters = method.getParameterTypes();
    for (int i = 0; i < parameters.length; i++) {
      if (i == 0) {
        sb.append(':');
      } else {
        sb.append(',');
      }
      sb.append(parameters[i].getName());
    }
    return sb.toString();
  }

  /**
   * Checks whether can control member accessible.
   *
   * @return If can control member accessible, it return {@literal true}
   * @since 3.5.0
   */
  public static boolean canControlMemberAccessible() {
    try {
      SecurityManager securityManager = System.getSecurityManager();
      if (null != securityManager) {
        securityManager.checkPermission(new ReflectPermission("suppressAccessChecks"));
      }
    } catch (SecurityException e) {
      return false;
    }
    return true;
  }

http://www.niftyadmin.cn/n/1530540.html

相关文章

Java Type接口 运行时获取泛型类型

一、Type接口 Type是所有类型的父接口&#xff0c;他有4个子接口和一个实现类。 Class比较常见&#xff0c;它表示的是原始类型。Class类的对象表示JVM中的一个类或接口。每个Java类在JVM里都表现为一个Class对象&#xff0c;可以通过“类名.class”、“对象.getClass()”、…

何为前后端分离?

一、前后端不分离 在前后端不分离架构中&#xff0c;所有的静态资源和业务代码统一部署在同一台服务器上。服务器接收到浏览器的请求后&#xff0c;进行处理得到数据&#xff0c;然后将数据填充到静态页面中&#xff0c;最终返回给浏览器。 二、前后端分离 实现前后端分离后&a…

Spring AOP原理解析——经典的基于代理的AOP是如何实现的?

Spring实现AOP有4种方式&#xff0c;感兴趣的可以查看下面的博文&#xff1a; https://blog.csdn.net/u011983531/article/details/49391129 在阅读这篇文章之前&#xff0c;请先行了解一下动态代理的相关知识&#xff0c;因为这是AOP 的基础。 一.经典的基于代理的AOP 首先…

Spring AOP原理解析——基于自动代理AOP是如何实现的?

在上一篇&#xff08;https://blog.csdn.net/u011983531/article/details/80359304&#xff09;我们介绍了基于经典代理的AOP实现方案&#xff0c;在这一篇中&#xff0c;我们将看看基于自动代理的AOP是如何实现的。 //基于自动代理AOP <bean id"sleepHelper" cl…

Spring AOP原理解析——基于AOP标签的AOP是如何实现的?

前面我们讲过了经典的基于代理的AOP和基于自动代理的AOP是如何实现的&#xff0c;有了前面的基础&#xff0c;学习基于AOP标签的AOP的实现原理&#xff0c;就很简单了。 经典的基于代理的AOP&#xff1a; https://blog.csdn.net/u011983531/article/details/80359304 基于自…

Spring AOP原理解析——Spring事务

Spring申明式事务有两种方式&#xff1a; 基于配置文件基于注解 一、基于配置文件的事务 <!-- 会重复读&#xff0c;不会脏读事务 --> <tx:advice id"defaultTxAdvice" transaction-manager"transactionManager"><tx:attributes><…

Spring MVC原理解析——MultipartRequest解析

下面是SpringMVC处理请求的流程图&#xff0c;在进行正式的映射之前&#xff0c;Spring MVC会先判断当前请求是不是multipart/form-data请求&#xff0c;如果是&#xff0c;会将request解析为MultipartHttpServletRequest。 那么SpringMVC是如何将HttpServletRequest解析为M…