|
| 1 | +/* |
| 2 | + * Copyright 2014-2020 TNG Technology Consulting GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.tngtech.archunit.library; |
| 17 | + |
| 18 | +import com.tngtech.archunit.PublicAPI; |
| 19 | +import com.tngtech.archunit.core.domain.Dependency; |
| 20 | +import com.tngtech.archunit.core.domain.JavaClass; |
| 21 | +import com.tngtech.archunit.lang.ArchCondition; |
| 22 | +import com.tngtech.archunit.lang.ArchRule; |
| 23 | +import com.tngtech.archunit.lang.ConditionEvents; |
| 24 | +import com.tngtech.archunit.lang.SimpleConditionEvent; |
| 25 | + |
| 26 | +import static com.tngtech.archunit.PublicAPI.Usage.ACCESS; |
| 27 | +import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses; |
| 28 | + |
| 29 | +@PublicAPI(usage = ACCESS) |
| 30 | +public final class DependencyRules { |
| 31 | + private DependencyRules() { |
| 32 | + } |
| 33 | + |
| 34 | + @PublicAPI(usage = ACCESS) |
| 35 | + public static final ArchRule NO_CLASSES_SHOULD_DEPEND_UPPER_PACKAGES = |
| 36 | + noClasses().should(dependOnUpperPackages()) |
| 37 | + .because("that might prevent packages on that level from being split into separate artifacts in a clean way"); |
| 38 | + |
| 39 | + @PublicAPI(usage = ACCESS) |
| 40 | + public static ArchCondition<JavaClass> dependOnUpperPackages() { |
| 41 | + return new DependOnUpperPackagesCondition(); |
| 42 | + } |
| 43 | + |
| 44 | + private static class DependOnUpperPackagesCondition extends ArchCondition<JavaClass> { |
| 45 | + DependOnUpperPackagesCondition() { |
| 46 | + super("depend on upper packages"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void check(final JavaClass clazz, final ConditionEvents events) { |
| 51 | + for (Dependency dependency : clazz.getDirectDependenciesFromSelf()) { |
| 52 | + boolean dependencyOnUpperPackage = isDependencyOnUpperPackage(dependency.getOriginClass(), dependency.getTargetClass()); |
| 53 | + events.add(new SimpleConditionEvent(dependency, dependencyOnUpperPackage, dependency.getDescription())); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private boolean isDependencyOnUpperPackage(JavaClass origin, JavaClass target) { |
| 58 | + String originPackageName = origin.getPackageName(); |
| 59 | + String targetSubPackagePrefix = target.getPackageName() + "."; |
| 60 | + return originPackageName.startsWith(targetSubPackagePrefix); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments