Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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; |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 18 | import java.lang.reflect.Proxy; |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 19 | import java.util.Arrays; |
| 20 | |
| 21 | public class Main { |
| 22 | public static void main(String[] args) throws Exception { |
| 23 | System.loadLibrary(args[1]); |
| 24 | |
| 25 | doTest(); |
| 26 | } |
| 27 | |
| 28 | public static void doTest() throws Exception { |
| 29 | testMethod("java.lang.Object", "toString"); |
| 30 | testMethod("java.lang.String", "charAt", int.class); |
| 31 | testMethod("java.lang.Math", "sqrt", double.class); |
| 32 | testMethod("java.util.List", "add", Object.class); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 33 | |
| 34 | testMethod(getProxyClass(), "run"); |
| 35 | } |
| 36 | |
| 37 | private static Class<?> proxyClass = null; |
| 38 | |
| 39 | private static Class<?> getProxyClass() throws Exception { |
| 40 | if (proxyClass != null) { |
| 41 | return proxyClass; |
| 42 | } |
| 43 | |
| 44 | proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class }); |
| 45 | return proxyClass; |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private static void testMethod(String className, String methodName, Class<?>... types) |
| 49 | throws Exception { |
| 50 | Class<?> base = Class.forName(className); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 51 | testMethod(base, methodName, types); |
| 52 | } |
| 53 | |
| 54 | private static void testMethod(Class<?> base, String methodName, Class<?>... types) |
| 55 | throws Exception { |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 56 | Method m = base.getDeclaredMethod(methodName, types); |
| 57 | String[] result = getMethodName(m); |
| 58 | System.out.println(Arrays.toString(result)); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 59 | |
| 60 | Class<?> declClass = getMethodDeclaringClass(m); |
| 61 | if (base != declClass) { |
| 62 | throw new RuntimeException("Declaring class not equal: " + base + " vs " + declClass); |
| 63 | } |
| 64 | System.out.println(declClass); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 65 | |
| 66 | int modifiers = getMethodModifiers(m); |
| 67 | if (modifiers != m.getModifiers()) { |
| 68 | throw new RuntimeException("Modifiers not equal: " + m.getModifiers() + " vs " + modifiers); |
| 69 | } |
| 70 | System.out.println(modifiers); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private static native String[] getMethodName(Method m); |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 74 | private static native Class<?> getMethodDeclaringClass(Method m); |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 75 | private static native int getMethodModifiers(Method m); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 76 | } |