From ef726a4bde4f11cd8fe3c9919b4de70970854634 Mon Sep 17 00:00:00 2001 From: Peter Gafert Date: Sat, 9 Mar 2024 23:02:13 +0100 Subject: [PATCH] pass `SourceCodeLocation` on to construct `Dependency` At the moment the line number is passed to the `Dependency`, then it constructs a `SourceCodeLocation` from the `originClass` and the `lineNumber`. In all cases we have a `SourceCodeLocation` outside already though. And the `SourceCodeLocation` constructed in the constructor of `Dependency` will just create an equivalent `SourceCodeLocation` to what we already have when calling the constructor. We can thus just pass on the `SourceCodeLocation` from the outside. Signed-off-by: Peter Gafert --- .../archunit/core/domain/Dependency.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/core/domain/Dependency.java b/archunit/src/main/java/com/tngtech/archunit/core/domain/Dependency.java index dc0faf077..ca16c2e04 100644 --- a/archunit/src/main/java/com/tngtech/archunit/core/domain/Dependency.java +++ b/archunit/src/main/java/com/tngtech/archunit/core/domain/Dependency.java @@ -62,7 +62,7 @@ public final class Dependency implements HasDescription, Comparable, private final SourceCodeLocation sourceCodeLocation; private final int hashCode; - private Dependency(JavaClass originClass, JavaClass targetClass, int lineNumber, String description) { + private Dependency(JavaClass originClass, JavaClass targetClass, SourceCodeLocation sourceCodeLocation, String description) { checkArgument(!originClass.equals(targetClass) || targetClass.isPrimitive(), "Tried to create illegal dependency '%s' (%s -> %s), this is likely a bug!", description, originClass.getSimpleName(), targetClass.getSimpleName()); @@ -70,8 +70,8 @@ private Dependency(JavaClass originClass, JavaClass targetClass, int lineNumber, this.originClass = originClass; this.targetClass = targetClass; this.description = description; - this.sourceCodeLocation = SourceCodeLocation.of(originClass, lineNumber); - hashCode = Objects.hash(originClass, targetClass, lineNumber, description); + this.sourceCodeLocation = sourceCodeLocation; + hashCode = Objects.hash(originClass, targetClass, sourceCodeLocation, description); } static Set tryCreateFromAccess(JavaAccess access) { @@ -79,7 +79,7 @@ static Set tryCreateFromAccess(JavaAccess access) { JavaClass targetOwner = access.getTargetOwner(); ImmutableSet.Builder dependencies = ImmutableSet.builder() .addAll(createComponentTypeDependencies(originOwner, access.getOrigin().getDescription(), targetOwner, access.getSourceCodeLocation())); - dependencies.addAll(asSet(tryCreateDependency(originOwner, targetOwner, access.getDescription(), access.getLineNumber()))); + dependencies.addAll(asSet(tryCreateDependency(originOwner, targetOwner, access.getDescription(), access.getSourceCodeLocation()))); return dependencies.build(); } @@ -95,7 +95,7 @@ static Dependency fromInheritance(JavaClass origin, JavaClass targetSupertype) { String dependencyDescription = originDescription + " " + dependencyType + " " + targetType + " " + targetDescription; String description = dependencyDescription + " in " + origin.getSourceCodeLocation(); - Optional result = tryCreateDependency(origin, targetSupertype, description, 0); + Optional result = tryCreateDependency(origin, targetSupertype, description, origin.getSourceCodeLocation()); if (!result.isPresent()) { throw new IllegalStateException(String.format("Tried to create illegal inheritance dependency '%s' (%s -> %s), this is likely a bug!", @@ -212,7 +212,7 @@ private static Set tryCreateDependency( String targetDescription = bracketFormat(targetClass.getName()); String dependencyDescription = originDescription + " " + dependencyType + " " + targetDescription; String description = dependencyDescription + " in " + sourceCodeLocation; - dependencies.addAll(asSet(tryCreateDependency(originClass, targetClass, description, sourceCodeLocation.getLineNumber()))); + dependencies.addAll(asSet(tryCreateDependency(originClass, targetClass, description, sourceCodeLocation))); return dependencies.build(); } @@ -225,17 +225,17 @@ private static Set createComponentTypeDependencies( String componentTypeTargetDescription = bracketFormat(componentType.get().getName()); String componentTypeDependencyDescription = originDescription + " depends on component type " + componentTypeTargetDescription; String componentTypeDescription = componentTypeDependencyDescription + " in " + sourceCodeLocation; - result.addAll(asSet(tryCreateDependency(originClass, componentType.get(), componentTypeDescription, sourceCodeLocation.getLineNumber()))); + result.addAll(asSet(tryCreateDependency(originClass, componentType.get(), componentTypeDescription, sourceCodeLocation))); componentType = componentType.get().tryGetComponentType(); } return result.build(); } - private static Optional tryCreateDependency(JavaClass originClass, JavaClass targetClass, String description, int lineNumber) { + private static Optional tryCreateDependency(JavaClass originClass, JavaClass targetClass, String description, SourceCodeLocation sourceCodeLocation) { if (originClass.equals(targetClass) || targetClass.isPrimitive()) { return Optional.empty(); } - return Optional.of(new Dependency(originClass, targetClass, lineNumber, description)); + return Optional.of(new Dependency(originClass, targetClass, sourceCodeLocation, description)); } private static String bracketFormat(String name) {