Object (Class<?> key) { Class<?> implementation = implementationClassCache.get(key); Class<?> aClass = implementation == null ? key : implementation; try { Constructor<?> constructor = aClass.getDeclaredConstructor(); constructor.setAccessible(true); return constructor.newInstance(); } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { throw new RuntimeException("Couldn't instantiate " + aClass, e); } }