Java获取类中的所有方法
一、获取当前类定义的所有方法,不包括父类和接口的
class.getDeclaredMethods()
eg:
import java.lang.reflect.Method; public class testH { //获取java类的所有方法并打印出来 public static void main(String args[]) { Class c = SSOUtil.class; Method[] m = c.getDeclaredMethods(); for (int i = 0; i < m.length; i++) { System.out.println(m[i].getName()); } } }
会返回当前类定义的所有方法(包括私有的、静态的、抽象的),但是不会返回接口和父类中定义的方法
二、获取当前类所有的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; }
转载自:https://blog.csdn.net/u011983531/article/details/80248945
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示