@Test public void providerConstructor() { Map<Type, Object> bindings = new HashMap<>(); bindings.put(ProviderConstructor.getProviderType(), (Provider<Integer>) () -> 1); ClassFactory factory = new DefaultClassFactory(bindings::get); ProviderConstructor obj = factory.newInstance(ProviderConstructor.class); Assert.assertEquals(1, obj.getValue()); }
@SuppressWarnings("unchecked") @Override public <T> T newInstance(Type type, Function<Type, Object> overrides) throws CreationException { Class<?> cls = (Class<?>) type; Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length == 1) { Constructor<?> c = constructors[0]; return newInstance(cls, c, c.getGenericParameterTypes(), overrides); } else { for (Constructor<?> c : constructors) { Type[] ptypes = c.getGenericParameterTypes(); if (ptypes.length == 0) { return newInstance(cls, c, ptypes, overrides); } } throw new CreationException("class " + cls.getCanonicalName() + " has more than one constructor and does not have a no-argument constructor"); } }
DefaultClassFactory implements ClassFactory { @SuppressWarnings("unchecked") @Override public <T> T newInstance(Type type, Function<Type, Object> overrides) throws CreationException { Class<?> cls = (Class<?>) type; Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length == 1) { Constructor<?> c = constructors[0]; return newInstance(cls, c, c.getGenericParameterTypes(), overrides); } else { for (Constructor<?> c : constructors) { Type[] ptypes = c.getGenericParameterTypes(); if (ptypes.length == 0) { return newInstance(cls, c, ptypes, overrides); } } throw new CreationException("class " + cls.getCanonicalName() + " has more than one constructor and does not have a no-argument constructor"); } } }
DefaultClassFactory implements ClassFactory { @SuppressWarnings("unchecked") @Override public <T> T newInstance(Type type, Function<Type, Object> overrides) throws CreationException { Class<?> cls = (Class<?>) type; Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length == 1) { Constructor<?> c = constructors[0]; return newInstance(cls, c, c.getGenericParameterTypes(), overrides); } else { for (Constructor<?> c : constructors) { Type[] ptypes = c.getGenericParameterTypes(); if (ptypes.length == 0) { return newInstance(cls, c, ptypes, overrides); } } throw new CreationException("class " + cls.getCanonicalName() + " has more than one constructor and does not have a no-argument constructor"); } } DefaultClassFactory();  DefaultClassFactory(Function<Type, Object> bindings); }
DefaultClassFactory implements ClassFactory { @SuppressWarnings("unchecked") @Override public <T> T newInstance(Type type, Function<Type, Object> overrides) throws CreationException { Class<?> cls = (Class<?>) type; Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length == 1) { Constructor<?> c = constructors[0]; return newInstance(cls, c, c.getGenericParameterTypes(), overrides); } else { for (Constructor<?> c : constructors) { Type[] ptypes = c.getGenericParameterTypes(); if (ptypes.length == 0) { return newInstance(cls, c, ptypes, overrides); } } throw new CreationException("class " + cls.getCanonicalName() + " has more than one constructor and does not have a no-argument constructor"); } } DefaultClassFactory();  DefaultClassFactory(Function<Type, Object> bindings); @SuppressWarnings("unchecked") @Override T newInstance(Type type, Function<Type, Object> overrides); }
DefaultClassFactory implements ClassFactory { @SuppressWarnings("unchecked") @Override public <T> T newInstance(Type type, Function<Type, Object> overrides) throws CreationException { Class<?> cls = (Class<?>) type; Constructor<?>[] constructors = cls.getDeclaredConstructors(); if (constructors.length == 1) { Constructor<?> c = constructors[0]; return newInstance(cls, c, c.getGenericParameterTypes(), overrides); } else { for (Constructor<?> c : constructors) { Type[] ptypes = c.getGenericParameterTypes(); if (ptypes.length == 0) { return newInstance(cls, c, ptypes, overrides); } } throw new CreationException("class " + cls.getCanonicalName() + " has more than one constructor and does not have a no-argument constructor"); } } DefaultClassFactory();  DefaultClassFactory(Function<Type, Object> bindings); @SuppressWarnings("unchecked") @Override T newInstance(Type type, Function<Type, Object> overrides);  }