public final class Method extends Executable { ... public Object invoke(Object obj, Object... args) throws ... { ... // 权限检查 MethodAccessor ma = methodAccessor; if (ma == null) { ma = acquireMethodAccessor(); } return ma.invoke(obj, args); } }
# 不同版本的输出略有不同,这里我使用了Java 10。 $ java Test java.lang.Exception: #0 at Test.target(Test.java:5) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl .invoke0(Native Method) a t java.base/jdk.internal.reflect.NativeMethodAccessorImpl. .invoke(NativeMethodAccessorImpl.java:62) t java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.i .invoke(DelegatingMethodAccessorImpl.java:43) java.base/java.lang.reflect.Method.invoke(Method.java:564) t Test.main(Test.java:131
public class Test { public static void target(int i) { new Exception("#" + i).printStackTrace(); }
public static void main(String[] args) throws Exception { Class<?> klass = Class.forName("Test"); Method method = klass.getMethod("target", int.class); for (int i = 0; i < 20; i++) { method.invoke(null, i); } } }
# 使用-verbose:class打印加载的类 $ java -verbose:class Test ... java.lang.Exception: #14 at Test.target(Test.java:5) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl .invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl .invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at Test.main(Test.java:12) [0.158s][info][class,load] ... ... [0.160s][info][class,load] jdk.internal.reflect.GeneratedMethodAccessor1 source: __JVM_DefineClass__ java.lang.Exception: #15 at Test.target(Test.java:5) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl .invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl .invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at Test.main(Test.java:12) java.lang.Exception: #16 at Test.target(Test.java:5) at jdk.internal.reflect.GeneratedMethodAccessor1 .invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl .invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:564) at Test.main(Test.java:12) ...
public class Test { public static void target(int i) { // 空方法 }
public static void main(String[] args) throws Exception { Class<?> klass = Class.forName("Test"); Method method = klass.getMethod("target", int.class);
long current = System.currentTimeMillis(); for (int i = 1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { long temp = System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
public class Test { public static void target(int i) { // 空方法 }
public static void main(String[] args) throws Exception { Class<?> klass = Class.forName("Test"); Method method = klass.getMethod("target", int.class);
Object[] arg = new Object[1]; // 在循环外构造参数数组 arg[0] = 128;
long current = System.currentTimeMillis(); for (int i = 1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { long temp = System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
// 在运行指令中添加如下两个虚拟机参数: // -Djava.lang.Integer.IntegerCache.high=128 // -Dsun.reflect.noInflation=true public class Test { public static void target(int i) { // 空方法 }
public static void main(String[] args) throws Exception { Class<?> klass = Class.forName("Test"); Method method = klass.getMethod("target", int.class); method.setAccessible(true); // 关闭权限检查
long current = System.currentTimeMillis(); for (int i = 1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { long temp = System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
long current = System.currentTimeMillis(); for (int i = 1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { long temp = System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
method.invoke(null, 128); } }
public static void polluteProfile() throws Exception { Method method1 = Test.class.getMethod("target1", int.class); Method method2 = Test.class.getMethod("target2", int.class); for (int i = 0; i < 2000; i++) { method1.invoke(null, 0); method2.invoke(null, 0); } } public static void target1(int i) { } public static void target2(int i) { } }
long current = System.currentTimeMillis(); for (int i = 1; i <= 2_000_000_000; i++) { if (i % 100_000_000 == 0) { long temp = System.currentTimeMillis(); System.out.println(temp - current); current = temp; }
method.invoke(null, 128); } }
public static void polluteProfile() throws Exception { Method method1 = Test.class.getMethod("target", int.class); Method method2 = Test.class.getMethod("target", int.class); for (int i = 0; i < 2000; i++) { method1.invoke(null, 0); method2.invoke(null, 0); } } public static void target1(int i) { } public static void target2(int i) { } }