From e7b531e06103baac2f9ad8b185a9482000310387 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 6 Dec 2023 14:17:43 +0100 Subject: [PATCH] GH-406 - Ignore Spring AOT generated types in architecture model. We now explicitly exclude classes generated by Spring AOT in the architectural model. For technical reasons, they might introduce dependencies to application components considered module internals otherwise. Also, proxies generated do not need to be considered either. --- .../modulith/core/ApplicationModules.java | 6 ++++- .../aot/Some$$SpringCGLIB$$Proxy.java | 24 +++++++++++++++++++ .../com/acme/myproject/aot/Spring__Aot.java | 24 +++++++++++++++++++ .../com/acme/myproject/aot/package-info.java | 1 + .../ApplicationModulesIntegrationTest.java | 11 +++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Some$$SpringCGLIB$$Proxy.java create mode 100644 spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Spring__Aot.java create mode 100644 spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/package-info.java diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java index bf4d3a37..54d4215e 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java @@ -17,6 +17,7 @@ package org.springframework.modulith.core; import static com.tngtech.archunit.base.DescribedPredicate.*; import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*; +import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.*; import static java.util.stream.Collectors.*; import java.util.*; @@ -43,6 +44,7 @@ import org.springframework.util.function.SingletonSupplier; import com.tngtech.archunit.base.DescribedPredicate; import com.tngtech.archunit.core.domain.JavaClass; import com.tngtech.archunit.core.domain.JavaClasses; +import com.tngtech.archunit.core.domain.properties.HasName; import com.tngtech.archunit.core.importer.ClassFileImporter; import com.tngtech.archunit.core.importer.ImportOption; import com.tngtech.archunit.lang.EvaluationResult; @@ -62,6 +64,8 @@ public class ApplicationModules implements Iterable { private static final ImportOption IMPORT_OPTION = new ImportOption.DoNotIncludeTests(); private static final boolean JGRAPHT_PRESENT = ClassUtils.isPresent("org.jgrapht.Graph", ApplicationModules.class.getClassLoader()); + private static final DescribedPredicate IS_AOT_TYPE = nameContaining("__") + .or(nameContaining("$$SpringCGLIB$$")); static { @@ -97,7 +101,7 @@ public class ApplicationModules implements Iterable { this.allClasses = new ClassFileImporter() // .withImportOption(option) // .importPackages(packages) // - .that(not(ignored)); + .that(not(ignored.or(IS_AOT_TYPE))); Classes classes = Classes.of(allClasses); diff --git a/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Some$$SpringCGLIB$$Proxy.java b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Some$$SpringCGLIB$$Proxy.java new file mode 100644 index 00000000..37fff7e6 --- /dev/null +++ b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Some$$SpringCGLIB$$Proxy.java @@ -0,0 +1,24 @@ +/* + * Copyright 2023 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 com.acme.myproject.aot; + +/** + * + * @author Oliver Drotbohm + */ +public class Some$$SpringCGLIB$$Proxy { + +} diff --git a/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Spring__Aot.java b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Spring__Aot.java new file mode 100644 index 00000000..7ab49fd5 --- /dev/null +++ b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/Spring__Aot.java @@ -0,0 +1,24 @@ +/* + * Copyright 2023 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 com.acme.myproject.aot; + +/** + * + * @author Oliver Drotbohm + */ +public class Spring__Aot { + +} diff --git a/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/package-info.java b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/package-info.java new file mode 100644 index 00000000..ab2626ed --- /dev/null +++ b/spring-modulith-integration-test/src/main/java/com/acme/myproject/aot/package-info.java @@ -0,0 +1 @@ +package com.acme.myproject.aot; diff --git a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java index a0e85d42..e0765dd1 100644 --- a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java +++ b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java @@ -30,6 +30,8 @@ import java.util.stream.Stream; import org.junit.jupiter.api.Test; import com.acme.myproject.Application; +import com.acme.myproject.aot.Some$$SpringCGLIB$$Proxy; +import com.acme.myproject.aot.Spring__Aot; import com.acme.myproject.complex.internal.FirstTypeBasedPort; import com.acme.myproject.complex.internal.SecondTypeBasePort; import com.acme.myproject.moduleA.ServiceComponentA; @@ -185,6 +187,15 @@ class ApplicationModulesIntegrationTest { assertThat(third.getDeclaredDependencies(modules).isAllowedDependency(Fourth.class)).isTrue(); } + @Test // GH-406 + void excludesSpringAOTGeneratedTypes() { + + assertThat(modules.getModuleByName("aot")).hasValueSatisfying(it -> { + assertThat(it.contains(Spring__Aot.class)).isFalse(); + assertThat(it.contains(Some$$SpringCGLIB$$Proxy.class)).isFalse(); + }); + } + private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class... types) { Stream.of(types).forEach(type -> {