用于
Class.getDeclaredMethods()从类或接口获取所有方法(私有方法或其他方法)的列表。
Class c = ob.getClass();for (Method method : c.getDeclaredMethods()) { if (method.getAnnotation(PostConstruct.class) != null) { System.out.println(method.getName()); }}注意:
这不包括继承的方法。使用
Class.getMethods()了点。它将返回所有
公共 方法(是否继承)。
要全面列出类可以访问的所有内容(包括继承的方法),您需要遍历其扩展的类树。所以:
Class c = ob.getClass();for (Class c = ob.getClass(); c != null; c = c.getSuperclass()) { for (Method method : c.getDeclaredMethods()) { if (method.getAnnotation(PostConstruct.class) != null) { System.out.println(c.getName() + "." + method.getName()); } }}


