Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method toClassDependencies() to JavaAccess and to SliceDependency #1250

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ String additionalToStringFields() {
return "";
}

/**
* @return all matching class dependencies.
*/
@PublicAPI(usage = ACCESS)
public Set<Dependency> toClassDependencies() {
Copy link
Member

@hankem hankem Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name might be inspired by the previous usage of Dependency.tryCreateFromAccess in JavaClassDependencies (which is no public API). I wonder whether the name getDependencies might be more intuitive? (Same in SliceDependency.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name was chosen as in the existing method com.tngtech.archunit.library.modules.ModuleDependency#toClassDependencies

To be honest, I would use getDependencies otherwise.

I have also considered adding an interface declaring this method. It might be called SomeDependency and implemented by JavaAccess, SliceDependency, ModuleDependency, and Dependency. I decided against it only because I was not sure about its name.

return Dependency.tryCreateFromAccess(this);
}

@Override
@PublicAPI(usage = ACCESS)
public String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

Expand Down Expand Up @@ -62,6 +63,11 @@ public Slice getTarget() {
return target;
}

@PublicAPI(usage = ACCESS)
public Set<Dependency> toClassDependencies() {
return relevantDependencies;
}

@Override
@PublicAPI(usage = ACCESS)
public String getDescription() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
import com.tngtech.archunit.core.domain.JavaAccess.Functions.Get;
import com.tngtech.archunit.core.importer.testexamples.SomeClass;
import com.tngtech.archunit.core.importer.testexamples.SomeEnum;
import org.assertj.core.api.Assertions;
import org.junit.Test;

import static com.google.common.collect.Iterables.getOnlyElement;
import static com.tngtech.archunit.base.DescribedPredicate.alwaysFalse;
import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext;
import static com.tngtech.archunit.core.domain.TestUtils.importClassesWithContext;
import static com.tngtech.archunit.core.domain.TestUtils.newMethodCallBuilder;
import static com.tngtech.archunit.core.domain.TestUtils.resolvedTargetFrom;
import static com.tngtech.archunit.core.domain.TestUtils.simulateCall;
import static com.tngtech.archunit.testutil.Assertions.assertThat;
import static com.tngtech.archunit.testutil.Assertions.assertThatType;

public class JavaAccessTest {
@Test
Expand All @@ -24,6 +27,15 @@ public void when_the_origin_is_an_inner_class_the_toplevel_class_is_displayed_as

assertThat(access.getDescription()).contains("(SomeClass.java:7)");
}
@Test
public void java_access_transforms_to_class_dependency() {
TestJavaAccess access = anyAccess();
Dependency dependency = getOnlyElement(access.toClassDependencies());

assertThatType(dependency.getOriginClass()).as("origin class").isEqualTo(access.getOriginOwner());
assertThatType(dependency.getTargetClass()).as("target class").isEqualTo(access.getTargetOwner());
Assertions.assertThat(dependency.getDescription()).as("description").isEqualTo(access.getDescription());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move the test for access.toClassDependencies() to a separate test, as it's not really related to when_the_origin_is_an_inner_class_the_toplevel_class_is_displayed_as_location.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

@Test
public void location_of_origin_of_deeper_inner_class_hierarchies() {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/src/main/groovy/archunit.java-conventions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ tasks.withType(JavaCompile) { Task task ->

javadoc {
options.addBooleanOption('html5', true)
options.encoding = UTF_8.toString()
}