blob: bf25a0d0280aa3b6028fc2ea00290ebaaa5f838f [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");
Andreas Gampefdeef522017-01-09 14:40:25 -080035
36 // Find a synthetic method in the dummy inner class. Do not print the name. Javac and Jack
37 // disagree on the naming of synthetic accessors.
38 testMethod(findSyntheticMethod(), NestedSynthetic.class, false);
Andreas Gampe368a2082016-10-28 17:33:13 -070039 }
40
41 private static Class<?> proxyClass = null;
42
43 private static Class<?> getProxyClass() throws Exception {
44 if (proxyClass != null) {
45 return proxyClass;
46 }
47
48 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
49 return proxyClass;
Andreas Gampe3c252f02016-10-27 18:25:17 -070050 }
51
52 private static void testMethod(String className, String methodName, Class<?>... types)
53 throws Exception {
54 Class<?> base = Class.forName(className);
Andreas Gampe368a2082016-10-28 17:33:13 -070055 testMethod(base, methodName, types);
56 }
57
58 private static void testMethod(Class<?> base, String methodName, Class<?>... types)
59 throws Exception {
Andreas Gampe3c252f02016-10-27 18:25:17 -070060 Method m = base.getDeclaredMethod(methodName, types);
Andreas Gampefdeef522017-01-09 14:40:25 -080061 testMethod(m, base, true);
62 }
63
64 private static void testMethod(Method m, Class<?> base, boolean printName) {
Andreas Gampe3c252f02016-10-27 18:25:17 -070065 String[] result = getMethodName(m);
Andreas Gampefdeef522017-01-09 14:40:25 -080066 if (!result[0].equals(m.getName())) {
67 throw new RuntimeException("Name not equal: " + m.getName() + " vs " + result[0]);
68 }
69 if (printName) {
70 System.out.println(Arrays.toString(result));
71 }
Andreas Gampe368a2082016-10-28 17:33:13 -070072
73 Class<?> declClass = getMethodDeclaringClass(m);
74 if (base != declClass) {
75 throw new RuntimeException("Declaring class not equal: " + base + " vs " + declClass);
76 }
77 System.out.println(declClass);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070078
79 int modifiers = getMethodModifiers(m);
80 if (modifiers != m.getModifiers()) {
81 throw new RuntimeException("Modifiers not equal: " + m.getModifiers() + " vs " + modifiers);
82 }
83 System.out.println(modifiers);
Andreas Gampef71832e2017-01-09 11:38:04 -080084
85 System.out.print("Max locals: ");
86 try {
87 System.out.println(getMaxLocals(m));
88 } catch (RuntimeException e) {
89 System.out.println(e.getMessage());
90 }
91
92 System.out.print("Argument size: ");
93 try {
94 System.out.println(getArgumentsSize(m));
95 } catch (RuntimeException e) {
96 System.out.println(e.getMessage());
97 }
98
99 System.out.print("Location start: ");
100 try {
101 System.out.println(getMethodLocationStart(m));
102 } catch (RuntimeException e) {
103 System.out.println(e.getMessage());
104 }
105
106 System.out.print("Location end: ");
107 try {
108 System.out.println(getMethodLocationEnd(m));
109 } catch (RuntimeException e) {
110 System.out.println(e.getMessage());
111 }
Andreas Gampefdeef522017-01-09 14:40:25 -0800112
113 System.out.println("Is native: " + isMethodNative(m));
114 System.out.println("Is obsolete: " + isMethodObsolete(m));
115 System.out.println("Is synthetic: " + isMethodSynthetic(m));
116 }
117
118 private static class NestedSynthetic {
119 // Accessing this private field will create a synthetic accessor method;
120 private static String dummy;
121 }
122
123 private static void dummyAccess() {
124 System.out.println(NestedSynthetic.dummy);
125 }
126
127 private static Method findSyntheticMethod() throws Exception {
128 Method methods[] = NestedSynthetic.class.getDeclaredMethods();
129 for (Method m : methods) {
130 if (m.isSynthetic()) {
131 return m;
132 }
133 }
134 throw new RuntimeException("Could not find synthetic method");
Andreas Gampe3c252f02016-10-27 18:25:17 -0700135 }
136
137 private static native String[] getMethodName(Method m);
Andreas Gampe368a2082016-10-28 17:33:13 -0700138 private static native Class<?> getMethodDeclaringClass(Method m);
Andreas Gampe36bcd4f2016-10-28 18:07:18 -0700139 private static native int getMethodModifiers(Method m);
Andreas Gampef71832e2017-01-09 11:38:04 -0800140 private static native int getMaxLocals(Method m);
141 private static native int getArgumentsSize(Method m);
142 private static native long getMethodLocationStart(Method m);
143 private static native long getMethodLocationEnd(Method m);
Andreas Gampefdeef522017-01-09 14:40:25 -0800144 private static native boolean isMethodNative(Method m);
145 private static native boolean isMethodObsolete(Method m);
146 private static native boolean isMethodSynthetic(Method m);
Andreas Gampe3c252f02016-10-27 18:25:17 -0700147}