InjectorBuilderImpl implements InjectorBuilder { @Override public Injector build() throws InstantiationException { final File configFile = configFileLocation.toFile(); final Properties properties = loadProperties(INTERNAL_LOGGER, configFile); overriddenParams.forEach(properties::setProperty); final Set<Class<?>> allInjectableTypes = unmodifiableSet( injectables.values().stream() .flatMap(List::stream) .map(Injectable::get) .collect(toSet()) ); final Set<Injectable<?>> injectablesSet = unmodifiableSet( injectables.values().stream() .flatMap(List::stream) .collect(toCollection((Supplier<Set<Injectable<?>>>) LinkedHashSet::new)) ); final DependencyGraph graph = DependencyGraph.create(injectablesSet.stream().map(Injectable::get), this::proxyFor); final Map<Class<?>, Object> instanceMap = injectablesSet.stream() .map(Injectable::get) .collect(toMap( Function.identity(), i -> UNINSTANTIATED, (a, b) -> a, LinkedHashMap::new )); INTERNAL_LOGGER.debug(String.format("Creating %d injectable instances.", injectablesSet.size())); INTERNAL_LOGGER.debug(HORIZONTAL_LINE); final Set<Injectable<?>> injectablesLeft = new LinkedHashSet<>(injectablesSet); while (!injectablesLeft.isEmpty()) { tryToCreateInstances(properties, allInjectableTypes, instanceMap, injectablesLeft); } final LinkedList<Object> instances = instancesSoFarInReversedOrder(instanceMap); instanceMap.clear(); final Injector injector = new InjectorImpl( injectablesSet, unmodifiableList(instances), properties, classLoader, graph, this ); final ClassMapper classMapper = new ClassMapper() { @Override public <T> T apply(Class<T> type) { return findIn( type, injector, instances, true ); } @Override public <T> T applyOrNull(Class<T> type) { return findIn( type, injector, instances, false ); } }; instances.forEach(this::assertAnnotationsCorrect); instances.forEach(instance -> setAutoInjectedFields(instance, instances, injector)); executions.stream() .map(builder -> builder.build(graph)) .forEachOrdered(execution -> { final DependencyNode node = graph.get(execution.getType()); node.getExecutions().add(execution); }); final AtomicBoolean hasAnythingChanged = new AtomicBoolean(); final AtomicInteger nextState = new AtomicInteger(0); while (nextState.get() <= State.STARTED.ordinal()) { handleNextState(graph, instances, injector, classMapper, hasAnythingChanged, nextState); } INTERNAL_LOGGER.debug(HORIZONTAL_LINE); INTERNAL_LOGGER.debug( "| %-79s |", "All " + instances.size() + " components have been configured!" ); INTERNAL_LOGGER.debug(HORIZONTAL_LINE); return injector; } InjectorBuilderImpl();   InjectorBuilderImpl(ClassLoader classLoader);   InjectorBuilderImpl(Set<Class<?>> injectables); private  InjectorBuilderImpl(ClassLoader classLoader, Set<Class<?>> injectables);   InjectorBuilderImpl(final InjectorBuilder toCopy); @Override InjectorBuilder withComponent(Class<?> injectableType); @Override InjectorBuilder withComponent(Class<T> injectableType, Supplier<T> instanceSupplier); @Override InjectorBuilder withBundle(Class<? extends InjectBundle> bundleClass); @Override InjectorBuilder withInjectorProxy(InjectorProxy injectorProxy); @Override InjectorBuilder withConfigFileLocation(Path configFile); @Override InjectorBuilder withParam(String name, String value); @Override InjectorBuilder before(ExecutionBuilder<T> executionBuilder); @Override Injector build(); @Override InjectorProxy proxyFor(Class<?> clazz); static final Logger INTERNAL_LOGGER; }
@Test void construct() throws InstantiationException { final InjectorBuilder builder = new InjectorBuilderImpl(); final Injector injector = builder.build(); assertEquals(0, injector.injectables().count()); }
@Test void construct2() throws InstantiationException { final InjectorBuilder builder = new InjectorBuilderImpl(InjectorBuilderImpl.class.getClassLoader()); final Injector injector = builder.build(); assertEquals(0, injector.injectables().count()); assertEquals(InjectorBuilderImpl.class.getClassLoader(), injector.classLoader()); }
@Test void construct3() throws InstantiationException { final InjectorBuilder builder = new InjectorBuilderImpl(singleton(Foo.class)); final Injector injector = builder.build(); assertEquals(1, injector.injectables().count()); }