class TestInvocationHandler implements InvocationHandler {
@Override public Object invoke(Object proxy, Method method, Object[] args) { // Force garbage collection to try to make `proxy` move in memory // (in the case of a moving garbage collector).
System.gc();
privatevoid testMethod10(Object proxy, Object[] args) { // Get argument 0 (method target) from the proxy method frame ($Proxy0.method10 activation).
Object arg0 = getProxyMethodArgument(0);
System.out.println(" arg0: " + arg0.getClass().getName());
Main.assertEquals(proxy, arg0); // Get argument `i` from the proxy method frame ($Proxy0.method10 activation). for (int i = 0; i < 10; ++i) { int arg_pos = i + 1;
String arg = (String) getProxyMethodArgument(arg_pos);
System.out.println(" arg" + arg_pos + ": " + arg.getClass().getName() + " \"" + arg + "\"");
Main.assertEquals(args[i], arg);
}
}
privatevoid testMethod10Even(Object proxy, Object[] args) { // Get argument 0 (method target) from the proxy method frame ($Proxy0.method10Even // activation).
Object arg0 = getProxyMethodArgument(0);
System.out.println(" arg0: " + arg0.getClass().getName());
Main.assertEquals(proxy, arg0); // Get argument `i` from the proxy method frame ($Proxy0.method10Even activation). for (int i = 1; i < 10; i += 2) { int arg_pos = i + 1;
String arg = (String) getProxyMethodArgument(arg_pos);
System.out.println(" arg" + arg_pos + ": " + arg.getClass().getName() + " \"" + arg + "\"");
Main.assertEquals(args[i], arg);
}
}
// Get reference argument at position `arg_pos` in proxy frame. // This method should only be called from one of the // `TestInvocationHandler.testMethod*` methods via `TestInvocationHandler.invoke`. private Object getProxyMethodArgument(int arg_pos) { // Find proxy frame in stack (from a testMethod* method). // // depth method // ---------------------------------------------------------------------- // 0 TestInvocationHandler.getArgument (outermost frame) // 1 TestInvocationHandler.getProxyMethodArgument // 2 TestInvocationHandler.testMethod* // 3 TestInvocationHandler.invoke // 4 java.lang.reflect.Proxy.invoke // -> 5 TestInterface.method* (proxy method) // 6 Main.main (innermost frame) // int proxy_method_frame_depth = 5; return getArgument(arg_pos, proxy_method_frame_depth);
}
// Get reference argument at position `arg_pos` in frame at depth `frame_depth`. privatenative Object getArgument(int arg_pos, int frame_depth);
}
publicclass Main { publicstaticvoid main(String[] args) {
System.loadLibrary(args[0]);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.