Skip to content

Commit

Permalink
pass SourceCodeLocation on to construct Dependency
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
codecholeric committed Mar 9, 2024
1 parent d4a8233 commit ef726a4
Showing 1 changed file with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,24 @@ public final class Dependency implements HasDescription, Comparable<Dependency>,
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());

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<Dependency> tryCreateFromAccess(JavaAccess<?> access) {
JavaClass originOwner = access.getOriginOwner();
JavaClass targetOwner = access.getTargetOwner();
ImmutableSet.Builder<Dependency> dependencies = ImmutableSet.<Dependency>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();
}

Expand All @@ -95,7 +95,7 @@ static Dependency fromInheritance(JavaClass origin, JavaClass targetSupertype) {
String dependencyDescription = originDescription + " " + dependencyType + " " + targetType + " " + targetDescription;

String description = dependencyDescription + " in " + origin.getSourceCodeLocation();
Optional<Dependency> result = tryCreateDependency(origin, targetSupertype, description, 0);
Optional<Dependency> 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!",
Expand Down Expand Up @@ -212,7 +212,7 @@ private static Set<Dependency> 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();
}

Expand All @@ -225,17 +225,17 @@ private static Set<Dependency> 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<Dependency> tryCreateDependency(JavaClass originClass, JavaClass targetClass, String description, int lineNumber) {
private static Optional<Dependency> 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) {
Expand Down

0 comments on commit ef726a4

Please sign in to comment.