Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 17 | import java.lang.reflect.InvocationHandler; |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 18 | import java.lang.reflect.Method; |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 19 | import java.lang.reflect.Proxy; |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 20 | |
Andreas Gampe | 1c83cbc | 2014-07-22 18:52:29 -0700 | [diff] [blame] | 21 | public class Main { |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 22 | public static void main(String[] args) { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 23 | System.loadLibrary(args[0]); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 24 | testFindClassOnAttachedNativeThread(); |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 25 | testFindFieldOnAttachedNativeThread(); |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 26 | testReflectFieldGetFromAttachedNativeThreadNative(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 27 | testCallStaticVoidMethodOnSubClass(); |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 28 | testGetMirandaMethod(); |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 29 | testZeroLengthByteBuffers(); |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 30 | testByteMethod(); |
| 31 | testShortMethod(); |
| 32 | testBooleanMethod(); |
| 33 | testCharMethod(); |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 34 | testIsAssignableFromOnPrimitiveTypes(); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 35 | testShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 36 | testShallowGetStackClass2(); |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 37 | testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 38 | testNewStringObject(); |
Mathieu Chartier | 22c1caa | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 39 | testRemoveLocalObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 40 | testProxyGetMethodID(); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | private static native void testFindClassOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 44 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 45 | private static boolean testFindFieldOnAttachedNativeThreadField; |
| 46 | |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 47 | private static native void testReflectFieldGetFromAttachedNativeThreadNative(); |
| 48 | |
| 49 | public static boolean testReflectFieldGetFromAttachedNativeThreadField; |
| 50 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 51 | private static void testFindFieldOnAttachedNativeThread() { |
| 52 | testFindFieldOnAttachedNativeThreadNative(); |
| 53 | if (!testFindFieldOnAttachedNativeThreadField) { |
| 54 | throw new AssertionError(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private static native void testFindFieldOnAttachedNativeThreadNative(); |
| 59 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 60 | private static void testCallStaticVoidMethodOnSubClass() { |
| 61 | testCallStaticVoidMethodOnSubClassNative(); |
| 62 | if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) { |
| 63 | throw new AssertionError(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | private static native void testCallStaticVoidMethodOnSubClassNative(); |
| 68 | |
| 69 | private static class testCallStaticVoidMethodOnSubClass_SuperClass { |
| 70 | private static boolean executed = false; |
| 71 | private static void execute() { |
| 72 | executed = true; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private static class testCallStaticVoidMethodOnSubClass_SubClass |
| 77 | extends testCallStaticVoidMethodOnSubClass_SuperClass { |
| 78 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 79 | |
| 80 | private static native Method testGetMirandaMethodNative(); |
| 81 | |
| 82 | private static void testGetMirandaMethod() { |
| 83 | Method m = testGetMirandaMethodNative(); |
| 84 | if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) { |
| 85 | throw new AssertionError(); |
| 86 | } |
| 87 | } |
| 88 | |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 89 | private static native void testZeroLengthByteBuffers(); |
| 90 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 91 | private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface { |
| 92 | public boolean inAbstract() { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private static interface testGetMirandaMethod_MirandaInterface { |
| 98 | public boolean inInterface(); |
| 99 | } |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 100 | |
| 101 | // Test sign-extension for values < 32b |
| 102 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 103 | static native byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 104 | byte b8, byte b9, byte b10); |
| 105 | |
| 106 | private static void testByteMethod() { |
| 107 | byte returns[] = { 0, 1, 2, 127, -1, -2, -128 }; |
| 108 | for (int i = 0; i < returns.length; i++) { |
| 109 | byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6, |
| 110 | (byte)(-7), (byte)8, (byte)(-9), (byte)10); |
| 111 | if (returns[i] != result) { |
| 112 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 113 | throw new AssertionError(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Mathieu Chartier | 22c1caa | 2015-06-02 13:40:12 -0700 | [diff] [blame] | 118 | private static native void removeLocalObject(Object o); |
| 119 | |
| 120 | private static void testRemoveLocalObject() { |
| 121 | removeLocalObject(new Object()); |
| 122 | } |
| 123 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 124 | private static native short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 125 | short s8, short s9, short s10); |
| 126 | |
| 127 | private static void testShortMethod() { |
| 128 | short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 }; |
| 129 | for (int i = 0; i < returns.length; i++) { |
| 130 | short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6, |
| 131 | (short)(-7), (short)8, (short)(-9), (short)10); |
| 132 | if (returns[i] != result) { |
| 133 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 134 | throw new AssertionError(); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Test zero-extension for values < 32b |
| 140 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 141 | private static native boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 142 | boolean b8, boolean b9, boolean b10); |
| 143 | |
| 144 | private static void testBooleanMethod() { |
| 145 | if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) { |
| 146 | throw new AssertionError(); |
| 147 | } |
| 148 | |
| 149 | if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) { |
| 150 | throw new AssertionError(); |
| 151 | } |
| 152 | } |
| 153 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 154 | private static native char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 155 | char c8, char c9, char c10); |
| 156 | |
| 157 | private static void testCharMethod() { |
| 158 | char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000, |
| 159 | (char)34000 }; |
| 160 | for (int i = 0; i < returns.length; i++) { |
| 161 | char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345, |
| 162 | (char)3456); |
| 163 | if (returns[i] != result) { |
| 164 | System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result); |
| 165 | throw new AssertionError(); |
| 166 | } |
| 167 | } |
| 168 | } |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 169 | |
| 170 | // http://b/16531674 |
| 171 | private static void testIsAssignableFromOnPrimitiveTypes() { |
| 172 | if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) { |
| 173 | System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true"); |
| 174 | throw new AssertionError(); |
| 175 | } |
| 176 | |
| 177 | if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) { |
| 178 | System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true"); |
| 179 | throw new AssertionError(); |
| 180 | } |
| 181 | } |
| 182 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 183 | private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 184 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 185 | private static void testShallowGetCallingClassLoader() { |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 186 | nativeTestShallowGetCallingClassLoader(); |
| 187 | } |
| 188 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 189 | private native static void nativeTestShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 190 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 191 | private static void testShallowGetStackClass2() { |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame] | 192 | nativeTestShallowGetStackClass2(); |
| 193 | } |
| 194 | |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 195 | private static native void nativeTestShallowGetStackClass2(); |
| 196 | |
| 197 | private static native void testCallNonvirtual(); |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 198 | |
| 199 | private static native void testNewStringObject(); |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 200 | |
| 201 | private interface SimpleInterface { |
| 202 | void a(); |
| 203 | } |
| 204 | |
| 205 | private static class DummyInvocationHandler implements InvocationHandler { |
| 206 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 207 | return null; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | private static void testProxyGetMethodID() { |
| 212 | InvocationHandler handler = new DummyInvocationHandler(); |
| 213 | SimpleInterface proxy = |
| 214 | (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(), |
| 215 | new Class[] {SimpleInterface.class}, handler); |
| 216 | if (testGetMethodID(SimpleInterface.class) == 0) { |
| 217 | throw new AssertionError(); |
| 218 | } |
| 219 | if (testGetMethodID(proxy.getClass()) == 0) { |
| 220 | throw new AssertionError(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private static native long testGetMethodID(Class<?> c); |
Brian Carlstrom | 58e5e5d | 2014-09-07 23:52:02 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | class JniCallNonvirtualTest { |
| 228 | public boolean nonstaticMethodSuperCalled = false; |
| 229 | public boolean nonstaticMethodSubCalled = false; |
| 230 | |
| 231 | private static native void testCallNonvirtual(); |
| 232 | |
| 233 | public JniCallNonvirtualTest() { |
| 234 | System.out.println("Super.<init>"); |
| 235 | } |
| 236 | |
| 237 | public static void staticMethod() { |
| 238 | System.out.println("Super.staticMethod"); |
| 239 | } |
| 240 | |
| 241 | public void nonstaticMethod() { |
| 242 | System.out.println("Super.nonstaticMethod"); |
| 243 | nonstaticMethodSuperCalled = true; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest { |
| 248 | |
| 249 | public JniCallNonvirtualTestSubclass() { |
| 250 | System.out.println("Subclass.<init>"); |
| 251 | } |
| 252 | |
| 253 | public static void staticMethod() { |
| 254 | System.out.println("Subclass.staticMethod"); |
| 255 | } |
| 256 | |
| 257 | public void nonstaticMethod() { |
| 258 | System.out.println("Subclass.nonstaticMethod"); |
| 259 | nonstaticMethodSubCalled = true; |
| 260 | } |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 261 | } |