寻回和解析方法的修饰符

xiaoxiao2026-04-16  3

可能的方法的修饰符: 访问修饰符:public ,protected,private; 限制一个实例:static; 禁止修改:final; 要求重写:abstract; 线程安全:synchronized; 指明实现实现其它语言:native; 要求进行准确浮点运算行为:strictpf. 以下示例获取到被给方法名的修饰符,也显示了是否为synthetic(编译器生成的). import java.lang.reflect.Method;import java.lang.reflect.Modifier;import static java.lang.System.out;public class MethodModifierSpy { private static int count; private static synchronized void inc() {count++;} private static synchronized int cnt() {return count;} public static void main(String... args) { try { Class<?> c = Class.forName(args[0]); Method[] allMethods = c.getDeclaredMethods(); for(Method m: allMethods) { if(!m.getName().equals(args[1])) { continue; } out.format("%s%n",m.toGenericString()); out.format(" Modifiers: %s%n", Modifier.toString(m.getModifiers())); out.format(" [ synthetic = %-5b var-args =%-5b bridge=%-5b]%n", m.isSynthetic(),m.isVarArgs(),m.isBridge()); inc(); }//for out.format("%d matching overload%s found%n",cnt(), (cnt() ==1?"":"s")); } catch(ClassNotFoundException x) { x.printStackTrace(); } }//main}//class
转载请注明原文地址: https://www.6miu.com/read-5047429.html

最新回复(0)