|
17 | 17 | package org.springframework.build.architecture; |
18 | 18 |
|
19 | 19 | import com.tngtech.archunit.base.DescribedPredicate; |
| 20 | +import com.tngtech.archunit.core.domain.JavaAnnotation; |
20 | 21 | import com.tngtech.archunit.core.domain.JavaClass; |
21 | 22 | import com.tngtech.archunit.lang.ArchRule; |
22 | 23 | import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; |
23 | 24 | import com.tngtech.archunit.library.dependencies.SliceAssignment; |
24 | 25 | import com.tngtech.archunit.library.dependencies.SliceIdentifier; |
25 | 26 | import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition; |
26 | | -import java.util.List; |
27 | 27 |
|
28 | 28 | abstract class ArchitectureRules { |
29 | 29 |
|
| 30 | + private static final String[] SHADED_PACKAGES = {"org.springframework.asm..", |
| 31 | + "org.springframework.cglib..", |
| 32 | + "org.springframework.javapoet..", |
| 33 | + "org.springframework.objenesis.."}; |
| 34 | + |
30 | 35 | static ArchRule allPackagesShouldBeFreeOfTangles() { |
31 | 36 | return SlicesRuleDefinition.slices() |
32 | 37 | .assignedFrom(new SpringSlices()).should().beFreeOfCycles(); |
@@ -80,18 +85,41 @@ public boolean test(JavaClass javaClass) { |
80 | 85 | .allowEmptyShould(true); |
81 | 86 | } |
82 | 87 |
|
83 | | - static class SpringSlices implements SliceAssignment { |
| 88 | + static ArchRule deprecatedMethodsShouldHaveSinceAttribute() { |
| 89 | + return ArchRuleDefinition.methods().that() |
| 90 | + .areDeclaredInClassesThat().resideOutsideOfPackages(SHADED_PACKAGES).and() |
| 91 | + .areAnnotatedWith(Deprecated.class) |
| 92 | + .should().beAnnotatedWith(deprecatedWithSinceAttribute()) |
| 93 | + .allowEmptyShould(true); |
| 94 | + } |
84 | 95 |
|
85 | | - private final List<String> ignoredPackages = List.of("org.springframework.asm", |
86 | | - "org.springframework.cglib", |
87 | | - "org.springframework.javapoet", |
88 | | - "org.springframework.objenesis"); |
| 96 | + static ArchRule deprecatedTypesShouldHaveSinceAttribute() { |
| 97 | + return ArchRuleDefinition.classes().that() |
| 98 | + .resideOutsideOfPackages(SHADED_PACKAGES).and() |
| 99 | + .areAnnotatedWith(Deprecated.class) |
| 100 | + .should().beAnnotatedWith(deprecatedWithSinceAttribute()) |
| 101 | + .allowEmptyShould(true); |
| 102 | + } |
| 103 | + |
| 104 | + private static DescribedPredicate<JavaAnnotation> deprecatedWithSinceAttribute() { |
| 105 | + return new DescribedPredicate<JavaAnnotation>("@Deprecated(since=\"...\"") { |
| 106 | + @Override |
| 107 | + public boolean test(JavaAnnotation annotation) { |
| 108 | + if (annotation.getRawType().isEquivalentTo(Deprecated.class)) { |
| 109 | + return annotation.get("since").isPresent() && |
| 110 | + !annotation.get("since").get().toString().isEmpty(); |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
| 116 | + |
| 117 | + static class SpringSlices implements SliceAssignment { |
89 | 118 |
|
90 | 119 | @Override |
91 | 120 | public SliceIdentifier getIdentifierOf(JavaClass javaClass) { |
92 | | - |
93 | 121 | String packageName = javaClass.getPackageName(); |
94 | | - for (String ignoredPackage : ignoredPackages) { |
| 122 | + for (String ignoredPackage : SHADED_PACKAGES) { |
95 | 123 | if (packageName.startsWith(ignoredPackage)) { |
96 | 124 | return SliceIdentifier.ignore(); |
97 | 125 | } |
|
0 commit comments