@Test(expected = RuntimeException.class) public void testInstantiateObjectWithNoDefaultConstructor() { ReflectiveInstantiator sut = new ReflectiveInstantiator(); sut.instantiateObject(MyModelViewWithExceptionThrowingDefaultConstructor.class); }
public Object instantiateObject(final Class< ? > objType) { Constructor< ? > emptyConstructor = null; for (Constructor< ? > constructor : objType.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { emptyConstructor = constructor; break; } } if (emptyConstructor == null) { String message = "Cannot find empty constructor for " + objType.getName(); log.error(message); throw new RuntimeException(message); } emptyConstructor.setAccessible(true); Object obj = null; try { obj = emptyConstructor.newInstance(new Object[0]); } catch (Exception e) { String message = "Couldn't instantiate: " + objType.getName(); log.error(message, e); throw new RuntimeException(message); } return obj; }
ReflectiveInstantiator { public Object instantiateObject(final Class< ? > objType) { Constructor< ? > emptyConstructor = null; for (Constructor< ? > constructor : objType.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { emptyConstructor = constructor; break; } } if (emptyConstructor == null) { String message = "Cannot find empty constructor for " + objType.getName(); log.error(message); throw new RuntimeException(message); } emptyConstructor.setAccessible(true); Object obj = null; try { obj = emptyConstructor.newInstance(new Object[0]); } catch (Exception e) { String message = "Couldn't instantiate: " + objType.getName(); log.error(message, e); throw new RuntimeException(message); } return obj; } }
ReflectiveInstantiator { public Object instantiateObject(final Class< ? > objType) { Constructor< ? > emptyConstructor = null; for (Constructor< ? > constructor : objType.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { emptyConstructor = constructor; break; } } if (emptyConstructor == null) { String message = "Cannot find empty constructor for " + objType.getName(); log.error(message); throw new RuntimeException(message); } emptyConstructor.setAccessible(true); Object obj = null; try { obj = emptyConstructor.newInstance(new Object[0]); } catch (Exception e) { String message = "Couldn't instantiate: " + objType.getName(); log.error(message, e); throw new RuntimeException(message); } return obj; }  }
ReflectiveInstantiator { public Object instantiateObject(final Class< ? > objType) { Constructor< ? > emptyConstructor = null; for (Constructor< ? > constructor : objType.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { emptyConstructor = constructor; break; } } if (emptyConstructor == null) { String message = "Cannot find empty constructor for " + objType.getName(); log.error(message); throw new RuntimeException(message); } emptyConstructor.setAccessible(true); Object obj = null; try { obj = emptyConstructor.newInstance(new Object[0]); } catch (Exception e) { String message = "Couldn't instantiate: " + objType.getName(); log.error(message, e); throw new RuntimeException(message); } return obj; }  Object instantiateObject(final Class< ? > objType); }
ReflectiveInstantiator { public Object instantiateObject(final Class< ? > objType) { Constructor< ? > emptyConstructor = null; for (Constructor< ? > constructor : objType.getDeclaredConstructors()) { if (constructor.getParameterTypes().length == 0) { emptyConstructor = constructor; break; } } if (emptyConstructor == null) { String message = "Cannot find empty constructor for " + objType.getName(); log.error(message); throw new RuntimeException(message); } emptyConstructor.setAccessible(true); Object obj = null; try { obj = emptyConstructor.newInstance(new Object[0]); } catch (Exception e) { String message = "Couldn't instantiate: " + objType.getName(); log.error(message, e); throw new RuntimeException(message); } return obj; }  Object instantiateObject(final Class< ? > objType);  }