ClassTypeUtils { public static Class getClass(String typeStr) { Class clazz = ReflectCache.getClassCache(typeStr); if (clazz == null) { if ("void".equals(typeStr)) { clazz = void.class; } else if ("boolean".equals(typeStr)) { clazz = boolean.class; } else if ("byte".equals(typeStr)) { clazz = byte.class; } else if ("char".equals(typeStr)) { clazz = char.class; } else if ("double".equals(typeStr)) { clazz = double.class; } else if ("float".equals(typeStr)) { clazz = float.class; } else if ("int".equals(typeStr)) { clazz = int.class; } else if ("long".equals(typeStr)) { clazz = long.class; } else if ("short".equals(typeStr)) { clazz = short.class; } else { String jvmName = canonicalNameToJvmName(typeStr); clazz = ClassUtils.forName(jvmName); } ReflectCache.putClassCache(typeStr, clazz); } return clazz; }  static Class[] getClasses(String[] typeStrs); static Class getClass(String typeStr); static String canonicalNameToJvmName(String canonicalName); static String[] getTypeStrs(Class[] types); static String[] getTypeStrs(Class[] types, boolean javaStyle); static String getTypeStr(Class clazz); static String jvmNameToCanonicalName(String jvmName);  }
@Test public void testGetClass() { Assert.assertEquals(String.class, ClassTypeUtils.getClass("java.lang.String")); Assert.assertEquals(int.class, ClassTypeUtils.getClass("int")); Assert.assertEquals(boolean.class, ClassTypeUtils.getClass("boolean")); Assert.assertEquals(byte.class, ClassTypeUtils.getClass("byte")); Assert.assertEquals(char.class, ClassTypeUtils.getClass("char")); Assert.assertEquals(double.class, ClassTypeUtils.getClass("double")); Assert.assertEquals(float.class, ClassTypeUtils.getClass("float")); Assert.assertEquals(int.class, ClassTypeUtils.getClass("int")); Assert.assertEquals(long.class, ClassTypeUtils.getClass("long")); Assert.assertEquals(short.class, ClassTypeUtils.getClass("short")); Assert.assertEquals(void.class, ClassTypeUtils.getClass("void")); class LocalType { } Assert.assertEquals(anonymous.getClass(), ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$1")); Assert.assertEquals(LocalType.class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$1LocalType")); Assert.assertEquals(MemberClass.class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$MemberClass")); Assert.assertEquals(StaticClass.class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.StaticClass")); Assert.assertEquals(String[].class, ClassTypeUtils.getClass("java.lang.String[]")); Assert.assertEquals(boolean[].class, ClassTypeUtils.getClass("boolean[]")); Assert.assertEquals(byte[].class, ClassTypeUtils.getClass("byte[]")); Assert.assertEquals(char[].class, ClassTypeUtils.getClass("char[]")); Assert.assertEquals(double[].class, ClassTypeUtils.getClass("double[]")); Assert.assertEquals(float[].class, ClassTypeUtils.getClass("float[]")); Assert.assertEquals(int[].class, ClassTypeUtils.getClass("int[]")); Assert.assertEquals(long[].class, ClassTypeUtils.getClass("long[]")); Assert.assertEquals(short[].class, ClassTypeUtils.getClass("short[]")); Assert.assertEquals(Array.newInstance(anonymous.getClass(), 2, 3).getClass(), ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$1[][]")); Assert.assertEquals(LocalType[][].class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$1LocalType[][]")); Assert.assertEquals(MemberClass[].class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.ClassTypeUtilsTest$MemberClass[]")); Assert.assertEquals(StaticClass[].class, ClassTypeUtils.getClass("com.alipay.sofa.rpc.common.utils.StaticClass[]")); Assert.assertEquals(int[][].class, ClassTypeUtils.getClass("int[][]")); Assert.assertEquals(String[].class, ClassTypeUtils.getClass(String[].class.getName())); Assert.assertEquals(boolean[].class, ClassTypeUtils.getClass(boolean[].class.getName())); Assert.assertEquals(byte[].class, ClassTypeUtils.getClass(byte[].class.getName())); Assert.assertEquals(char[].class, ClassTypeUtils.getClass(char[].class.getName())); Assert.assertEquals(double[].class, ClassTypeUtils.getClass(double[].class.getName())); Assert.assertEquals(float[].class, ClassTypeUtils.getClass(float[].class.getName())); Assert.assertEquals(int[].class, ClassTypeUtils.getClass(int[].class.getName())); Assert.assertEquals(long[].class, ClassTypeUtils.getClass(long[].class.getName())); Assert.assertEquals(short[].class, ClassTypeUtils.getClass(short[].class.getName())); Assert.assertEquals(int[][].class, ClassTypeUtils.getClass(int[][].class.getName())); }
@Test public void testGetClassAccordingToTCL() { URLClassLoader current = (URLClassLoader) this.getClass().getClassLoader(); TempClassLoader t0 = new TempClassLoader(current.getURLs(), null); TempClassLoader t1 = new TempClassLoader(current.getURLs(), null); ClassLoader old = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(t0); Class c0 = ClassTypeUtils.getClass(ClassTypeUtils.class.getCanonicalName()); Thread.currentThread().setContextClassLoader(t1); Class c1 = ClassTypeUtils.getClass(ClassTypeUtils.class.getCanonicalName()); Thread.currentThread().setContextClassLoader(t0); Assert.assertEquals(c0, ReflectCache.getClassCache(ClassTypeUtils.class.getCanonicalName())); Thread.currentThread().setContextClassLoader(t1); Assert.assertEquals(c1, ReflectCache.getClassCache(ClassTypeUtils.class.getCanonicalName())); Thread.currentThread().setContextClassLoader(old); }