blob: 7a7f4c0e2b2433ec57d915bda286c1b14cc93b35 [file] [log] [blame]
Andreas Gampee492ae32016-10-28 19:34:57 -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.Proxy;
18import java.util.Arrays;
19
20public class Main {
21 public static void main(String[] args) throws Exception {
22 System.loadLibrary(args[1]);
23
24 doTest();
25 }
26
27 public static void doTest() throws Exception {
28 testClass("java.lang.Object");
29 testClass("java.lang.String");
30 testClass("java.lang.Math");
31 testClass("java.util.List");
32
33 testClass(getProxyClass());
34
35 testClass(int.class);
36 testClass(double[].class);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -080037
38 testClassType(int.class);
39 testClassType(getProxyClass());
40 testClassType(Runnable.class);
41 testClassType(String.class);
42
43 testClassType(int[].class);
44 testClassType(Runnable[].class);
45 testClassType(String[].class);
Andreas Gampeac587272017-01-05 15:21:34 -080046
47 testClassFields(Integer.class);
48 testClassFields(int.class);
49 testClassFields(String[].class);
Andreas Gampeff9d2092017-01-06 09:12:49 -080050
Andreas Gampe18fee4d2017-01-06 11:36:35 -080051 testClassMethods(Integer.class);
52 testClassMethods(int.class);
53 testClassMethods(String[].class);
54
Andreas Gampeff9d2092017-01-06 09:12:49 -080055 testClassStatus(int.class);
56 testClassStatus(String[].class);
57 testClassStatus(Object.class);
58 testClassStatus(TestForNonInit.class);
59 try {
60 System.out.println(TestForInitFail.dummy);
61 } catch (ExceptionInInitializerError e) {
62 }
63 testClassStatus(TestForInitFail.class);
Andreas Gampe8b07e472017-01-06 14:20:39 -080064
65 testInterfaces(int.class);
66 testInterfaces(String[].class);
67 testInterfaces(Object.class);
68 testInterfaces(InfA.class);
69 testInterfaces(InfB.class);
70 testInterfaces(InfC.class);
71 testInterfaces(ClassA.class);
72 testInterfaces(ClassB.class);
73 testInterfaces(ClassC.class);
Andreas Gampee492ae32016-10-28 19:34:57 -070074 }
75
76 private static Class<?> proxyClass = null;
77
78 private static Class<?> getProxyClass() throws Exception {
79 if (proxyClass != null) {
80 return proxyClass;
81 }
82
83 proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), new Class[] { Runnable.class });
84 return proxyClass;
85 }
86
87 private static void testClass(String className) throws Exception {
88 Class<?> base = Class.forName(className);
89 testClass(base);
90 }
91
92 private static void testClass(Class<?> base) throws Exception {
93 String[] result = getClassSignature(base);
94 System.out.println(Arrays.toString(result));
Andreas Gampe64013e52017-01-06 13:07:19 -080095 int mod = getClassModifiers(base);
96 if (mod != base.getModifiers()) {
97 throw new RuntimeException("Unexpected modifiers: " + base.getModifiers() + " vs " + mod);
98 }
99 System.out.println(Integer.toHexString(mod));
Andreas Gampee492ae32016-10-28 19:34:57 -0700100 }
101
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800102 private static void testClassType(Class<?> c) throws Exception {
103 boolean isInterface = isInterface(c);
104 boolean isArray = isArrayClass(c);
105 System.out.println(c.getName() + " interface=" + isInterface + " array=" + isArray);
106 }
107
Andreas Gampeac587272017-01-05 15:21:34 -0800108 private static void testClassFields(Class<?> c) throws Exception {
109 System.out.println(Arrays.toString(getClassFields(c)));
110 }
111
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800112 private static void testClassMethods(Class<?> c) throws Exception {
113 System.out.println(Arrays.toString(getClassMethods(c)));
114 }
115
Andreas Gampeff9d2092017-01-06 09:12:49 -0800116 private static void testClassStatus(Class<?> c) {
117 System.out.println(c + " " + Integer.toBinaryString(getClassStatus(c)));
118 }
119
Andreas Gampe8b07e472017-01-06 14:20:39 -0800120 private static void testInterfaces(Class<?> c) {
121 System.out.println(c + " " + Arrays.toString(getImplementedInterfaces(c)));
122 }
123
Andreas Gampee492ae32016-10-28 19:34:57 -0700124 private static native String[] getClassSignature(Class<?> c);
Andreas Gampe4fd66ec2017-01-05 14:42:13 -0800125
126 private static native boolean isInterface(Class<?> c);
127 private static native boolean isArrayClass(Class<?> c);
Andreas Gampeac587272017-01-05 15:21:34 -0800128
Andreas Gampe64013e52017-01-06 13:07:19 -0800129 private static native int getClassModifiers(Class<?> c);
130
Andreas Gampeac587272017-01-05 15:21:34 -0800131 private static native Object[] getClassFields(Class<?> c);
Andreas Gampe18fee4d2017-01-06 11:36:35 -0800132 private static native Object[] getClassMethods(Class<?> c);
Andreas Gampe8b07e472017-01-06 14:20:39 -0800133 private static native Class[] getImplementedInterfaces(Class<?> c);
Andreas Gampeff9d2092017-01-06 09:12:49 -0800134
135 private static native int getClassStatus(Class<?> c);
136
137 private static class TestForNonInit {
138 public static double dummy = Math.random(); // So it can't be compile-time initialized.
139 }
140
141 private static class TestForInitFail {
142 public static int dummy = ((int)Math.random())/0; // So it throws when initializing.
143 }
Andreas Gampe8b07e472017-01-06 14:20:39 -0800144
145 public static interface InfA {
146 }
147 public static interface InfB extends InfA {
148 }
149 public static interface InfC extends InfB {
150 }
151
152 public abstract static class ClassA implements InfA {
153 }
154 public abstract static class ClassB extends ClassA implements InfB {
155 }
156 public abstract static class ClassC implements InfA, InfC {
157 }
Andreas Gampee492ae32016-10-28 19:34:57 -0700158}