@Test(expected = RuntimeException.class) public void testExceptionWithConstructorWithMoreThanOneParam() { Foo4 foo4 = ReflectionUtil.newInstance(Foo4.class); }
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);  }