ReflectionUtils { public static void assertArrayIndex(Object array, int index) throws IllegalArgumentException { if (index < 0) { String message = String.format("The index argument must be positive , actual is %s", index); throw new ArrayIndexOutOfBoundsException(message); } ReflectionUtils.assertArrayType(array); int length = Array.getLength(array); if (index > length - 1) { String message = String.format("The index must be less than %s , actual is %s", length, index); throw new ArrayIndexOutOfBoundsException(message); } }  @Nonnull static String getCallerClassName(); @Nonnull static Class<?> getCallerClass(); static Package getCallerPackage(); static void assertArrayIndex(Object array, int index); static void assertArrayType(Object array); static void assertFieldMatchType(Object object, String fieldName, Class<?> expectedType); @Nonnull static List<T> toList(Object array); @Nonnull static Map<String, Object> readFieldsAsMap(Object object); static final String SUN_REFLECT_REFLECTION_CLASS_NAME; }
@Test public void testAssertArrayIndex() { int size = 10; Object array = Array.newInstance(int.class, size); for (int i = 0; i < size; i++) { ReflectionUtils.assertArrayIndex(array, i); } for (int i = size; i < size * 2; i++) { ArrayIndexOutOfBoundsException exception = null; try { ReflectionUtils.assertArrayIndex(array, i); } catch (ArrayIndexOutOfBoundsException e) { exception = e; System.err.println(e.getMessage()); } Assert.assertNotNull(exception); } }