public static <T> Supplier<T> costructor(Constructor<T> constructor, Parameter<?>... parameters) { final Class<?>[] params = constructor.getParameterTypes(); if (params.length == 0) { return new StaticConstructorSupplier<T>(constructor, NO_ARGS); } Argument<?>[] arguments = Argument.arguments(parameterTypes(constructor), parameters); return Argument.allConstants(arguments) ? new StaticConstructorSupplier<T>(constructor, Argument.constantsFrom(arguments)) : new ConstructorSupplier<T>(constructor, arguments); }
@Override public T supply(Dependency<? super T> dependency, Injector injector) { Object owner = instance; if (instanceMethod && owner == null) { owner = injector.resolve(dependency(factory.getDeclaringClass())); } final Object[] args = Argument.resolve(dependency, injector, arguments); return returnType.getRawType().cast(Invoke.method(factory, owner, args)); }
public static <T> Supplier<T> method( Type<T> returnType, Method factory, Object instance, Parameter<?>... parameters) { if (!Type.returnType(factory).isAssignableTo(returnType)) { throw new IllegalArgumentException( "The factory methods methods return type `" + Type.returnType(factory) + "` is not assignable to: " + returnType); } if (instance != null && factory.getDeclaringClass() != instance.getClass()) { throw new IllegalArgumentException( "The factory method and the instance it is invoked on have to be the same class."); } Argument<?>[] arguments = Argument.arguments(Type.parameterTypes(factory), parameters); return new FactoryMethodSupplier<T>(returnType, factory, instance, arguments); }
@Override public T supply(Dependency<? super T> dependency, Injector injector) { return Invoke.constructor(constructor, Argument.resolve(dependency, injector, arguments)); }