/art/test/045-reflect-array/src/Main.java

https://gitlab.com/brian0218/rk3288_r-box_android4.4.2_sdk · Java · 269 lines · 216 code · 45 blank · 8 comment · 33 complexity · 312037e9445765be32e594a6040a7700 MD5 · raw file

  1. /*
  2. * Copyright 2006 The Android Open Source Project
  3. */
  4. import java.lang.reflect.Array;
  5. /**
  6. * Test java.lang.reflect.Array.
  7. */
  8. public class Main {
  9. public static void main(String[] args) {
  10. testSingleInt();
  11. testSingleChar();
  12. testSingleShort();
  13. testSingleLong();
  14. testSingle();
  15. testMultiInt();
  16. testMulti();
  17. testAbstract();
  18. System.out.println("ReflectArrayTest passed");
  19. }
  20. static void testSingleInt() {
  21. Object intArray;
  22. intArray = Array.newInstance(Integer.TYPE, 2);
  23. int[] array = (int[]) intArray;
  24. array[0] = 5;
  25. Array.setInt(intArray, 1, 6);
  26. if (Array.getInt(intArray, 0) != 5)
  27. throw new RuntimeException();
  28. if (array[1] != 6)
  29. throw new RuntimeException();
  30. try {
  31. array[2] = 27;
  32. throw new RuntimeException("store should have failed");
  33. } catch (ArrayIndexOutOfBoundsException abe) { }
  34. try {
  35. Array.setInt(intArray, 2, 27);
  36. throw new RuntimeException("store should have failed");
  37. } catch (ArrayIndexOutOfBoundsException abe) { }
  38. if (array.length != Array.getLength(intArray) ||
  39. array.length != 2)
  40. {
  41. throw new RuntimeException("bad len");
  42. }
  43. Integer x123 = Integer.valueOf(123);
  44. Integer x456 = Integer.valueOf(456);
  45. Array.set(intArray, 0, x123);
  46. Array.set(intArray, 1, x456);
  47. if (!Array.get(intArray, 0).equals(x123) || !Array.get(intArray, 1).equals(x456)) {
  48. throw new RuntimeException("bad 123 or 456");
  49. }
  50. int[][] wrongArray;
  51. try {
  52. wrongArray = (int[][]) intArray;
  53. throw new RuntimeException("cast should have failed");
  54. } catch (ClassCastException cce) { }
  55. intArray = Array.newInstance(Integer.TYPE, 0);
  56. if (Array.getLength(intArray) != 0)
  57. throw new RuntimeException();
  58. System.out.println("ReflectArrayTest.testSingleInt passed");
  59. }
  60. static void testSingleChar() {
  61. Object charArray = Array.newInstance(Character.TYPE, 7);
  62. char[] array = (char[]) charArray;
  63. array[0] = '0';
  64. array[1] = 'W';
  65. array[2] = '2';
  66. array[3] = '3';
  67. array[4] = 'X';
  68. array[5] = '5';
  69. array[6] = '6';
  70. Array.setChar(charArray, 1, '1');
  71. Array.setChar(charArray, 4, '4');
  72. try {
  73. Array.setShort(charArray, 3, (short) 'Y');
  74. throw new RuntimeException("shouldn't allow short in char array");
  75. } catch (IllegalArgumentException iae) {}
  76. try {
  77. Array.setInt(charArray, 5, 'Z');
  78. throw new RuntimeException("shouldn't allow int in char array");
  79. } catch (IllegalArgumentException iae) {}
  80. try {
  81. for (int i = 0; i < array.length; i++) {
  82. if (Array.getInt(charArray, i) - '0' != i) {
  83. throw new RuntimeException("mismatch: " + i + " is " + array[i]);
  84. }
  85. }
  86. if (Array.getInt(charArray, 4) != '4') {
  87. throw new RuntimeException("load should have worked");
  88. }
  89. } catch (IllegalArgumentException iae) {
  90. iae.printStackTrace();
  91. }
  92. try {
  93. Array.getByte(charArray, 2);
  94. throw new RuntimeException("shouldn't allow read of char as byte");
  95. } catch (IllegalArgumentException iae) {}
  96. Array.setChar(charArray, 3, (char) 0xffff);
  97. try {
  98. if (Array.getInt(charArray, 3) != 0xffff) {
  99. throw new RuntimeException("char got sign-extended? "
  100. + Array.getInt(charArray, 3));
  101. }
  102. } catch (IllegalArgumentException iae) {
  103. iae.printStackTrace();
  104. }
  105. System.out.println("ReflectArrayTest.testSingleChar passed");
  106. }
  107. static void testSingleShort() {
  108. Object shortArray = Array.newInstance(Short.TYPE, 1);
  109. Array.setShort(shortArray, 0, (short) -1);
  110. if (Array.getInt(shortArray, 0) != -1) {
  111. throw new RuntimeException("short didn't get sign-extended");
  112. }
  113. Short box = (Short) Array.get(shortArray, 0);
  114. System.out.println("ReflectArrayTest.testSingleShort passed");
  115. }
  116. static void testSingleLong() {
  117. Object longArray = Array.newInstance(Long.TYPE, 2);
  118. Array.setInt(longArray, 0, 123);
  119. Array.setLong(longArray, 1, 0x1122334455667788L);
  120. try {
  121. Array.getInt(longArray, 0);
  122. throw new RuntimeException("shouldn't allow read of long as int");
  123. } catch (IllegalArgumentException iae) {}
  124. long[] array = (long[]) longArray;
  125. if (array[0] != 123 || array[1] != 0x1122334455667788L) {
  126. throw new RuntimeException();
  127. }
  128. float f = Array.getFloat(longArray, 0);
  129. if (f < 122.9 || f > 123.1) {
  130. throw new RuntimeException("long-as-float failed - " + f);
  131. }
  132. if (Array.getLong(longArray, 1) != 0x1122334455667788L) {
  133. throw new RuntimeException("long1 failed");
  134. }
  135. System.out.println("ReflectArrayTest.testSingleLong passed");
  136. }
  137. static void testSingle() {
  138. Object strArray;
  139. strArray = Array.newInstance(String.class, 2);
  140. String[] array = (String[]) strArray;
  141. array[0] = "entry zero";
  142. Array.set(strArray, 1, "entry one");
  143. try {
  144. Array.set(strArray, 2, "entry two");
  145. throw new RuntimeException("store should have failed");
  146. } catch (ArrayIndexOutOfBoundsException abe) { }
  147. //System.out.println("array: " + array);
  148. if (!"entry zero".equals(Array.get(strArray, 0)))
  149. throw new RuntimeException();
  150. if (!"entry one".equals(array[1]))
  151. throw new RuntimeException();
  152. if (array.length != Array.getLength(strArray) ||
  153. array.length != 2)
  154. {
  155. throw new RuntimeException("bad len");
  156. }
  157. try {
  158. Array.set(strArray, 0, new Integer(5));
  159. throw new RuntimeException("store of Integer should have failed");
  160. } catch (IllegalArgumentException iae) {}
  161. System.out.println("ReflectArrayTest.testSingle passed");
  162. }
  163. static void testMultiInt() {
  164. Object intIntIntArray;
  165. int[] dimensions = { 3, 2, 1 };
  166. intIntIntArray = Array.newInstance(Integer.TYPE, dimensions);
  167. int[][][] array3 = (int[][][]) intIntIntArray;
  168. array3[0][0][0] = 123; // trouble
  169. array3[2][1][0] = 456;
  170. try {
  171. array3[2][1][1] = 768;
  172. throw new RuntimeException("store should have failed");
  173. }
  174. catch (ArrayIndexOutOfBoundsException abe) {
  175. }
  176. System.out.println("ReflectArrayTest.testMultiInt passed");
  177. }
  178. static void testMulti() {
  179. Object strStrStrArray;
  180. int[] dimensions = { 1, 2, 3 };
  181. strStrStrArray = Array.newInstance(String.class, dimensions);
  182. String[][][] array3 = (String[][][]) strStrStrArray;
  183. array3[0][0][0] = "zero zero zero";
  184. array3[0][1][2] = "zero one two";
  185. try {
  186. array3[1][0][0] = "bad store";
  187. throw new RuntimeException("store should have failed");
  188. }
  189. catch (ArrayIndexOutOfBoundsException abe) {
  190. }
  191. try {
  192. String[][] array2 = (String[][]) strStrStrArray;
  193. throw new RuntimeException("expecting bad cast");
  194. }
  195. catch (ClassCastException cce) {
  196. }
  197. String[] strar = new String[4];
  198. strar[2] = "zero one two ++";
  199. array3[0][1] = strar;
  200. System.out.println(array3[0][1][2]);
  201. //System.out.println("array3: " + array3);
  202. int[] dimensions2 = { 1, 2 };
  203. strStrStrArray = Array.newInstance(String[].class, dimensions2);
  204. array3 = (String[][][]) strStrStrArray;
  205. array3[0][1] = new String[3];
  206. array3[0][1][2] = "zero one two";
  207. try {
  208. array3[1][0][0] = "bad store";
  209. throw new RuntimeException("store should have failed");
  210. }
  211. catch (ArrayIndexOutOfBoundsException abe) {
  212. }
  213. System.out.println("ReflectArrayTest.testMulti passed");
  214. }
  215. static void testAbstract() {
  216. Object arrayOfAbstractClasses = Array.newInstance(Number.class, 1);
  217. System.out.println(arrayOfAbstractClasses.getClass().toString() + " modifiers: " +
  218. arrayOfAbstractClasses.getClass().getModifiers());
  219. arrayOfAbstractClasses = Array.newInstance(Cloneable.class, 1);
  220. System.out.println(arrayOfAbstractClasses.getClass().toString() + " modifiers: " +
  221. arrayOfAbstractClasses.getClass().getModifiers());
  222. System.out.println("ReflectArrayTest.testAbstract passed");
  223. }
  224. }