Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 17 | import java.lang.reflect.*; |
| 18 | import java.util.*; |
| 19 | |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 20 | class SystemMethods { |
| 21 | public static int test0() { |
| 22 | System.logI("hello world"); |
| 23 | return 123; |
| 24 | } |
| 25 | |
| 26 | public static int test1() { |
| 27 | String[] digits = new String[] { |
| 28 | "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f", |
| 29 | }; |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 30 | long t = System.currentTimeMillis(); |
| 31 | for (int i = 7; i >= 0; --i) { |
| 32 | int b = ((int) (t >> (i * 8))) & 0xff; |
| 33 | System.logI(digits[(b >> 4) & 0xf]); |
| 34 | System.logI(digits[b & 0xf]); |
| 35 | } |
| 36 | return 123; |
| 37 | } |
| 38 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 39 | private static String[] STRING_DIGITS = new String[] { |
| 40 | "0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f", |
| 41 | }; |
| 42 | |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 43 | public static int test2() { |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 44 | char[] cs = new char[20]; |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 45 | long t = System.currentTimeMillis(); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 46 | StringBuilder sb = new StringBuilder(20); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 47 | for (int i = 7; i >= 0; --i) { |
| 48 | int b = ((int) (t >> (i * 8))) & 0xff; |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 49 | sb.append(STRING_DIGITS[(b >> 4) & 0xf]); |
| 50 | sb.append(STRING_DIGITS[b & 0xf]); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 51 | } |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 52 | String result = sb.toString(); |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 53 | System.logI(result); |
| 54 | return 123; |
| 55 | } |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 56 | |
| 57 | private static char[] DIGITS = new char[] { |
| 58 | '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| 59 | 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
| 60 | 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
| 61 | 'u', 'v', 'w', 'x', 'y', 'z' |
| 62 | }; |
| 63 | |
| 64 | public static int test3() { |
| 65 | long t = System.currentTimeMillis(); |
| 66 | |
| 67 | long v = t; |
| 68 | // int i = (int) v; |
| 69 | // if (v >= 0 && i == v) { |
| 70 | // return intToHexString(i, false, 0); |
| 71 | // } |
| 72 | |
| 73 | int bufLen = 16; // Max number of hex digits in a long |
| 74 | char[] buf = new char[bufLen]; |
| 75 | int cursor = bufLen; |
| 76 | |
| 77 | do { |
| 78 | buf[--cursor] = DIGITS[((int) v) & 0xF]; |
| 79 | } while ((v >>>= 4) != 0); |
| 80 | |
| 81 | String s = new String(buf, cursor, bufLen - cursor); |
| 82 | System.logI(s); |
| 83 | |
| 84 | System.logI(IntegralToString.longToHexString(t)); |
| 85 | System.logI(Long.toHexString(t)); |
| 86 | System.logI(Long.toString(t)); |
| 87 | return 123; |
| 88 | } |
| 89 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 90 | private static boolean z = true; |
| 91 | private static byte b = 8; |
| 92 | private static char c = 'x'; |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 93 | private static double d = Math.PI; |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 94 | private static float f = 3.14f; |
| 95 | private static int i = 32; |
| 96 | private static long j = 0x0123456789abcdefL; |
| 97 | private static short s = 16; |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 98 | |
Elliott Hughes | f5ecf06 | 2011-09-06 17:37:59 -0700 | [diff] [blame] | 99 | public static int test4() { |
| 100 | String s = "int=" + i + " long=" + j; |
| 101 | System.logI(s); |
| 102 | return 123; |
| 103 | } |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 104 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 105 | public static int test5() throws Exception { |
| 106 | System.logI("new Thread..."); |
| 107 | Thread t = new Thread(new Runnable() { |
| 108 | public void run() { |
| 109 | System.logI("hello from a new thread!"); |
| 110 | System.logI(Thread.currentThread().toString()); |
| 111 | try { |
| 112 | System.logI("sleeping for 2s"); |
| 113 | Thread.sleep(2*1000); |
| 114 | } catch (Exception ex) { ex.printStackTrace(); } |
| 115 | System.logI("finished sleeping"); |
| 116 | throw new RuntimeException("uncaught exception"); |
| 117 | } |
| 118 | }); |
| 119 | System.logI("calling Thread.toString..."); |
| 120 | System.logI(t.toString()); |
| 121 | System.logI("calling Thread.start..."); |
| 122 | t.start(); |
| 123 | //t.join(); |
| 124 | System.logI("...done!"); |
Elliott Hughes | 1240dad | 2011-09-09 16:24:50 -0700 | [diff] [blame] | 125 | return 123; |
| 126 | } |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 127 | |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 128 | public static void test6() throws Exception { |
| 129 | java.lang.reflect.Field f; |
| 130 | |
| 131 | f = SystemMethods.class.getDeclaredField("z"); |
| 132 | System.out.println(f.getBoolean(null)); |
| 133 | f = SystemMethods.class.getDeclaredField("b"); |
| 134 | System.out.println(f.getByte(null)); |
| 135 | f = SystemMethods.class.getDeclaredField("c"); |
| 136 | System.out.println(f.getChar(null)); |
| 137 | f = SystemMethods.class.getDeclaredField("d"); |
| 138 | System.out.println(f.getDouble(null)); |
| 139 | f = SystemMethods.class.getDeclaredField("f"); |
| 140 | System.out.println(f.getFloat(null)); |
| 141 | f = SystemMethods.class.getDeclaredField("i"); |
| 142 | System.out.println(f.getInt(null)); |
| 143 | f = SystemMethods.class.getDeclaredField("j"); |
| 144 | System.out.println(f.getLong(null)); |
| 145 | f = SystemMethods.class.getDeclaredField("s"); |
| 146 | System.out.println(f.getShort(null)); |
| 147 | |
| 148 | f = SystemMethods.class.getDeclaredField("z"); |
| 149 | f.setBoolean(null, false); |
| 150 | f = SystemMethods.class.getDeclaredField("b"); |
| 151 | f.setByte(null, (byte) 7); |
| 152 | f = SystemMethods.class.getDeclaredField("c"); |
| 153 | f.setChar(null, 'y'); |
| 154 | f = SystemMethods.class.getDeclaredField("d"); |
| 155 | f.setDouble(null, 2.7); |
| 156 | f = SystemMethods.class.getDeclaredField("f"); |
| 157 | f.setFloat(null, 2.7f); |
| 158 | f = SystemMethods.class.getDeclaredField("i"); |
| 159 | f.setInt(null, 31); |
| 160 | f = SystemMethods.class.getDeclaredField("j"); |
| 161 | f.setLong(null, 63); |
| 162 | f = SystemMethods.class.getDeclaredField("s"); |
| 163 | f.setShort(null, (short) 15); |
| 164 | |
| 165 | f = SystemMethods.class.getDeclaredField("z"); |
| 166 | System.out.println(f.getBoolean(null)); |
| 167 | f = SystemMethods.class.getDeclaredField("b"); |
| 168 | System.out.println(f.getByte(null)); |
| 169 | f = SystemMethods.class.getDeclaredField("c"); |
| 170 | System.out.println(f.getChar(null)); |
| 171 | f = SystemMethods.class.getDeclaredField("d"); |
| 172 | System.out.println(f.getDouble(null)); |
| 173 | f = SystemMethods.class.getDeclaredField("f"); |
| 174 | System.out.println(f.getFloat(null)); |
| 175 | f = SystemMethods.class.getDeclaredField("i"); |
| 176 | System.out.println(f.getInt(null)); |
| 177 | f = SystemMethods.class.getDeclaredField("j"); |
| 178 | System.out.println(f.getLong(null)); |
| 179 | f = SystemMethods.class.getDeclaredField("s"); |
| 180 | System.out.println(f.getShort(null)); |
| 181 | |
| 182 | f = SystemMethods.class.getDeclaredField("z"); |
| 183 | f.set(null, Boolean.valueOf(true)); |
| 184 | f = SystemMethods.class.getDeclaredField("b"); |
| 185 | f.set(null, Byte.valueOf((byte) 6)); |
| 186 | f = SystemMethods.class.getDeclaredField("c"); |
| 187 | f.set(null, Character.valueOf('z')); |
| 188 | f = SystemMethods.class.getDeclaredField("d"); |
| 189 | f.set(null, Double.valueOf(1.3)); |
| 190 | f = SystemMethods.class.getDeclaredField("f"); |
| 191 | f.set(null, Float.valueOf(1.3f)); |
| 192 | f = SystemMethods.class.getDeclaredField("i"); |
| 193 | f.set(null, Integer.valueOf(30)); |
| 194 | f = SystemMethods.class.getDeclaredField("j"); |
| 195 | f.set(null, Long.valueOf(62)); |
| 196 | f.set(null, Integer.valueOf(62)); |
| 197 | f = SystemMethods.class.getDeclaredField("s"); |
| 198 | f.set(null, Short.valueOf((short) 14)); |
| 199 | |
| 200 | f = SystemMethods.class.getDeclaredField("z"); |
| 201 | System.out.println(f.getBoolean(null)); |
| 202 | f = SystemMethods.class.getDeclaredField("b"); |
| 203 | System.out.println(f.getByte(null)); |
| 204 | f = SystemMethods.class.getDeclaredField("c"); |
| 205 | System.out.println(f.getChar(null)); |
| 206 | f = SystemMethods.class.getDeclaredField("d"); |
| 207 | System.out.println(f.getDouble(null)); |
| 208 | f = SystemMethods.class.getDeclaredField("f"); |
| 209 | System.out.println(f.getFloat(null)); |
| 210 | f = SystemMethods.class.getDeclaredField("i"); |
| 211 | System.out.println(f.getInt(null)); |
| 212 | f = SystemMethods.class.getDeclaredField("j"); |
| 213 | System.out.println(f.getLong(null)); |
| 214 | f = SystemMethods.class.getDeclaredField("s"); |
| 215 | System.out.println(f.getShort(null)); |
| 216 | |
| 217 | try { |
| 218 | f = SystemMethods.class.getDeclaredField("s"); |
| 219 | f.set(null, Integer.valueOf(14)); |
| 220 | } catch (Exception ex) { |
| 221 | ex.printStackTrace(); |
| 222 | } |
| 223 | |
| 224 | f = SystemMethods.class.getDeclaredField("z"); |
| 225 | show(f.get(null)); |
| 226 | f = SystemMethods.class.getDeclaredField("b"); |
| 227 | show(f.get(null)); |
| 228 | f = SystemMethods.class.getDeclaredField("c"); |
| 229 | show(f.get(null)); |
| 230 | f = SystemMethods.class.getDeclaredField("d"); |
| 231 | show(f.get(null)); |
| 232 | f = SystemMethods.class.getDeclaredField("f"); |
| 233 | show(f.get(null)); |
| 234 | f = SystemMethods.class.getDeclaredField("i"); |
| 235 | show(f.get(null)); |
| 236 | f = SystemMethods.class.getDeclaredField("j"); |
| 237 | show(f.get(null)); |
| 238 | f = SystemMethods.class.getDeclaredField("s"); |
| 239 | show(f.get(null)); |
| 240 | |
| 241 | /* |
| 242 | private static boolean z = true; |
| 243 | private static byte b = 8; |
| 244 | private static char c = 'x'; |
| 245 | private static double d = Math.PI; |
| 246 | private static float f = 3.14f; |
| 247 | private static int i = 32; |
| 248 | private static long j = 0x0123456789abcdefL; |
| 249 | private static short s = 16; |
| 250 | */ |
| 251 | } |
| 252 | |
| 253 | private static void show(Object o) { |
| 254 | System.out.println(o + " (" + (o != null ? o.getClass() : "null") + ")"); |
| 255 | } |
| 256 | |
| 257 | public static void test7() throws Exception { |
| 258 | System.out.println(Arrays.toString(String.class.getDeclaredConstructors())); |
| 259 | System.out.println(Arrays.toString(String.class.getDeclaredFields())); |
| 260 | System.out.println(Arrays.toString(String.class.getDeclaredMethods())); |
| 261 | |
| 262 | System.out.println(Arrays.toString(SystemMethods.class.getInterfaces())); |
| 263 | System.out.println(Arrays.toString(String.class.getInterfaces())); |
| 264 | |
| 265 | // System.out.println(SystemMethods.class.getModifiers()); |
| 266 | // System.out.println(String.class.getModifiers()); |
| 267 | |
| 268 | System.out.println(String.class.isAssignableFrom(Object.class)); |
| 269 | System.out.println(Object.class.isAssignableFrom(String.class)); |
| 270 | |
| 271 | System.out.println(String.class.isInstance("hello")); |
| 272 | System.out.println(String.class.isInstance(123)); |
| 273 | |
| 274 | java.lang.reflect.Method m; |
| 275 | |
| 276 | m = SystemMethods.class.getDeclaredMethod("IV", int.class); |
| 277 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 278 | show(m.invoke(null, 4444)); |
| 279 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 280 | |
| 281 | m = SystemMethods.class.getDeclaredMethod("IIV", int.class, int.class); |
| 282 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 283 | show(m.invoke(null, 1111, 2222)); |
| 284 | |
| 285 | m = SystemMethods.class.getDeclaredMethod("III", int.class, int.class); |
| 286 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 287 | show(m.invoke(null, 1111, 2222)); |
| 288 | |
| 289 | m = SystemMethods.class.getDeclaredMethod("sumArray", int[].class); |
| 290 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 291 | show(m.invoke(null, new int[] { 1, 2, 3, 4 })); |
| 292 | |
| 293 | m = SystemMethods.class.getDeclaredMethod("concat", String[].class); |
| 294 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 295 | show(m.invoke(null, (Object) new String[] { "h", "e", "l", "l", "o" })); |
| 296 | |
| 297 | m = SystemMethods.class.getDeclaredMethod("ZBCDFIJSV", boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class); |
| 298 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 299 | show(m.invoke(null, true, (byte) 0, '1', 2, 3, 4, 5, (short) 6)); |
| 300 | |
| 301 | m = SystemMethods.class.getDeclaredMethod("ZBCDLFIJSV", boolean.class, byte.class, char.class, double.class, String.class, float.class, int.class, long.class, short.class); |
| 302 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 303 | show(m.invoke(null, true, (byte) 0, '1', 2, "hello world", 3, 4, 5, (short) 6)); |
| 304 | |
| 305 | try { |
| 306 | m = SystemMethods.class.getDeclaredMethod("thrower"); |
| 307 | System.out.println(Arrays.toString(m.getParameterTypes())); |
| 308 | show(m.invoke(null)); |
| 309 | System.out.println("************* should have thrown!"); |
| 310 | } catch (Exception ex) { |
| 311 | ex.printStackTrace(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private static void thrower() { |
| 316 | throw new ArithmeticException("surprise!"); |
| 317 | } |
| 318 | |
| 319 | public static int sumArray(int[] xs) { |
| 320 | int result = 0; |
| 321 | for (int x : xs) { |
| 322 | result += x; |
| 323 | } |
| 324 | return result; |
| 325 | } |
| 326 | |
| 327 | public static String concat(String[] strings) { |
| 328 | String result = ""; |
| 329 | for (String s : strings) { |
| 330 | result += s; |
| 331 | } |
| 332 | return result; |
| 333 | } |
| 334 | |
| 335 | public static void IV(int i) { |
| 336 | System.out.println(i); |
| 337 | } |
| 338 | |
| 339 | public static void IIV(int i, int j) { |
| 340 | System.out.println(i + " " + j); |
| 341 | } |
| 342 | |
| 343 | public static int III(int i, int j) { |
| 344 | System.out.println(i + " " + j); |
| 345 | return i + j; |
| 346 | } |
| 347 | |
| 348 | public static void ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s) { |
| 349 | System.out.println(z + " " + b + " " + c + " " + d + " " + f + " " + i + " " + l + " " + s); |
| 350 | } |
| 351 | |
| 352 | public static void ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s) { |
| 353 | System.out.println(z + " " + b + " " + c + " " + d + " " + " " + string + " " + f + " " + i + " " + l + " " + s); |
| 354 | } |
| 355 | |
| 356 | public static void test8() throws Exception { |
| 357 | Constructor<?> ctor; |
| 358 | |
| 359 | ctor = String.class.getConstructor(new Class[0]); |
| 360 | show(ctor.newInstance((Object[]) null)); |
| 361 | |
| 362 | ctor = String.class.getConstructor(char[].class, int.class, int.class); |
| 363 | show(ctor.newInstance(new char[] { 'x', 'y', 'z', '!' }, 1, 2)); |
| 364 | } |
| 365 | |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 366 | public static void main(String[] args) throws Exception { |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame^] | 367 | test7(); |
| 368 | test8(); |
Elliott Hughes | 29f2742 | 2011-09-18 16:02:18 -0700 | [diff] [blame] | 369 | } |
Elliott Hughes | 0f4c41d | 2011-09-04 14:58:03 -0700 | [diff] [blame] | 370 | } |