GH-408 - 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.
This commit is contained in:
Oliver Drotbohm
2023-12-06 14:17:43 +01:00
parent 5944f0e5ea
commit 4c60cdc705
5 changed files with 65 additions and 1 deletions

View File

@@ -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.*;
@@ -45,6 +46,7 @@ import org.springframework.util.ClassUtils;
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;
@@ -64,6 +66,8 @@ public class ApplicationModules implements Iterable<ApplicationModule> {
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<HasName> IS_AOT_TYPE = nameContaining("__")
.or(nameContaining("$$SpringCGLIB$$"));
static {
@@ -98,7 +102,7 @@ public class ApplicationModules implements Iterable<ApplicationModule> {
this.allClasses = new ClassFileImporter() //
.withImportOption(option) //
.importPackages(packages) //
.that(not(ignored));
.that(not(ignored.or(IS_AOT_TYPE)));
Classes classes = Classes.of(allClasses);

View File

@@ -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 {
}

View File

@@ -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 {
}

View File

@@ -0,0 +1 @@
package com.acme.myproject.aot;

View File

@@ -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-408
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 -> {