Reflection { public static String getClassName( final Class<?> clazz ) { final String fullName = clazz.getName(); final int fullNameLength = fullName.length(); int numArrayDimensions = 0; while (numArrayDimensions < fullNameLength) { final char c = fullName.charAt(numArrayDimensions); if (c != '[') { String name = null; switch (c) { case 'L': { name = fullName.subSequence(numArrayDimensions + 1, fullNameLength).toString(); break; } case 'B': { name = "byte"; break; } case 'C': { name = "char"; break; } case 'D': { name = "double"; break; } case 'F': { name = "float"; break; } case 'I': { name = "int"; break; } case 'J': { name = "long"; break; } case 'S': { name = "short"; break; } case 'Z': { name = "boolean"; break; } case 'V': { name = "void"; break; } default: { name = fullName.subSequence(numArrayDimensions, fullNameLength).toString(); } } if (numArrayDimensions == 0) { return name; } if (numArrayDimensions < BRACKETS_PAIR.length) { name = name + BRACKETS_PAIR[numArrayDimensions]; } else { for (int i = 0; i < numArrayDimensions; i++) { name = name + BRACKETS_PAIR[1]; } } return name; } ++numArrayDimensions; } return fullName; } Reflection( Class<?> targetClass ); static Class<?>[] buildArgumentClasses( Object... arguments ); static List<Class<?>> buildArgumentClassList( Object... arguments ); static List<Class<?>> convertArgumentClassesToPrimitives( Class<?>... arguments ); static String getClassName( final Class<?> clazz ); static void setValue(Object instance, String fieldName, Object value); static Field getField(String fieldName, Class<?> objectClass); static Method findMethod(Class<?> type, String methodName); static Field findFieldRecursively(Class<?> c, String fieldName); @SuppressWarnings( "unchecked" ) static T getInstance(String classname, ClassLoader cl); static Object invokeAccessibly(Object instance, Method method, Object[] parameters); Class<?> getTargetClass(); Method[] findMethods( String methodName,
                                 boolean caseSensitive ); Method[] findMethods( Pattern methodNamePattern ); Method[] findGetterMethods(); String[] findGetterPropertyNames(); Method findFirstMethod( String methodName,
                                   boolean caseSensitive ); Method findFirstMethod( Pattern methodNamePattern ); Iterable<Method> findAllMethods( String methodName,
                                            boolean caseSensitive ); Iterable<Method> findAllMethods( Pattern methodNamePattern ); Object invokeBestMethodOnTarget( String[] methodNames,
                                            final Object target,
                                            final Object... arguments ); Object invokeSetterMethodOnTarget( String javaPropertyName,
                                              Object target,
                                              Object argument ); Object invokeGetterMethodOnTarget( String javaPropertyName,
                                              Object target ); Method findBestMethodOnTarget( String methodName,
                                          Object... arguments ); Method findBestMethodWithSignature( String methodName,
                                               Class<?>... argumentsClasses ); Method findBestMethodWithSignature( String methodName,
                                               boolean caseSensitive,
                                               Class<?>... argumentsClasses ); Property getProperty( Object target,
                                 String propertyName,
                                 String label,
                                 String category,
                                 String description,
                                 Object... allowedValues ); Property getProperty( Object target,
                                 String propertyName,
                                 String description ); Property getProperty( Object target,
                                 String propertyName ); List<Property> getAllPropertiesOn( Object target ); Map<String, Property> getAllPropertiesByNameOn( Object target ); void setProperty( Object target,
                             Property property,
                             Object value ); Object getProperty( Object target,
                               Property property ); String getPropertyAsString( Object target,
                                       Property property );  }
@Test public void shouldGetClassNameForClass() { assertThat(Reflection.getClassName(String.class), is(String.class.getName())); assertThat(Reflection.getClassName(ArrayList.class), is(ArrayList.class.getName())); assertThat(Reflection.getClassName(StringUtil.class), is(StringUtil.class.getName())); }
@Test public void shouldGetClassNameOfInterface() { assertThat(Reflection.getClassName(CharSequence.class), is(CharSequence.class.getName())); assertThat(Reflection.getClassName(List.class), is(List.class.getName())); }
@Test public void shouldGetClassNameWithPrimitive() { assertThat(Reflection.getClassName(Integer.TYPE), is("int")); assertThat(Reflection.getClassName(Boolean.TYPE), is("boolean")); assertThat(Reflection.getClassName(Long.TYPE), is("long")); assertThat(Reflection.getClassName(Short.TYPE), is("short")); assertThat(Reflection.getClassName(Float.TYPE), is("float")); assertThat(Reflection.getClassName(Double.TYPE), is("double")); assertThat(Reflection.getClassName(Character.TYPE), is("char")); assertThat(Reflection.getClassName(Byte.TYPE), is("byte")); assertThat(Reflection.getClassName(Void.TYPE), is("void")); }
@Test public void shouldGetClassNameWith1DPrimitiveArray() { assertThat(Reflection.getClassName(new int[0].getClass()), is("int[]")); assertThat(Reflection.getClassName(new boolean[0].getClass()), is("boolean[]")); assertThat(Reflection.getClassName(new long[0].getClass()), is("long[]")); assertThat(Reflection.getClassName(new short[0].getClass()), is("short[]")); assertThat(Reflection.getClassName(new float[0].getClass()), is("float[]")); assertThat(Reflection.getClassName(new double[0].getClass()), is("double[]")); assertThat(Reflection.getClassName(new char[0].getClass()), is("char[]")); assertThat(Reflection.getClassName(new byte[0].getClass()), is("byte[]")); }
@Test public void shouldGetClassNameWith2DPrimitiveArray() { assertThat(Reflection.getClassName(new int[0][0].getClass()), is("int[][]")); assertThat(Reflection.getClassName(new boolean[0][0].getClass()), is("boolean[][]")); assertThat(Reflection.getClassName(new long[0][0].getClass()), is("long[][]")); assertThat(Reflection.getClassName(new short[0][0].getClass()), is("short[][]")); assertThat(Reflection.getClassName(new float[0][0].getClass()), is("float[][]")); assertThat(Reflection.getClassName(new double[0][0].getClass()), is("double[][]")); assertThat(Reflection.getClassName(new char[0][0].getClass()), is("char[][]")); assertThat(Reflection.getClassName(new byte[0][0].getClass()), is("byte[][]")); }
@Test public void shouldGetClassNameWith3DPrimitiveArray() { assertThat(Reflection.getClassName(new int[0][0][0].getClass()), is("int[][][]")); assertThat(Reflection.getClassName(new boolean[0][0][0].getClass()), is("boolean[][][]")); assertThat(Reflection.getClassName(new long[0][0][0].getClass()), is("long[][][]")); assertThat(Reflection.getClassName(new short[0][0][0].getClass()), is("short[][][]")); assertThat(Reflection.getClassName(new float[0][0][0].getClass()), is("float[][][]")); assertThat(Reflection.getClassName(new double[0][0][0].getClass()), is("double[][][]")); assertThat(Reflection.getClassName(new char[0][0][0].getClass()), is("char[][][]")); assertThat(Reflection.getClassName(new byte[0][0][0].getClass()), is("byte[][][]")); }
@Test public void shouldGetClassNameWith8DPrimitiveArray() { assertThat(Reflection.getClassName(new int[0][0][0][0][0][0][0][0].getClass()), is("int[][][][][][][][]")); assertThat(Reflection.getClassName(new boolean[0][0][0][0][0][0][0][0].getClass()), is("boolean[][][][][][][][]")); assertThat(Reflection.getClassName(new long[0][0][0][0][0][0][0][0].getClass()), is("long[][][][][][][][]")); assertThat(Reflection.getClassName(new short[0][0][0][0][0][0][0][0].getClass()), is("short[][][][][][][][]")); assertThat(Reflection.getClassName(new float[0][0][0][0][0][0][0][0].getClass()), is("float[][][][][][][][]")); assertThat(Reflection.getClassName(new double[0][0][0][0][0][0][0][0].getClass()), is("double[][][][][][][][]")); assertThat(Reflection.getClassName(new char[0][0][0][0][0][0][0][0].getClass()), is("char[][][][][][][][]")); assertThat(Reflection.getClassName(new byte[0][0][0][0][0][0][0][0].getClass()), is("byte[][][][][][][][]")); }