// 本地实现 // Method.invoke -> DelegatingMethodAccessorImpl.invoke // -> NativeMethodAccessorImpl.invoke -> NativeMethodAccessorImpl.invoke0 java.lang.Exception: #0 at me.zhongmingmao.basic.reflect.V0.target(V0.java:7) -- Java at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) -- C++ at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) -- Java at me.zhongmingmao.basic.reflect.V0.main(V0.java:13)
// 第15次反射调用时,触发了动态实现的生成,JVM额外加载其他类 java.lang.Exception: #14 at me.zhongmingmao.basic.reflect.V1.target(V1.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at me.zhongmingmao.basic.reflect.V1.main(V1.java:14) ... // 加载自动生成的字节码 [Loaded sun.reflect.GeneratedMethodAccessor1 from __JVM_DefineClass__] java.lang.Exception: #15 at me.zhongmingmao.basic.reflect.V1.target(V1.java:7) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at me.zhongmingmao.basic.reflect.V1.main(V1.java:14) // 切换至刚刚生成的动态实现 java.lang.Exception: #16 at me.zhongmingmao.basic.reflect.V1.target(V1.java:7) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at me.zhongmingmao.basic.reflect.V1.main(V1.java:14)
privatestaticvoidreflectCall()throws Exception { Class<?> klass = Class.forName("me.zhongmingmao.basic.reflect.V3"); Methodmethod= klass.getMethod("target", int.class);
longcurrent= System.currentTimeMillis(); for (inti=1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { longtemp= System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
longcurrent= System.currentTimeMillis(); for (inti=1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { longtemp= System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
longcurrent= System.currentTimeMillis(); for (inti=1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { longtemp= System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
longcurrent= System.currentTimeMillis(); for (inti=1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { longtemp= System.currentTimeMillis(); System.out.println(temp - current); current = temp; }