ReflectUtils { public static String getName(Class<?> c) { if (c.isArray()) { StringBuilder sb = new StringBuilder(); do { sb.append("[]"); c = c.getComponentType(); } while (c.isArray()); return c.getName() + sb.toString(); } return c.getName(); } private  ReflectUtils(); static boolean isPrimitives(Class<?> cls); static boolean isPrimitive(Class<?> cls); static Class<?> getBoxedClass(Class<?> c); static boolean isCompatible(Class<?> c, Object o); static boolean isCompatible(Class<?>[] cs, Object[] os); static String getCodeBase(Class<?> cls); static String getName(Class<?> c); static Class<?> getGenericClass(Class<?> cls); static Class<?> getGenericClass(Class<?> cls, int i); static String getName(final Method m); static String getSignature(String methodName, Class<?>[] parameterTypes); static String getName(final Constructor<?> c); static String getDesc(Class<?> c); static String getDesc(final Class<?>[] cs); static String getDesc(final Method m); static String[] getDescArray(final Method m); static String getDesc(final Constructor<?> c); static String getDescWithoutMethodName(Method m); static String getDesc(final CtClass c); static String getDesc(final CtMethod m); static String getDesc(final CtConstructor c); static String getDescWithoutMethodName(final CtMethod m); static String name2desc(String name); static String desc2name(String desc); static Class<?> forName(String name); static Class<?> forName(ClassLoader cl, String name); static Class<?> name2class(String name); static Class<?> desc2class(String desc); static Class<?>[] desc2classArray(String desc); @Deprecated static Method findMethodByMethodSignature(Class<?> clazz, String methodName, String[] parameterTypes); @Deprecated static Method findMethodByMethodName(Class<?> clazz, String methodName); static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType); static boolean isInstance(Object obj, String interfaceClazzName); static Object getEmptyObject(Class<?> returnType); static Object defaultReturn(Method m); static Object defaultReturn(Class<?> classType); static boolean isBeanPropertyReadMethod(Method method); static String getPropertyNameFromBeanReadMethod(Method method); static boolean isBeanPropertyWriteMethod(Method method); static String getPropertyNameFromBeanWriteMethod(Method method); static boolean isPublicInstanceField(Field field); static Map<String, Field> getBeanPropertyFields(Class cl); static Map<String, Method> getBeanPropertyReadMethods(Class cl); static Type[] getReturnTypes(Method method); static Set<ParameterizedType> findParameterizedTypes(Class<?> sourceClass); static Set<Class<T>> findHierarchicalTypes(Class<?> sourceClass, Class<T> matchType); static T getProperty(Object bean, String methodName); static Class[] resolveTypes(Object... values); static final char JVM_VOID; static final char JVM_BOOLEAN; static final char JVM_BYTE; static final char JVM_CHAR; static final char JVM_DOUBLE; static final char JVM_FLOAT; static final char JVM_INT; static final char JVM_LONG; static final char JVM_SHORT; static final Class<?>[] EMPTY_CLASS_ARRAY; static final String JAVA_IDENT_REGEX; static final String JAVA_NAME_REGEX; static final String CLASS_DESC; static final String ARRAY_DESC; static final String DESC_REGEX; static final Pattern DESC_PATTERN; static final String METHOD_DESC_REGEX; static final Pattern METHOD_DESC_PATTERN; static final Pattern GETTER_METHOD_DESC_PATTERN; static final Pattern SETTER_METHOD_DESC_PATTERN; static final Pattern IS_HAS_CAN_METHOD_DESC_PATTERN; }
@Test public void testGetName() throws Exception { assertEquals("boolean", ReflectUtils.getName(boolean.class)); assertEquals("int[][][]", ReflectUtils.getName(int[][][].class)); assertEquals("java.lang.Object[][]", ReflectUtils.getName(Object[][].class)); assertEquals("Z", ReflectUtils.getDesc(boolean.class)); assertEquals("[[[I", ReflectUtils.getDesc(int[][][].class)); assertEquals("[[Ljava/lang/Object;", ReflectUtils.getDesc(Object[][].class)); assertEquals("Z", ReflectUtils.name2desc(ReflectUtils.getName(boolean.class))); assertEquals("[[[I", ReflectUtils.name2desc(ReflectUtils.getName(int[][][].class))); assertEquals("[[Ljava/lang/Object;", ReflectUtils.name2desc(ReflectUtils.getName(Object[][].class))); assertEquals("short[]", ReflectUtils.desc2name(ReflectUtils.getDesc(short[].class))); assertEquals("boolean[]", ReflectUtils.desc2name(ReflectUtils.getDesc(boolean[].class))); assertEquals("byte[]", ReflectUtils.desc2name(ReflectUtils.getDesc(byte[].class))); assertEquals("char[]", ReflectUtils.desc2name(ReflectUtils.getDesc(char[].class))); assertEquals("double[]", ReflectUtils.desc2name(ReflectUtils.getDesc(double[].class))); assertEquals("float[]", ReflectUtils.desc2name(ReflectUtils.getDesc(float[].class))); assertEquals("int[]", ReflectUtils.desc2name(ReflectUtils.getDesc(int[].class))); assertEquals("long[]", ReflectUtils.desc2name(ReflectUtils.getDesc(long[].class))); assertEquals("int", ReflectUtils.desc2name(ReflectUtils.getDesc(int.class))); assertEquals("void", ReflectUtils.desc2name(ReflectUtils.getDesc(void.class))); assertEquals("java.lang.Object[][]", ReflectUtils.desc2name(ReflectUtils.getDesc(Object[][].class))); }
@Test public void testGetMethodName() throws Exception { assertThat(ReflectUtils.getName(Foo2.class.getDeclaredMethod("hello", int[].class)), equalTo("java.util.List hello(int[])")); }
@Test public void testGetConstructorName() throws Exception { Constructor c = Foo2.class.getConstructors()[0]; assertThat(ReflectUtils.getName(c), equalTo("(java.util.List,int[])")); }