ClassUtils { public static <T> T newInstance(Class<T> clazz) throws SofaRpcRuntimeException { if (clazz.isPrimitive()) { return (T) getDefaultPrimitiveValue(clazz); } T t = getDefaultWrapperValue(clazz); if (t != null) { return t; } try { if (!(clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers()))) { try { Constructor<T> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (Exception ignore) { } } Constructor<T>[] constructors = (Constructor<T>[]) clazz.getDeclaredConstructors(); if (constructors == null || constructors.length == 0) { throw new SofaRpcRuntimeException("The " + clazz.getCanonicalName() + " has no default constructor!"); } Constructor<T> constructor = constructors[0]; if (constructor.getParameterTypes().length > 0) { for (Constructor<T> c : constructors) { if (c.getParameterTypes().length < constructor.getParameterTypes().length) { constructor = c; if (constructor.getParameterTypes().length == 0) { break; } } } } constructor.setAccessible(true); Class<?>[] argTypes = constructor.getParameterTypes(); Object[] args = new Object[argTypes.length]; for (int i = 0; i < args.length; i++) { args[i] = getDefaultPrimitiveValue(argTypes[i]); } return constructor.newInstance(args); } catch (SofaRpcRuntimeException e) { throw e; } catch (Throwable e) { throw new SofaRpcRuntimeException(e.getMessage(), e); } }  static Class forName(String className); static Class forName(String className, boolean initialize); static Class forName(String className, ClassLoader cl); static List<Method> getAllMethods(Class clazz); static List<Field> getAllFields(Class clazz); static T newInstance(Class<T> clazz); static T newInstanceWithArgs(Class<T> clazz, Class<?>[] argTypes, Object[] args); static Object getDefaultPrimitiveValue(Class clazz); static T getDefaultWrapperValue(Class<T> clazz); static String getMethodKey(String interfaceName, String methodName); static boolean isAssignableFrom(Class<?> interfaceClass, Class<?> implementClass);  }
@Test public void testNewInstance() throws Exception { short s = ClassUtils.newInstance(short.class); Assert.assertTrue(s == 0); Short s2 = ClassUtils.newInstance(Short.class); Assert.assertTrue(s2 == 0); int i = ClassUtils.newInstance(int.class); Assert.assertTrue(i == 0); Integer integer = ClassUtils.newInstance(Integer.class); Assert.assertTrue(integer == 0); long l = ClassUtils.newInstance(long.class); Assert.assertTrue(l == 0); Long l2 = ClassUtils.newInstance(Long.class); Assert.assertTrue(l2 == 0); double d = ClassUtils.newInstance(double.class); Assert.assertTrue(d == 0.0d); Double d2 = ClassUtils.newInstance(Double.class); Assert.assertTrue(d2 == 0.0d); float f = ClassUtils.newInstance(float.class); Assert.assertTrue(f == 0.0f); Float f2 = ClassUtils.newInstance(Float.class); Assert.assertTrue(f2 == 0.0f); byte b = ClassUtils.newInstance(byte.class); Assert.assertTrue(b == 0); Byte b2 = ClassUtils.newInstance(Byte.class); Assert.assertTrue(b2 == 0); char c = ClassUtils.newInstance(char.class); Assert.assertTrue(c == 0); Character c2 = ClassUtils.newInstance(Character.class); Assert.assertTrue(c2 == 0); boolean bl = ClassUtils.newInstance(boolean.class); Assert.assertFalse(bl); Boolean bl2 = ClassUtils.newInstance(Boolean.class); Assert.assertFalse(bl2); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass1.class)); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass2.class)); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass3.class)); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass4.class)); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass5.class)); Assert.assertNotNull(ClassUtils.newInstance(TestMemberClass6.class)); Assert.assertNotNull(ClassUtils.newInstance(TestClass1.class)); Assert.assertNotNull(ClassUtils.newInstance(TestClass2.class)); TestClass3 class3 = ClassUtils.newInstance(TestClass3.class); Assert.assertNotNull(class3); Assert.assertNull(class3.getName()); Assert.assertEquals(class3.getAge(), 0); }