/*
 * Copyright 2018-2024 the original author or authors.
 *
 * Licensed 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
 *
 *      https://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 org.springframework.modulith.core;

import static com.tngtech.archunit.base.DescribedPredicate.*;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import static java.lang.System.*;
import static java.util.Comparator.*;
import static org.springframework.modulith.core.SyntacticSugar.*;
import static org.springframework.modulith.core.Types.JavaXTypes.*;
import static org.springframework.modulith.core.Types.SpringDataTypes.*;
import static org.springframework.modulith.core.Types.SpringTypes.*;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.lang.Nullable;
import org.springframework.modulith.core.Types.JMoleculesTypes;
import org.springframework.modulith.core.Types.JavaTypes;
import org.springframework.modulith.core.Types.SpringTypes;
import org.springframework.modulith.core.Violations.Violation;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.function.SingletonSupplier;

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.Dependency;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaCodeUnit;
import com.tngtech.archunit.core.domain.JavaConstructor;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.core.domain.JavaMember;
import com.tngtech.archunit.core.domain.JavaMethod;
import com.tngtech.archunit.core.domain.SourceCodeLocation;

/**
 * An application module.
 *
 * @author Oliver Drotbohm
 */
public class ApplicationModule implements Comparable<ApplicationModule> {

	/**
	 * The base package of the {@link ApplicationModule}.
	 */
	private final JavaPackage basePackage;
	private final Classes classes;
	private final JavaPackages exclusions;

	private final ApplicationModuleInformation information;

	/**
	 * All {@link NamedInterfaces} of the {@link ApplicationModule} either declared explicitly via {@link NamedInterface}
	 * or implicitly.
	 */
	private final NamedInterfaces namedInterfaces;
	private final ApplicationModuleSource source;

	private final Supplier<Classes> springBeans;
	private final Supplier<Classes> aggregateRoots;
	private final Supplier<List<JavaClass>> valueTypes;
	private final Supplier<List<EventType>> publishedEvents;

	/**
	 * Creates a new {@link ApplicationModule} from the given {@link ApplicationModuleSource}.
	 *
	 * @param source must not be {@literal null}.
	 */
	ApplicationModule(ApplicationModuleSource source) {
		this(source, JavaPackages.NONE);
	}

	/**
	 * Creates a new {@link ApplicationModule} for the given base package and whether to use fully-qualified module names.
	 *
	 * @param source must not be {@literal null}.
	 * @param exclusions must not be {@literal null}.
	 */
	ApplicationModule(ApplicationModuleSource source, JavaPackages exclusions) {

		Assert.notNull(source, "Base package must not be null!");
		Assert.notNull(exclusions, "Exclusions must not be null!");

		JavaPackage basePackage = source.getModuleBasePackage();

		this.source = source;
		this.basePackage = basePackage;
		this.exclusions = exclusions;
		this.classes = basePackage.getClasses(exclusions);
		this.information = ApplicationModuleInformation.of(basePackage);
		this.namedInterfaces = isOpen()
				? NamedInterfaces.forOpen(basePackage)
				: NamedInterfaces.discoverNamedInterfaces(basePackage);

		this.springBeans = SingletonSupplier.of(() -> filterSpringBeans(classes));
		this.aggregateRoots = SingletonSupplier.of(() -> findAggregateRoots(classes));
		this.valueTypes = SingletonSupplier
				.of(() -> findArchitecturallyEvidentType(ArchitecturallyEvidentType::isValueObject));
		this.publishedEvents = SingletonSupplier.of(() -> findPublishedEvents());
	}

	/**
	 * Returns the module's base package.
	 *
	 * @return the basePackage
	 */
	public JavaPackage getBasePackage() {
		return basePackage;
	}

	/**
	 * Returns all {@link NamedInterfaces} exposed by the module.
	 *
	 * @return the namedInterfaces will never be {@literal null}.
	 */
	public NamedInterfaces getNamedInterfaces() {
		return namedInterfaces;
	}

	/**
	 * Returns the logical name of the module.
	 *
	 * @return will never be {@literal null} or empty.
	 * @deprecated since 1.3, use {@link #getIdentifier()} instead.
	 */
	@Deprecated
	public String getName() {
		return getIdentifier().toString();
	}

	/**
	 * Returns the logical identifier of the module.
	 *
	 * @return will never be {@literal null}.
	 * @since 1.3
	 */
	public ApplicationModuleIdentifier getIdentifier() {
		return source.getIdentifier();
	}

	/**
	 * Returns the name of the {@link ApplicationModule} for display purposes.
	 *
	 * @return will never be {@literal null} or empty.
	 */
	public String getDisplayName() {
		return information.getDisplayName()
				.orElseGet(() -> StringUtils.capitalize(basePackage.getLocalName()));
	}

	/**
	 * Returns {@link DeclaredDependencies} of the current {@link ApplicationModule}.
	 *
	 * @param modules must not be {@literal null}.
	 * @param type must not be {@literal null}.
	 * @return will never be {@literal null}.
	 * @deprecated since 1.3. Use {@link #getDirectDependencies(ApplicationModules, DependencyType...)} instead.
	 */
	@Deprecated
	public ApplicationModuleDependencies getDependencies(ApplicationModules modules, DependencyType... type) {
		return getDirectDependencies(modules, type);
	}

	/**
	 * Returns the direct {@link ApplicationModuleDependencies} of the current {@link ApplicationModule}.
	 *
	 * @param modules must not be {@literal null}.
	 * @param type must not be {@literal null}.
	 * @return will never be {@literal null}.
	 */
	public ApplicationModuleDependencies getDirectDependencies(ApplicationModules modules, DependencyType... type) {
		return getDependencies(modules, DependencyDepth.IMMEDIATE, type);
	}

	/**
	 * Returns the all {@link ApplicationModuleDependencies} (including transitive ones) of the current
	 * {@link ApplicationModule}.
	 *
	 * @param modules must not be {@literal null}.
	 * @param type must not be {@literal null}.
	 * @return will never be {@literal null}.
	 */
	public ApplicationModuleDependencies getAllDependencies(ApplicationModules modules, DependencyType... type) {
		return getDependencies(modules, DependencyDepth.ALL, type);
	}

	/**
	 * Returns all event types the current module exposes an event listener for.
	 *
	 * @param modules must not be {@literal null}.
	 * @return
	 */
	public List<JavaClass> getEventsListenedTo(ApplicationModules modules) {

		Assert.notNull(modules, "Modules must not be null!");

		return getAllModuleDependencies(modules) //
				.filter(it -> it.type == DependencyType.EVENT_LISTENER) //
				.map(QualifiedDependency::getTarget) //
				.toList();
	}

	/**
	 * Returns all {@link EventType}s published by the module.
	 *
	 * @return will never be {@literal null}.
	 */
	public List<EventType> getPublishedEvents() {
		return publishedEvents.get();
	}

	/**
	 * Returns all value types contained in the module.
	 *
	 * @return will never be {@literal null}.
	 */
	public List<JavaClass> getValueTypes() {
		return valueTypes.get();
	}

	/**
	 * Returns all types that are considered aggregate roots.
	 *
	 * @return will never be {@literal null}.
	 */
	public List<JavaClass> getAggregateRoots() {

		return aggregateRoots.get().stream() //
				.flatMap(this::resolveModuleSuperTypes) //
				.distinct() //
				.toList();
	}

	/**
	 * Returns all modules that contain types which the types of the current module depend on.
	 *
	 * @param modules must not be {@literal null}.
	 * @return
	 */
	public Stream<ApplicationModule> getBootstrapDependencies(ApplicationModules modules) {

		Assert.notNull(modules, "Modules must not be null!");

		return getBootstrapDependencies(modules, DependencyDepth.IMMEDIATE);
	}

	public Stream<ApplicationModule> getBootstrapDependencies(ApplicationModules modules, DependencyDepth depth) {

		Assert.notNull(modules, "Modules must not be null!");
		Assert.notNull(depth, "Dependency depth must not be null!");

		return streamBootstrapDependencies(modules, depth);
	}

	/**
	 * Returns all {@link JavaPackage} for the current module including the ones by its dependencies.
	 *
	 * @param modules must not be {@literal null}.
	 * @param depth must not be {@literal null}.
	 * @return
	 */
	public Stream<JavaPackage> getBootstrapBasePackages(ApplicationModules modules, DependencyDepth depth) {

		Assert.notNull(modules, "Modules must not be null!");
		Assert.notNull(depth, "Dependency depth must not be null!");

		var dependencies = streamBootstrapDependencies(modules, depth);

		return Stream.concat(Stream.of(this), dependencies) //
				.map(ApplicationModule::getBasePackage);
	}

	/**
	 * Returns all {@link SpringBean}s contained in the module.
	 *
	 * @return will never be {@literal null}.
	 */
	public List<SpringBean> getSpringBeans() {

		return getSpringBeansInternal().stream() //
				.map(it -> SpringBean.of(it, this)) //
				.toList();
	}

	/**
	 * Returns the {@link ArchitecturallyEvidentType} for the given type.
	 *
	 * @param type must not be {@literal null}.
	 * @return will never be {@literal null}.
	 * @throws IllegalArgumentException if the given type is not a module type.
	 */
	public ArchitecturallyEvidentType getArchitecturallyEvidentType(Class<?> type) {

		Assert.notNull(type, "Type must not be null!");

		return getType(type.getName())
				.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
				.orElseThrow(() -> new IllegalArgumentException("Couldn't find type %s in module %s!".formatted(
						FormattableType.of(type).getAbbreviatedFullName(this), getIdentifier())));
	}

	/**
	 * Returns whether the current module contains the given type.
	 *
	 * @param type must not be {@literal null}.
	 */
	public boolean contains(JavaClass type) {
		return contains(type.getName());
	}

	/**
	 * Returns whether the current module contains the given type.
	 *
	 * @param type must not be {@literal null}.
	 */
	public boolean contains(Class<?> type) {
		return contains(type.getName());
	}

	/**
	 * Returns the {@link JavaClass} for the given candidate simple or fully-qualified type name.
	 *
	 * @param candidate must not be {@literal null} or empty.
	 * @return will never be {@literal null}.
	 */
	public Optional<JavaClass> getType(String candidate) {

		Assert.hasText(candidate, "Candidate must not be null or emtpy!");

		return classes.stream()
				.filter(hasSimpleOrFullyQualifiedName(candidate))
				.findFirst();
	}

	/**
	 * Returns whether the given {@link JavaClass} is exposed by the current module, i.e. whether it's part of any of the
	 * module's named interfaces.
	 *
	 * @param type must not be {@literal null}.
	 * @return
	 */
	public boolean isExposed(JavaClass type) {

		Assert.notNull(type, "Type must not be null!");

		return namedInterfaces.stream().anyMatch(it -> it.contains(type));
	}

	public void verifyDependencies(ApplicationModules modules) {
		detectDependencies(modules).throwIfPresent();
	}

	public Violations detectDependencies(ApplicationModules modules) {

		return getAllModuleDependencies(modules) //
				.map(it -> it.isValidDependencyWithin(modules)) //
				.reduce(Violations.NONE, Violations::and);
	}

	/**
	 * Returns whether the module is considered a root one, i.e., it is an artificial one created for each base package
	 * configured.
	 *
	 * @return whether the module is considered a root one.
	 * @since 1.1
	 */
	public boolean isRootModule() {
		return false;
	}

	/**
	 * Returns whether the given module contains a type with the given simple or fully qualified name.
	 *
	 * @param candidate must not be {@literal null}.
	 * @since 1.3
	 */
	public boolean contains(String candidate) {

		var candidatePackageName = PackageName.ofType(candidate);

		return (candidatePackageName.isEmpty() || basePackage.getPackageName().contains(candidatePackageName))
				&& getType(candidate).isPresent();
	}

	public String toString(@Nullable ApplicationModules modules) {

		var builder = new StringBuilder("# ").append(getDisplayName());

		if (isOpen()) {
			builder.append(" (open)");
		}

		builder.append("\n");

		if (modules != null) {
			modules.getParentOf(this).ifPresent(it -> {
				builder.append("> Parent module: ").append(it.getIdentifier()).append("\n");
			});
		}

		builder.append("> Logical name: ").append(getIdentifier()).append('\n');
		builder.append("> Base package: ").append(basePackage.getName()).append('\n');

		builder.append("> Excluded packages: ");

		if (!exclusions.iterator().hasNext()) {
			builder.append("none").append('\n');
		} else {

			builder.append('\n');

			exclusions.stream().forEach(it -> {
				builder.append("  - ").append(it.getName()).append('\n');
			});
		}

		if (namedInterfaces.hasExplicitInterfaces()) {

			builder.append("> Named interfaces:\n");

			namedInterfaces.forEach(it -> builder.append("  + ") //
					.append(it.toString()) //
					.append('\n'));
		}

		if (modules != null) {

			List<ApplicationModule> dependencies = getBootstrapDependencies(modules).toList();

			builder.append("> Direct module dependencies: ");
			builder.append(dependencies.isEmpty()
					? "none"
					: dependencies.stream()
							.map(ApplicationModule::getIdentifier)
							.map(ApplicationModuleIdentifier::toString)
							.collect(Collectors.joining(", ")));
			builder.append('\n');
		}

		Classes beans = getSpringBeansInternal();

		if (beans.isEmpty()) {

			builder.append("> Spring beans: none\n");

		} else {

			builder.append("> Spring beans:\n");
			beans.forEach(it -> builder.append("  ") //
					.append(Classes.format(it, basePackage.getName(), isExposed(it)))//
					.append('\n'));
		}

		return builder.toString();
	}

	/**
	 * Returns whether the module has a base package with the given name.
	 *
	 * @param candidate must not be {@literal null} or empty.
	 * @return whether the module has a base package with the given name.
	 * @since 1.1
	 */
	boolean hasBasePackage(String candidate) {
		return basePackage.getName().equals(candidate);
	}

	Classes getSpringBeansInternal() {
		return springBeans.get();
	}

	/**
	 * Returns all declared module dependencies, either explicitly declared or defined as shared on the given
	 * {@link ApplicationModules} instance.
	 *
	 * @param modules must not be {@literal null}.
	 * @return
	 */
	DeclaredDependencies getDeclaredDependencies(ApplicationModules modules) {

		Assert.notNull(modules, "Modules must not be null!");

		var allowedDependencyNames = information.getDeclaredDependencies();

		if (DeclaredDependencies.isOpen(allowedDependencyNames)) {
			return DeclaredDependencies.open();
		}

		var explicitlyDeclaredModules = allowedDependencyNames.stream() //
				.map(it -> DeclaredDependency.of(it, this, modules));

		var sharedDependencies = modules.getSharedModules().stream()
				.map(DeclaredDependency::to);

		return Stream.concat(explicitlyDeclaredModules, sharedDependencies) //
				.distinct() //
				.collect(Collectors.collectingAndThen(Collectors.toList(), DeclaredDependencies::closed));
	}

	/**
	 * Returns whether the {@link ApplicationModule} contains the package with the given name, which means the given
	 * package is either the module's base package or a sub package of it.
	 *
	 * @param packageName must not be {@literal null} or empty.
	 * @return whether the {@link ApplicationModule} contains the package with the given name.
	 * @since 1.0.2
	 */
	boolean containsPackage(String packageName) {

		Assert.hasText(packageName, "Package name must not be null or empty!");

		var basePackageName = basePackage.getName();

		return packageName.equals(basePackageName)
				|| packageName.startsWith(basePackageName + ".");
	}

	/**
	 * Returns whether the module is considered open.
	 *
	 * @see org.springframework.modulith.ApplicationModule.Type
	 * @since 1.2
	 */
	boolean isOpen() {
		return information.isOpen();
	}

	/**
	 * Returns whether the given type is contained in any of the parent modules of the current one.
	 *
	 * @param type must not be {@literal null}.
	 * @param modules must not be {@literal null}.
	 * @since 1.3
	 */
	boolean containsTypeInAnyParent(JavaClass type, ApplicationModules modules) {

		Assert.notNull(type, "Type must not be null!");
		Assert.notNull(modules, "ApplicationModules must not be null!");

		return modules.getParentOf(this)
				.filter(it -> it.contains(type) || it.containsTypeInAnyParent(type, modules))
				.isPresent();
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.modulith.core.ApplicationModuleDependenciesAware#getDependencies(org.springframework.modulith.core.ApplicationModules, org.springframework.modulith.core.DependencyDepth, org.springframework.modulith.core.DependencyType[])
	 */
	public ApplicationModuleDependencies getDependencies(ApplicationModules modules, DependencyDepth depth,
			DependencyType... types) {

		Assert.notNull(modules, "ApplicationModules must not be null!");
		Assert.notNull(depth, "DependencyDepth must not be null!");
		Assert.notNull(types, "DependencyTypes must not be null!");

		return getAllModuleDependencies(modules) //
				.filter(it -> types.length == 0 ? true : Arrays.stream(types).anyMatch(it::hasType)) //
				.distinct() //
				.flatMap(it -> DefaultApplicationModuleDependency.of(it, modules)) //
				.distinct() //
				.flatMap(it -> resolveRecurseively(modules, it, depth, types)) //
				.collect(Collectors.collectingAndThen(Collectors.toList(), ApplicationModuleDependencies::of));
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		if (this == obj) {
			return true;
		}

		if (!(obj instanceof ApplicationModule that)) {
			return false;
		}

		return Objects.equals(this.source, that.source)
				&& Objects.equals(this.basePackage, that.basePackage) //
				&& Objects.equals(this.aggregateRoots, that.aggregateRoots) //
				&& Objects.equals(this.information, that.information) //
				&& Objects.equals(this.namedInterfaces, that.namedInterfaces) //
				&& Objects.equals(this.publishedEvents, that.publishedEvents) //
				&& Objects.equals(this.springBeans, that.springBeans) //
				&& Objects.equals(this.valueTypes, that.valueTypes);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return Objects.hash(source, basePackage, aggregateRoots, information, namedInterfaces, publishedEvents, springBeans,
				valueTypes);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return toString(null);
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(ApplicationModule o) {
		return getBasePackage().compareTo(o.getBasePackage());
	}

	private List<EventType> findPublishedEvents() {

		DescribedPredicate<JavaClass> isEvent = implement(JMoleculesTypes.DOMAIN_EVENT) //
				.or(isAnnotatedWith(JMoleculesTypes.AT_DOMAIN_EVENT));

		return classes.that(isEvent).stream() //
				.map(EventType::new).toList();
	}

	/**
	 * Returns a {@link Stream} of all super types of the given one that are declared in the same module as well as the
	 * type itself.
	 *
	 * @param type must not be {@literal null}.
	 * @return
	 */
	private Stream<JavaClass> resolveModuleSuperTypes(JavaClass type) {

		Assert.notNull(type, "Type must not be null!");

		return Stream.concat(//
				type.getAllRawSuperclasses().stream().filter(this::contains), //
				Stream.of(type));
	}

	private Stream<QualifiedDependency> getAllModuleDependencies(ApplicationModules modules) {

		return classes.stream() //
				.flatMap(it -> getModuleDependenciesOf(it, modules));
	}

	private Stream<ApplicationModule> streamBootstrapDependencies(ApplicationModules modules, DependencyDepth depth) {

		return switch (depth) {
			case NONE -> Stream.empty();
			case IMMEDIATE -> getDirectModuleBootstrapDependencies(modules);
			case ALL -> getAllBootstrapDependencies(modules);
			default -> getAllBootstrapDependencies(modules);
		};
	}

	private Stream<ApplicationModule> getAllBootstrapDependencies(ApplicationModules modules) {

		return getDirectModuleBootstrapDependencies(modules) //
				.flatMap(it -> Stream.concat(Stream.of(it), it.streamBootstrapDependencies(modules, DependencyDepth.ALL))) //
				.distinct();
	}

	private Stream<ApplicationModule> getDirectModuleBootstrapDependencies(ApplicationModules modules) {

		var beans = getSpringBeansInternal();

		return beans.stream() //
				.map(it -> ArchitecturallyEvidentType.of(it, beans)) //
				.flatMap(it -> QualifiedDependency.fromType(it)) //
				.filter(it -> isDependencyToOtherModule(it.target, modules)) //
				.filter(it -> it.hasType(DependencyType.USES_COMPONENT)) //
				.map(it -> modules.getModuleByType(it.target)) //
				.distinct() //
				.flatMap(it -> it.map(Stream::of).orElseGet(Stream::empty));
	}

	/**
	 * Returns the current module's immediate parent module, if present.
	 *
	 * @param modules must not be {@literal null}.
	 * @return will never be {@literal null}.
	 * @since 1.3
	 */
	Optional<ApplicationModule> getParentModule(ApplicationModules modules) {

		Assert.notNull(modules, "ApplicationModules must not be null!");

		var byPackageDepth = comparing(ApplicationModule::getBasePackage, JavaPackage.reverse());

		return modules.stream()
				.filter(it -> basePackage.isSubPackageOf(it.getBasePackage()))
				.sorted(byPackageDepth)
				.findFirst();
	}

	/**
	 * Returns the {@link ApplicationModule}s directly nested inside the current one.
	 *
	 * @param modules must not be {@literal null}.
	 * @return will never be {@literal null}.
	 * @since 1.3
	 */
	Collection<ApplicationModule> getDirectlyNestedModules(ApplicationModules modules) {

		Assert.notNull(modules, "ApplicationModules must not be null!");

		return doGetNestedModules(modules, false);
	}

	/**
	 * Returns all of the current {@link ApplicationModule}'s nested {@link ApplicationModule}s including ones contained
	 * in nested modules in turn.
	 *
	 * @param modules must not be {@literal null}.
	 * @return will never be {@literal null}.
	 * @since 1.3
	 */
	Collection<ApplicationModule> getNestedModules(ApplicationModules modules) {

		Assert.notNull(modules, "ApplicationModules must not be null!");

		return doGetNestedModules(modules, true);
	}

	/**
	 * @return the classes
	 */
	Classes getClasses() {
		return classes;
	}

	private String getQualifiedName(NamedInterface namedInterface) {
		return namedInterface.getQualifiedName(getIdentifier());
	}

	private Collection<ApplicationModule> doGetNestedModules(ApplicationModules modules, boolean recursive) {

		var result = modules.stream()
				.filter(it -> it.getParentModule(modules).filter(this::equals).isPresent());

		if (recursive) {
			result = result.flatMap(it -> Stream.concat(Stream.of(it), it.getNestedModules(modules).stream()));
		}

		return result.toList();
	}

	private List<JavaClass> findArchitecturallyEvidentType(Predicate<ArchitecturallyEvidentType> selector) {

		var springBeansInternal = getSpringBeansInternal();

		return classes.stream()
				.map(it -> ArchitecturallyEvidentType.of(it, springBeansInternal))
				.filter(selector)
				.map(ArchitecturallyEvidentType::getType)
				.toList();
	}

	private Stream<QualifiedDependency> getModuleDependenciesOf(JavaClass type, ApplicationModules modules) {

		var evidentType = ArchitecturallyEvidentType.of(type, getSpringBeansInternal());

		var injections = QualifiedDependency.fromType(evidentType) //
				.filter(it -> isDependencyToOtherModule(it.getTarget(), modules)); //

		var directDependencies = type.getDirectDependenciesFromSelf().stream() //
				.filter(it -> JavaTypes.IS_NOT_CORE_JAVA_TYPE.test(it.getTargetClass())) //
				.filter(it -> isDependencyToOtherModule(it.getTargetClass(), modules)) //
				.map(QualifiedDependency::new);

		return Stream.concat(injections, directDependencies).distinct();
	}

	private boolean isDependencyToOtherModule(JavaClass dependency, ApplicationModules modules) {
		return modules.contains(dependency) && !contains(dependency);
	}

	private Classes findAggregateRoots(Classes source) {

		return source.stream() //
				.map(it -> ArchitecturallyEvidentType.of(it, getSpringBeansInternal()))
				.filter(ArchitecturallyEvidentType::isAggregateRoot) //
				.map(ArchitecturallyEvidentType::getType) //
				.collect(Classes.toClasses());
	}

	private Stream<ApplicationModuleDependency> resolveRecurseively(ApplicationModules modules,
			DefaultApplicationModuleDependency dependency, DependencyDepth depth, DependencyType... type) {

		var tail = depth == DependencyDepth.ALL
				? dependency.getTargetModule()
						.getDependencies(modules, depth, type)
						.stream()
						.distinct()
				: Stream.<ApplicationModuleDependency> empty();

		return Stream.concat(Stream.of(dependency), tail);
	}

	private static Classes filterSpringBeans(Classes source) {

		Map<Boolean, List<JavaClass>> collect = source.that(isConfiguration()).stream() //
				.flatMap(it -> it.getMethods().stream()) //
				.filter(SpringTypes::isAtBeanMethod) //
				.map(JavaMethod::getRawReturnType) //
				.collect(Collectors.groupingBy(it -> source.contains(it)));

		Classes repositories = source.that(isSpringDataRepository());
		Classes coreComponents = source.that(not(INTERFACES).and(isComponent()));
		Classes configurationProperties = source.that(isConfigurationProperties());
		Classes jsr303Validator = source.that(isJsr303Validator());

		return coreComponents //
				.and(repositories) //
				.and(configurationProperties) //
				.and(jsr303Validator) //
				.and(collect.getOrDefault(true, Collections.emptyList())) //
				.and(collect.getOrDefault(false, Collections.emptyList()));
	}

	private static Predicate<JavaClass> hasSimpleOrFullyQualifiedName(String candidate) {
		return it -> it.getSimpleName().equals(candidate) || it.getFullName().equals(candidate);
	}

	static class DeclaredDependency {

		private static final String INVALID_EXPLICIT_MODULE_DEPENDENCY = "Invalid explicit module dependency in %s! No module found with name '%s'.";
		private static final String INVALID_NAMED_INTERFACE_DECLARATION = "No named interface named '%s' found! Original dependency declaration: %s -> %s.";
		private static final String WILDCARD = "*";

		private final ApplicationModule target;
		private final @Nullable NamedInterface namedInterface;

		/**
		 * Creates a new {@link DeclaredDependency} for the given {@link ApplicationModule} and {@link NamedInterface}.
		 *
		 * @param target must not be {@literal null}.
		 * @param namedInterface can be {@literal null}.
		 */
		private DeclaredDependency(ApplicationModule target, @Nullable NamedInterface namedInterface) {

			Assert.notNull(target, "Target ApplicationModule must not be null!");

			this.target = target;
			this.namedInterface = namedInterface;
		}

		/**
		 * Creates an {@link DeclaredDependency} to the module and optionally named interface defined by the given
		 * identifier.
		 *
		 * @param identifier must not be {@literal null} or empty. Follows the
		 *          {@code ${moduleName}(::${namedInterfaceName})} pattern.
		 * @param source the source module of the dependency, must not be {@literal null}.
		 * @param modules must not be {@literal null}.
		 * @return will never be {@literal null}.
		 * @throws IllegalArgumentException in case the given identifier is invalid, i.e. does not refer to an existing
		 *           module or named interface.
		 */
		public static DeclaredDependency of(String identifier, ApplicationModule source,
				ApplicationModules modules) {

			Assert.hasText(identifier, "Module dependency identifier must not be null or empty!");

			var segments = identifier.split("::");
			var targetModuleName = segments[0].trim();
			var namedInterfaceName = segments.length > 1 ? segments[1].trim() : null;

			var target = modules.getModuleByName(targetModuleName)
					.orElseThrow(() -> new IllegalArgumentException(
							INVALID_EXPLICIT_MODULE_DEPENDENCY.formatted(source.getIdentifier(), targetModuleName)));

			if (WILDCARD.equals(namedInterfaceName)) {
				return new DeclaredDependency(target, null);
			}

			var namedInterfaces = target.getNamedInterfaces();
			var namedInterface = namedInterfaceName == null
					? namedInterfaces.getUnnamedInterface()
					: namedInterfaces.getByName(namedInterfaceName)
							.orElseThrow(() -> new IllegalArgumentException(
									INVALID_NAMED_INTERFACE_DECLARATION.formatted(namedInterfaceName, source.getIdentifier(),
											identifier)));

			return new DeclaredDependency(target, namedInterface);
		}

		/**
		 * Creates a new {@link DeclaredDependency} to the unnamed interface of the given {@link ApplicationModule}.
		 *
		 * @param module must not be {@literal null}.
		 * @return
		 */
		public static DeclaredDependency to(ApplicationModule module) {

			Assert.notNull(module, "ApplicationModule must not be null!");

			return new DeclaredDependency(module, module.getNamedInterfaces().getUnnamedInterface());
		}

		/**
		 * Returns whether the {@link DeclaredDependency} contains the given {@link JavaClass}.
		 *
		 * @param type must not be {@literal null}.
		 * @return
		 */
		public boolean contains(JavaClass type) {

			Assert.notNull(type, "Type must not be null!");

			return namedInterface == null
					? target.getNamedInterfaces().containsInExplicitInterface(type)
					: namedInterface.contains(type);
		}

		/**
		 * Returns whether the {@link DeclaredDependency} contains the given {@link Class}.
		 *
		 * @param type must not be {@literal null}.
		 * @return
		 */
		boolean contains(Class<?> type) {

			Assert.notNull(type, "Type must not be null!");

			return namedInterface == null
					? target.getNamedInterfaces().containsInExplicitInterface(type)
					: namedInterface.contains(type);
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {

			var result = target.getIdentifier().toString();

			if (namedInterface == null) {
				return result + " :: " + WILDCARD;
			}

			if (namedInterface.isUnnamed()) {
				return result;
			}

			return result + " :: " + namedInterface.getName();
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof DeclaredDependency that)) {
				return false;
			}

			return Objects.equals(this.target, that.target) //
					&& Objects.equals(this.namedInterface, that.namedInterface);
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return Objects.hash(target, namedInterface);
		}
	}

	/**
	 * A collection wrapper for {@link DeclaredDependency} instances.
	 *
	 * @author Oliver Drotbohm
	 */
	static class DeclaredDependencies {

		private static final String OPEN_TOKEN = "¯\\_(ツ)_/¯";

		private final List<DeclaredDependency> dependencies;
		private final boolean closed;

		static boolean isOpen(List<String> declaredDependencies) {
			return declaredDependencies.size() == 1 && declaredDependencies.get(0).equals(OPEN_TOKEN);
		}

		public static DeclaredDependencies open() {
			return new DeclaredDependencies(Collections.emptyList(), false);
		}

		public static DeclaredDependencies closed(List<DeclaredDependency> dependencies) {
			return new DeclaredDependencies(dependencies, true);
		}

		/**
		 * Creates a new {@link DeclaredDependencies} for the given {@link List} of {@link DeclaredDependency}.
		 *
		 * @param dependencies must not be {@literal null}.
		 */
		private DeclaredDependencies(List<DeclaredDependency> dependencies, boolean closed) {

			Assert.notNull(dependencies, "Dependencies must not be null!");

			this.dependencies = dependencies;
			this.closed = closed;
		}

		/**
		 * Returns whether the given {@link JavaClass} is a valid dependency.
		 *
		 * @param type must not be {@literal null}.
		 * @return
		 */
		public boolean isAllowedDependency(JavaClass type) {
			return isAllowedDependency(it -> it.contains(type));
		}

		boolean isAllowedDependency(Class<?> type) {
			return isAllowedDependency(it -> it.contains(type));
		}

		private boolean isAllowedDependency(Predicate<DeclaredDependency> predicate) {

			Assert.notNull(predicate, "Predicate must not be null!");

			return closed ? !dependencies.isEmpty() && contains(predicate) : dependencies.isEmpty() || contains(predicate);
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {

			return dependencies.isEmpty() //
					? "none" //
					: dependencies.stream().map(DeclaredDependency::toString).collect(Collectors.joining(", "));
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof DeclaredDependencies that)) {
				return false;
			}

			return Objects.equals(this.dependencies, that.dependencies);
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return Objects.hash(dependencies);
		}

		/**
		 * Returns whether any of the dependencies contains the given {@link JavaClass}.
		 *
		 * @param type must not be {@literal null}.
		 */
		private boolean contains(Predicate<DeclaredDependency> condition) {

			Assert.notNull(condition, "Condition must not be null!");

			return dependencies.stream().anyMatch(condition);
		}
	}

	static class QualifiedDependency {

		private static final List<String> INJECTION_TYPES = Arrays.asList(AT_AUTOWIRED, AT_RESOURCE, AT_INJECT);

		private static final String INVALID_SUB_MODULE_REFERENCE = "Invalid sub-module reference from module '%s' to module '%s' (via %s -> %s)!";
		private static final String INTERNAL_REFERENCE = "Module '%s' depends on non-exposed type %s within module '%s'!";

		private final JavaClass source, target;
		private final String description;
		private final DependencyType type;

		/**
		 * Creates a new {@link QualifiedDependency} from the given source and target {@link JavaClass}, description and
		 * {@link DependencyType}.
		 *
		 * @param source must not be {@literal null}.
		 * @param target must not be {@literal null}.
		 * @param description must not be {@literal null}.
		 * @param type must not be {@literal null}.
		 */
		public QualifiedDependency(JavaClass source, JavaClass target, String description, DependencyType type) {

			Assert.notNull(source, "Source JavaClass must not be null!");
			Assert.notNull(target, "Target JavaClass must not be null!");
			Assert.notNull(description, "Description must not be null!");
			Assert.notNull(type, "DependencyType must not be null!");

			this.source = source;
			this.target = target;
			this.description = description;
			this.type = type;
		}

		QualifiedDependency(Dependency dependency) {
			this(dependency.getOriginClass(), //
					dependency.getTargetClass(), //
					dependency.getDescription(), //
					DependencyType.forDependency(dependency));
		}

		static Stream<QualifiedDependency> fromCodeUnitParameter(JavaCodeUnit codeUnit, JavaClass parameter) {

			if (JavaTypes.IS_CORE_JAVA_TYPE.test(parameter)) {
				return Stream.empty();
			}

			var description = createDescription(codeUnit, parameter, "parameter");
			var type = DependencyType.forCodeUnit(codeUnit) //
					.defaultOr(() -> DependencyType.forParameter(parameter));

			return Stream.of(new QualifiedDependency(codeUnit.getOwner(), parameter, description, type));
		}

		static Stream<QualifiedDependency> fromCodeUnitReturnType(JavaCodeUnit codeUnit) {

			var returnType = codeUnit.getRawReturnType();

			if (JavaTypes.IS_CORE_JAVA_TYPE.test(returnType)) {
				return Stream.empty();
			}

			var description = createDescription(codeUnit, codeUnit.getRawReturnType(), "return type");

			return Stream.of(new QualifiedDependency(codeUnit.getOwner(), codeUnit.getRawReturnType(), description,
					DependencyType.DEFAULT));
		}

		static Stream<QualifiedDependency> fromType(ArchitecturallyEvidentType type) {

			var source = type.getType();

			return Stream.concat(Stream.concat(fromConstructorOf(type), fromMethodsOf(source)), fromFieldsOf(source));
		}

		static Stream<QualifiedDependency> allFrom(JavaCodeUnit codeUnit) {

			var parameterDependencies = codeUnit.getRawParameterTypes()//
					.stream() //
					.filter(JavaTypes.IS_NOT_CORE_JAVA_TYPE) //
					.flatMap(it -> fromCodeUnitParameter(codeUnit, it));

			var returnType = fromCodeUnitReturnType(codeUnit);

			return Stream.concat(parameterDependencies, returnType);
		}

		/**
		 * Returns the source {@link JavaClass}.
		 *
		 * @return the source will never be {@literal null}.
		 */
		public JavaClass getSource() {
			return source;
		}

		/**
		 * Returns the target {@link JavaClass}.
		 *
		 * @return the target must not be {@literal null}.
		 */
		public JavaClass getTarget() {
			return target;
		}

		/**
		 * Returns whether the {@link QualifiedDependency} has the given {@link DependencyType}.
		 *
		 * @param type must not be {@literal null}.
		 * @return
		 */
		boolean hasType(DependencyType type) {
			return this.type.equals(type);
		}

		Violations isValidDependencyWithin(ApplicationModules modules) {

			var originModule = getExistingModuleOf(source, modules);
			var targetModule = getExistingModuleOf(target, modules);
			var violations = Violations.NONE;

			if (originModule.equals(targetModule)) {
				return violations;
			}

			var declaredDependencies = originModule.getDeclaredDependencies(modules);

			// Check explicitly defined allowed targets
			if (!declaredDependencies.isAllowedDependency(target)) {

				var targetNamedInterfaces = targetModule.getNamedInterfaces()
						.getNamedInterfacesContaining(target)
						.filter(NamedInterface::isNamed)
						.toList();

				var targetString = targetNamedInterfaces.isEmpty()
						? "module '%s'".formatted(targetModule.getIdentifier())
						: "named interface(s) '%s'".formatted(
								targetNamedInterfaces.stream()
										.map(targetModule::getQualifiedName)
										.collect(Collectors.joining(", ")));

				var message = "Module '%s' depends on %s via %s -> %s. Allowed targets: %s." //
						.formatted(originModule.getIdentifier(), targetString, source.getName(), target.getName(),
								declaredDependencies.toString());

				return violations.and(new Violation(message));
			}

			// No explicitly allowed dependencies - check for general access

			if (targetModule.isOpen()) {
				return violations;
			}

			if (originModule.containsTypeInAnyParent(target, modules)) {
				return violations;
			}

			if (!targetModule.isExposed(target)) {

				var violationText = INTERNAL_REFERENCE
						.formatted(originModule.getIdentifier(), target.getName(), targetModule.getIdentifier());

				return violations.and(new Violation(violationText + lineSeparator() + description));
			}

			// Parent child relationships

			if (!haveSameParentOrDirectParentRelationship(originModule, targetModule, modules)) {

				var violationText = INVALID_SUB_MODULE_REFERENCE
						.formatted(originModule.getIdentifier(), targetModule.getIdentifier(),
								FormattableType.of(source).getAbbreviatedFullName(originModule),
								FormattableType.of(target).getAbbreviatedFullName(targetModule));

				return violations.and(new Violation(violationText));
			}

			return violations;
		}

		ApplicationModule getExistingModuleOf(JavaClass javaClass, ApplicationModules modules) {

			return modules.getModuleByType(javaClass).orElseThrow(() -> new IllegalStateException(
					String.format("Source/Target of a %s should always be within a module, but %s is not",
							getClass().getSimpleName(), javaClass.getName())));
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return type.format(FormattableType.of(source), FormattableType.of(target));
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof QualifiedDependency other)) {
				return false;
			}

			return Objects.equals(this.source, other.source) //
					&& Objects.equals(this.target, other.target) //
					&& Objects.equals(this.description, other.description) //
					&& Objects.equals(this.type, other.type); //
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return Objects.hash(source, target, description, type);
		}

		private static Stream<QualifiedDependency> fromConstructorOf(ArchitecturallyEvidentType source) {

			var type = source.getType();
			var constructors = type.getConstructors();

			return constructors.stream() //
					.filter(it -> constructors.size() == 1 || isInjectionPoint(it)) //
					.flatMap(it -> it.getRawParameterTypes().stream() //
							.filter(Predicate.not(JavaTypes.IS_CORE_JAVA_TYPE))
							.map(parameter -> {
								return source.isInjectable() && !source.isConfigurationProperties()
										? new InjectionDependency(it, parameter)
										: new QualifiedDependency(type, parameter, createDescription(it, parameter, "parameter"),
												DependencyType.DEFAULT);
							}));
		}

		private static Stream<QualifiedDependency> fromFieldsOf(JavaClass source) {

			return source.getAllFields().stream() //
					.filter(it -> JavaTypes.IS_NOT_CORE_JAVA_TYPE.test(it.getRawType())) //
					.filter(QualifiedDependency::isInjectionPoint) //
					.map(field -> new InjectionDependency(field, field.getRawType()));
		}

		private static Stream<QualifiedDependency> fromMethodsOf(JavaClass source) {

			if (JavaTypes.IS_CORE_JAVA_TYPE.test(source)) {
				return Stream.empty();
			}

			var methods = source.getAllMethods().stream() //
					.filter(it -> !it.getOwner().isEquivalentTo(Object.class)) //
					.collect(Collectors.toSet());

			if (methods.isEmpty()) {
				return Stream.empty();
			}

			var returnTypes = methods.stream() //
					.filter(it -> !it.getRawReturnType().isPrimitive()) //
					.filter(it -> JavaTypes.IS_NOT_CORE_JAVA_TYPE.test(it.getRawReturnType())) //
					.flatMap(it -> fromCodeUnitReturnType(it));

			var injectionMethods = methods.stream() //
					.filter(QualifiedDependency::isInjectionPoint) //
					.collect(Collectors.toSet());

			var methodInjections = injectionMethods.stream() //
					.flatMap(it -> it.getRawParameterTypes().stream() //
							.filter(JavaTypes.IS_NOT_CORE_JAVA_TYPE) //
							.map(parameter -> new InjectionDependency(it, parameter)));

			var otherMethods = methods.stream() //
					.filter(it -> !injectionMethods.contains(it)) //
					.flatMap(it -> it.getRawParameterTypes().stream() //
							.filter(JavaTypes.IS_NOT_CORE_JAVA_TYPE) //
							.flatMap(parameter -> fromCodeUnitParameter(it, parameter)));

			return Stream.concat(Stream.concat(methodInjections, otherMethods), returnTypes);
		}

		private static String createDescription(JavaMember codeUnit, JavaClass declaringElement,
				String declarationDescription) {

			var type = declaringElement.getSimpleName();

			var codeUnitDescription = JavaConstructor.class.isInstance(codeUnit) //
					? String.format("%s", declaringElement.getSimpleName()) //
					: String.format("%s.%s", declaringElement.getSimpleName(), codeUnit.getName());

			if (JavaCodeUnit.class.isInstance(codeUnit)) {
				codeUnitDescription = String.format("%s(%s)", codeUnitDescription,
						JavaCodeUnit.class.cast(codeUnit).getRawParameterTypes().stream() //
								.map(JavaClass::getSimpleName) //
								.collect(Collectors.joining(", ")));
			}

			var annotations = codeUnit.getAnnotations().stream() //
					.filter(it -> INJECTION_TYPES.contains(it.getRawType().getName())) //
					.map(it -> "@" + it.getRawType().getSimpleName()) //
					.collect(Collectors.joining(" ", "", " "));

			annotations = StringUtils.hasText(annotations) ? annotations : "";

			var declaration = declarationDescription + " " + annotations + codeUnitDescription;
			var location = SourceCodeLocation.of(codeUnit.getOwner(), 0).toString();

			return String.format("%s declares %s in %s", type, declaration, location);
		}

		private static boolean isInjectionPoint(JavaMember unit) {
			return INJECTION_TYPES.stream().anyMatch(type -> unit.isAnnotatedWith(type));
		}

		private static boolean haveSameParentOrDirectParentRelationship(ApplicationModule source, ApplicationModule target,
				ApplicationModules modules) {

			var sourceParent = modules.getParentOf(source);
			var targetParent = modules.getParentOf(target);

			// Top-level modules
			return targetParent.isEmpty()

					// One is parent of the other
					|| hasValue(sourceParent, target)
					|| hasValue(targetParent, source)

					// Same immediate parent
					|| sourceParent.flatMap(it -> targetParent.filter(it::equals)).isPresent();
		}

		private static <T> boolean hasValue(Optional<T> optional, T expected) {
			return optional.filter(expected::equals).isPresent();
		}
	}

	private static class InjectionDependency extends QualifiedDependency {

		private final JavaMember source;
		private final boolean isConfigurationClass;

		/**
		 * Creates a new {@link InjectionDependency} for the given source, target and originating member.
		 *
		 * @param source must not be {@literal null}.
		 * @param target must not be {@literal null}.
		 */
		public InjectionDependency(JavaMember source, JavaClass target) {

			super(source.getOwner(), target,
					QualifiedDependency.createDescription(source, source.getOwner(), getDescriptionFor(source)),
					DependencyType.USES_COMPONENT);

			Assert.notNull(source, "Originating member must not be null!");

			this.source = source;
			this.isConfigurationClass = isConfiguration().test(source.getOwner());
		}

		/*
		 * (non-Javadoc)
		 * @see org.springframework.modulith.model.Module.ModuleDependency#isValidDependencyWithin(org.springframework.modulith.model.Modules)
		 */
		@Override
		Violations isValidDependencyWithin(ApplicationModules modules) {

			Violations violations = super.isValidDependencyWithin(modules);

			if (JavaField.class.isInstance(source) && !isConfigurationClass) {

				ApplicationModule module = getExistingModuleOf(source.getOwner(), modules);

				violations = violations.and(new Violation(
						String.format("Module %s uses field injection in %s. Prefer constructor injection instead!",
								module.getDisplayName(), source.getFullName())));
			}

			return violations;
		}

		private static String getDescriptionFor(JavaMember member) {

			if (JavaConstructor.class.isInstance(member)) {
				return "constructor";
			} else if (JavaMethod.class.isInstance(member)) {
				return "injection method";
			} else if (JavaField.class.isInstance(member)) {
				return "injected field";
			}

			throw new IllegalArgumentException(String.format("Invalid member type %s!", member.toString()));
		}
	}

	private static class DefaultApplicationModuleDependency
			implements ApplicationModuleDependency {

		private final QualifiedDependency dependency;
		private final ApplicationModule target;

		/**
		 * Creates a new {@link ApplicationModuleDependency} for the given {@link QualifiedDependency} and
		 * {@link ApplicationModules}.
		 *
		 * @param dependency must not be {@literal null}.
		 * @param target must not be {@literal null}.
		 */
		private DefaultApplicationModuleDependency(QualifiedDependency dependency, ApplicationModule target) {

			Assert.notNull(dependency, "QualifiedDependency must not be null!");
			Assert.notNull(target, "Target ApplicationModule must not be null!");

			this.dependency = dependency;
			this.target = target;
		}

		/**
		 * Creates a new {@link Stream} of {@link ApplicationModuleDependency} for the given {@link QualifiedDependency} and
		 * {@link ApplicationModules}.
		 *
		 * @param dependency must not be {@literal null}.
		 * @param modules must not be {@literal null}.
		 */
		static Stream<DefaultApplicationModuleDependency> of(QualifiedDependency dependency, ApplicationModules modules) {

			return modules.getModuleByType(dependency.getTarget()).stream()
					.map(it -> new DefaultApplicationModuleDependency(dependency, it));
		}

		/*
		 * (non-Javadoc)
		 * @see org.springframework.modulith.model.MaterializedDependency#getSourceType()
		 */
		@Override
		public JavaClass getSourceType() {
			return dependency.source;
		}

		/*
		 * (non-Javadoc)
		 * @see org.springframework.modulith.model.MaterializedDependency#getTargetType()
		 */
		@Override
		public JavaClass getTargetType() {
			return dependency.target;
		}

		/*
		 * (non-Javadoc)
		 * @see org.springframework.modulith.model.MaterializedDependency#getDependencyType()
		 */
		@Override
		public DependencyType getDependencyType() {
			return dependency.type;
		}

		/*
		 * (non-Javadoc)
		 * @see org.springframework.modulith.model.MaterializedDependency#getTargetModule()
		 */
		@Override
		public ApplicationModule getTargetModule() {
			return target;
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return "DefaultApplicationModuleDependency [dependency=" + dependency + ", target=" + target + "]";
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {

			if (this == obj) {
				return true;
			}

			if (!(obj instanceof DefaultApplicationModuleDependency other)) {
				return false;
			}

			return Objects.equals(this.target, other.target) //
					&& Objects.equals(this.dependency, other.dependency);
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.Object#hashCode()
		 */
		@Override
		public int hashCode() {
			return Objects.hash(target, dependency);
		}
	}
}
