@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[][][][][][][][]")); }
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 { 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 { 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 ); }
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 ); }
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 );  }