@Test public void testNewInstance() { String foo = ReflectionUtil.newInstance(String.class); assertNotNull(foo); Foo1 foo1 = ReflectionUtil.newInstance(Foo1.class); assertNotNull(foo1); Foo2 foo2 = ReflectionUtil.newInstance(Foo2.class); assertNotNull(foo2); Foo3 foo3 = ReflectionUtil.newInstance(Foo3.class); assertNotNull(foo3); class FooConverter implements Converter<FooConverter, ConverterInvocation> { @Override public FooConverter convert(ConverterInvocation input) { return this; } } FooConverter foo4 = ReflectionUtil.newInstance(FooConverter.class); assertNotNull(foo4); }
public static <T> T newInstance(final Class<T> clazz) { if(clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); } T instance = null; for(Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } throw new RuntimeException("Could not instantiate class: "+clazz+", no access to constructors."); }
ReflectionUtil { public static <T> T newInstance(final Class<T> clazz) { if(clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); } T instance = null; for(Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } throw new RuntimeException("Could not instantiate class: "+clazz+", no access to constructors."); } }
ReflectionUtil { public static <T> T newInstance(final Class<T> clazz) { if(clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); } T instance = null; for(Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } throw new RuntimeException("Could not instantiate class: "+clazz+", no access to constructors."); }  }
ReflectionUtil { public static <T> T newInstance(final Class<T> clazz) { if(clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); } T instance = null; for(Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } throw new RuntimeException("Could not instantiate class: "+clazz+", no access to constructors."); }  static T newInstance(final Class<T> clazz); }
ReflectionUtil { public static <T> T newInstance(final Class<T> clazz) { if(clazz.isAnonymousClass() || clazz.isInterface() || clazz.isAnnotation()) { throw new RuntimeException("Can not build new instance of an " + clazz.getName()); } T instance = null; for(Constructor<?> constructor : clazz.getConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } for(Constructor<?> constructor : clazz.getDeclaredConstructors()) { instance = (T) instantiateWithConstructor(constructor); if (isValidInstance(instance)) return instance; } throw new RuntimeException("Could not instantiate class: "+clazz+", no access to constructors."); }  static T newInstance(final Class<T> clazz);  }