ReflectionUtils { @Api(2.0) @Nullable public static <T> T newInstance(@NonNull Class<T> clazz) { try { Constructor<T> constructor = clazz.getDeclaredConstructor(); if (constructor != null) { constructor.setAccessible(true); return constructor.newInstance(); } return null; } catch (InstantiationException e) { throw new HaloReflectionException("The instance for " + clazz.getCanonicalName() + " can not be created.", e); } catch (IllegalAccessException e) { throw new HaloReflectionException("The default constructor of " + clazz.getCanonicalName() + " is not visible to create the instance.", e); } catch (NoSuchMethodException e) { throw new HaloReflectionException("No default constructor found for " + clazz.getCanonicalName() + ".", e); } catch (InvocationTargetException e) { throw new HaloReflectionException("The default constructor could not be invoked on " + clazz.getCanonicalName() + ".", e); } } private  ReflectionUtils(); @Api(2.0) @Nullable static Class<?> toClass(@Nullable String name); @Api(2.0) @Nullable static T newInstance(@NonNull Class<T> clazz); @Api(2.0) @NonNull static Constructor<T> getConstructor(@NonNull Class<T> constructedClass, @NonNull Class<D> innerConstructorClass); @Api(2.0) @NonNull static T build(@NonNull Constructor<T> constructor, @Nullable Object... data); @Api(2.0) @NonNull static String classNameToInnerFilter(@NonNull String className); @Api(2.0) @NonNull static Field[] getFields(@NonNull Class clazz, boolean bringParents); @Api(2.0) @NonNull static T valueOf(Field field, Object reference, Class<T> clazz);  }
@Test public void newInstanceTest() { PublicConstructorNoParam instanciable = ReflectionUtils.newInstance(PublicConstructorNoParam.class); Assert.assertNotNull(instanciable); }
@Test public void newInstancePrivateConstructorTest() { Assert.assertNotNull(ReflectionUtils.newInstance(PrivateConstructor.class)); }
@Test(expected = HaloReflectionException.class) public void newInstanceExceptionTest() { ReflectionUtils.newInstance(PublicConstructorParam.class); }