Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2020 Richard Hauswald - https://quantummaid.de/.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package de.quantummaid.injectmaid;
import de.quantummaid.injectmaid.api.Injector;
import de.quantummaid.injectmaid.api.ReusePolicy;
import de.quantummaid.injectmaid.api.SingletonType;
import de.quantummaid.injectmaid.api.interception.*;
import de.quantummaid.injectmaid.closing.Closer;
import de.quantummaid.injectmaid.instantiator.Instantiator;
import de.quantummaid.injectmaid.lifecyclemanagement.ExceptionDuringClose;
import de.quantummaid.injectmaid.lifecyclemanagement.LifecycleManager;
import de.quantummaid.injectmaid.timing.InstanceAndTimedDependencies;
import de.quantummaid.injectmaid.timing.InstantiationTime;
import de.quantummaid.injectmaid.timing.InstantiationTimes;
import de.quantummaid.injectmaid.timing.TimedInstantiation;
import de.quantummaid.reflectmaid.GenericType;
import de.quantummaid.reflectmaid.ReflectMaid;
import de.quantummaid.reflectmaid.resolvedtype.ResolvedType;
import de.quantummaid.reflectmaid.typescanner.TypeIdentifier;
import de.quantummaid.reflectmaid.typescanner.scopes.Scope;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import static de.quantummaid.injectmaid.InjectMaidBuilder.injectMaidBuilder;
import static de.quantummaid.injectmaid.InjectMaidException.injectMaidException;
import static de.quantummaid.injectmaid.ScopeManager.scopeManager;
import static de.quantummaid.injectmaid.ShutdownHook.shutdownHook;
import static de.quantummaid.injectmaid.SingletonStore.singletonStore;
import static de.quantummaid.injectmaid.api.interception.InterceptorFactories.interceptorFactories;
import static de.quantummaid.injectmaid.api.interception.overwrite.OverwritingInterceptor.overwritingInterceptor;
import static de.quantummaid.injectmaid.circledetector.CircularDependencyDetector.validateNoCircularDependencies;
import static de.quantummaid.injectmaid.timing.InstanceAndTimedDependencies.instanceWithNoDependencies;
import static de.quantummaid.injectmaid.timing.TimedInstantiation.timeInstantiation;
import static de.quantummaid.reflectmaid.typescanner.TypeIdentifier.typeIdentifierFor;
import static de.quantummaid.reflectmaid.typescanner.scopes.Scope.rootScope;
import static java.lang.Runtime.getRuntime;
import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@SuppressWarnings("java:S1200")
public final class InjectMaid implements Injector {
private final ReflectMaid reflectMaid;
private final Definitions definitions;
private final SingletonType defaultSingletonType;
private final SingletonStore singletonStore;
private final Scope scope;
private final ScopeManager scopeManager;
private final InterceptorFactories interceptorFactories;
private final List children = new ArrayList<>();
private final LifecycleManager lifecycleManager;
private final InjectMaid parent;
private final InstantiationTimes instantiationTimes;
public static InjectMaidBuilder anInjectMaid() {
final ReflectMaid reflectMaid = ReflectMaid.aReflectMaid();
return anInjectMaid(reflectMaid);
}
public static InjectMaidBuilder anInjectMaid(final ReflectMaid reflectMaid) {
return injectMaidBuilder(reflectMaid);
}
static InjectMaid injectMaid(final ReflectMaid reflectMaid,
final Definitions definitions,
final SingletonType defaultSingletonType,
final LifecycleManager lifecycleManager,
final List preConfiguredInterceptorFactories) {
validateNoCircularDependencies(definitions);
final Scope scope = rootScope();
final ScopeManager scopeManager = scopeManager();
final InterceptorFactories interceptorFactories = interceptorFactories(preConfiguredInterceptorFactories);
final InjectMaid injectMaid = new InjectMaid(
reflectMaid,
definitions,
defaultSingletonType,
singletonStore(),
scope,
scopeManager,
interceptorFactories,
lifecycleManager,
null,
InstantiationTimes.instantiationTimes(reflectMaid)
);
injectMaid.loadEagerSingletons();
return injectMaid;
}
@Override
public void initializeAllSingletons(final Duration enforcedMaxTime) {
initializeDefinitionsThat(Definition::isSingleton, enforcedMaxTime);
}
private void loadEagerSingletons() {
initializeDefinitionsThat(definition -> definition.isEagerSingleton(defaultSingletonType), null);
}
private void initializeDefinitionsThat(final Predicate predicate,
final Duration enforcedMaxTime) {
final Instant before = Instant.now();
definitions.definitionsOnScope(scope).stream()
.filter(predicate)
.forEach(definition -> {
final TypeIdentifier type = definition.type();
final TimedInstantiation