Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import java.lang.reflect.Method; |
| 18 | import java.lang.reflect.InvocationTargetException; |
| 19 | |
| 20 | class DebugProxy implements java.lang.reflect.InvocationHandler { |
| 21 | private Object obj; |
| 22 | static Class<?>[] interfaces = {Foo.class}; |
| 23 | |
| 24 | public static Object newInstance(Object obj) { |
| 25 | return java.lang.reflect.Proxy.newProxyInstance( |
| 26 | Foo.class.getClassLoader(), |
| 27 | interfaces, |
| 28 | new DebugProxy(obj)); |
| 29 | } |
| 30 | |
| 31 | private DebugProxy(Object obj) { |
| 32 | this.obj = obj; |
| 33 | } |
| 34 | |
| 35 | public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { |
| 36 | Object result; |
| 37 | if (obj == null) { |
| 38 | return null; |
| 39 | } |
| 40 | try { |
| 41 | System.out.println("before invoking method " + m.getName()); |
| 42 | result = m.invoke(obj, args); |
| 43 | } catch (InvocationTargetException e) { |
| 44 | throw e.getTargetException(); |
| 45 | } catch (Exception e) { |
| 46 | throw new RuntimeException("unexpected invocation exception: " + e.getMessage()); |
| 47 | } finally { |
| 48 | System.out.println("after invoking method " + m.getName()); |
| 49 | } |
| 50 | return result; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public class Main { |
| 55 | public static void call(Foo foo) { |
| 56 | if (foo == null) { |
| 57 | return; |
| 58 | } |
| 59 | foo.bar(null); |
| 60 | } |
| 61 | |
| 62 | public static void main(String[] args) { |
| 63 | System.loadLibrary(args[0]); |
| 64 | Foo foo = (Foo)DebugProxy.newInstance(null); |
| 65 | ensureJitCompiled(Main.class, "call"); |
| 66 | call(foo); |
| 67 | } |
| 68 | |
| 69 | private static native void ensureJitCompiled(Class<?> itf, String method_name); |
| 70 | } |