blob: 3459134ea2077eb28a19ef28bd281d420cb46790 [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -07001/*
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
17import java.lang.reflect.Method;
Andreas Gampe368a2082016-10-28 17:33:13 -070018import java.lang.reflect.Proxy;
Andreas Gampe3c252f02016-10-27 18:25:17 -070019import java.util.Arrays;
20
21public 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 Gampe368a2082016-10-28 17:33:13 -070033
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 Gampe3c252f02016-10-27 18:25:17 -070046 }
47
48 private static void testMethod(String className, String methodName, Class<?>... types)
49 throws Exception {
50 Class<?> base = Class.forName(className);
Andreas Gampe368a2082016-10-28 17:33:13 -070051 testMethod(base, methodName, types);
52 }
53
54 private static void testMethod(Class<?> base, String methodName, Class<?>... types)
55 throws Exception {
Andreas Gampe3c252f02016-10-27 18:25:17 -070056 Method m = base.getDeclaredMethod(methodName, types);
57 String[] result = getMethodName(m);
58 System.out.println(Arrays.toString(result));
Andreas Gampe368a2082016-10-28 17:33:13 -070059
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 Gampe36bcd4f2016-10-28 18:07:18 -070065
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 Gampe3c252f02016-10-27 18:25:17 -070071 }
72
73 private static native String[] getMethodName(Method m);
Andreas Gampe368a2082016-10-28 17:33:13 -070074 private static native Class<?> getMethodDeclaringClass(Method m);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070075 private static native int getMethodModifiers(Method m);
Andreas Gampe3c252f02016-10-27 18:25:17 -070076}