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 | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | private static native void testFindClassOnAttachedNativeThread(); |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 38 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 39 | private static boolean testFindFieldOnAttachedNativeThreadField; |
| 40 | |
Vladimir Marko | 3bd7a6c | 2014-06-12 15:22:31 +0100 | [diff] [blame] | 41 | private static native void testReflectFieldGetFromAttachedNativeThreadNative(); |
| 42 | |
| 43 | public static boolean testReflectFieldGetFromAttachedNativeThreadField; |
| 44 | |
Jeff Hao | 62509b6 | 2013-12-10 17:44:56 -0800 | [diff] [blame] | 45 | private static void testFindFieldOnAttachedNativeThread() { |
| 46 | testFindFieldOnAttachedNativeThreadNative(); |
| 47 | if (!testFindFieldOnAttachedNativeThreadField) { |
| 48 | throw new AssertionError(); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | private static native void testFindFieldOnAttachedNativeThreadNative(); |
| 53 | |
Brian Carlstrom | 67fe2b4 | 2013-10-15 18:51:42 -0700 | [diff] [blame] | 54 | private static void testCallStaticVoidMethodOnSubClass() { |
| 55 | testCallStaticVoidMethodOnSubClassNative(); |
| 56 | if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) { |
| 57 | throw new AssertionError(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static native void testCallStaticVoidMethodOnSubClassNative(); |
| 62 | |
| 63 | private static class testCallStaticVoidMethodOnSubClass_SuperClass { |
| 64 | private static boolean executed = false; |
| 65 | private static void execute() { |
| 66 | executed = true; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | private static class testCallStaticVoidMethodOnSubClass_SubClass |
| 71 | extends testCallStaticVoidMethodOnSubClass_SuperClass { |
| 72 | } |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 73 | |
| 74 | private static native Method testGetMirandaMethodNative(); |
| 75 | |
| 76 | private static void testGetMirandaMethod() { |
| 77 | Method m = testGetMirandaMethodNative(); |
| 78 | if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) { |
| 79 | throw new AssertionError(); |
| 80 | } |
| 81 | } |
| 82 | |
Narayan Kamath | ef809d0 | 2013-12-19 17:52:47 +0000 | [diff] [blame] | 83 | private static native void testZeroLengthByteBuffers(); |
| 84 | |
Jeff Hao | 201803f | 2013-11-20 18:11:39 -0800 | [diff] [blame] | 85 | private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface { |
| 86 | public boolean inAbstract() { |
| 87 | return true; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static interface testGetMirandaMethod_MirandaInterface { |
| 92 | public boolean inInterface(); |
| 93 | } |
Andreas Gampe | d110432 | 2014-05-01 14:38:56 -0700 | [diff] [blame] | 94 | |
| 95 | // Test sign-extension for values < 32b |
| 96 | |
| 97 | native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, |
| 98 | byte b8, byte b9, byte b10); |
| 99 | |
| 100 | private static void testByteMethod() { |
| 101 | byte returns[] = { 0, 1, 2, 127, -1, -2, -128 }; |
| 102 | for (int i = 0; i < returns.length; i++) { |
| 103 | byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6, |
| 104 | (byte)(-7), (byte)8, (byte)(-9), (byte)10); |
| 105 | if (returns[i] != result) { |
| 106 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 107 | throw new AssertionError(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7, |
| 113 | short s8, short s9, short s10); |
| 114 | |
| 115 | private static void testShortMethod() { |
| 116 | short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 }; |
| 117 | for (int i = 0; i < returns.length; i++) { |
| 118 | short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6, |
| 119 | (short)(-7), (short)8, (short)(-9), (short)10); |
| 120 | if (returns[i] != result) { |
| 121 | System.out.println("Run " + i + " with " + returns[i] + " vs " + result); |
| 122 | throw new AssertionError(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Test zero-extension for values < 32b |
| 128 | |
| 129 | native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7, |
| 130 | boolean b8, boolean b9, boolean b10); |
| 131 | |
| 132 | private static void testBooleanMethod() { |
| 133 | if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) { |
| 134 | throw new AssertionError(); |
| 135 | } |
| 136 | |
| 137 | if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) { |
| 138 | throw new AssertionError(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7, |
| 143 | char c8, char c9, char c10); |
| 144 | |
| 145 | private static void testCharMethod() { |
| 146 | char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000, |
| 147 | (char)34000 }; |
| 148 | for (int i = 0; i < returns.length; i++) { |
| 149 | char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345, |
| 150 | (char)3456); |
| 151 | if (returns[i] != result) { |
| 152 | System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result); |
| 153 | throw new AssertionError(); |
| 154 | } |
| 155 | } |
| 156 | } |
Narayan Kamath | 1268b74 | 2014-07-11 19:15:11 +0100 | [diff] [blame] | 157 | |
| 158 | // http://b/16531674 |
| 159 | private static void testIsAssignableFromOnPrimitiveTypes() { |
| 160 | if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) { |
| 161 | System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true"); |
| 162 | throw new AssertionError(); |
| 163 | } |
| 164 | |
| 165 | if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) { |
| 166 | System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true"); |
| 167 | throw new AssertionError(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | native static boolean nativeIsAssignableFrom(Class<?> from, Class<?> to); |
Andreas Gampe | 718ac65 | 2014-08-11 18:51:53 -0700 | [diff] [blame] | 172 | |
| 173 | static void testShallowGetCallingClassLoader() { |
| 174 | nativeTestShallowGetCallingClassLoader(); |
| 175 | } |
| 176 | |
| 177 | native static void nativeTestShallowGetCallingClassLoader(); |
Andreas Gampe | 0d334ce | 2014-08-13 23:05:38 -0700 | [diff] [blame^] | 178 | |
| 179 | static void testShallowGetStackClass2() { |
| 180 | nativeTestShallowGetStackClass2(); |
| 181 | } |
| 182 | |
| 183 | native static void nativeTestShallowGetStackClass2(); |
Brian Carlstrom | ce88853 | 2013-10-10 00:32:58 -0700 | [diff] [blame] | 184 | } |