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