Skip to content

Commit 66d8ac4

Browse files
hankemcodecholeric
authored andcommitted
add JavaClass.isFullyImported()
This method reports whether the class is completed from the ImportContext. Take an import of package `com.foo`, where some class `com.foo.Foo` accesses another class `com.bar.Bar`, then `Foo` would be `isFullyImported() == true`, because it was explicitly covered by the package to be imported, but `Bar` would have `isFullyImported() == false`, because it was not covered by the original package. This method is useful, because it has strong implications, whether the class is fully imported or not. For example only a fully imported classes has its dependencies imported, a class that was just imported as a dependency of a fully imported class will have its dependencies cut off to avoid uncontrollably huge imports. Signed-off-by: Manfred Hanke <[email protected]>
1 parent 66d97c5 commit 66d8ac4

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

archunit/src/main/java/com/tngtech/archunit/core/domain/JavaClass.java

+13
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public Set<JavaMember> get() {
147147
});
148148
private JavaClassDependencies javaClassDependencies = new JavaClassDependencies(this); // just for stubs; will be overwritten for imported classes
149149
private ReverseDependencies reverseDependencies = ReverseDependencies.EMPTY; // just for stubs; will be overwritten for imported classes
150+
private boolean fullyImported = false;
150151

151152
JavaClass(JavaClassBuilder builder) {
152153
source = checkNotNull(builder.getSource());
@@ -1149,6 +1150,17 @@ public Set<InstanceofCheck> getInstanceofChecksWithTypeOfSelf() {
11491150
return reverseDependencies.getInstanceofChecksWithTypeOf(this);
11501151
}
11511152

1153+
/**
1154+
* @return Whether this class has been fully imported, including all dependencies.<br>
1155+
* Classes that are only transitively imported are not necessarily fully imported.<br><br>
1156+
* Suppose you only import a class {@code Foo} that calls a method of class {@code Bar}.
1157+
* Then {@code Bar} is, as a dependency of the fully imported class {@code Foo}, only transitively imported.
1158+
*/
1159+
@PublicAPI(usage = ACCESS)
1160+
public boolean isFullyImported() {
1161+
return fullyImported;
1162+
}
1163+
11521164
/**
11531165
* @param clazz An arbitrary type
11541166
* @return true, if this {@link JavaClass} represents the same class as the supplied {@link Class}, otherwise false
@@ -1301,6 +1313,7 @@ JavaClassDependencies completeFrom(ImportContext context) {
13011313
codeUnit.completeFrom(context);
13021314
}
13031315
javaClassDependencies = new JavaClassDependencies(this);
1316+
fullyImported = true;
13041317
return javaClassDependencies;
13051318
}
13061319

archunit/src/test/java/com/tngtech/archunit/core/importer/ClassFileImporterTest.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ public void imports_simple_class_details() throws Exception {
244244
ImportedClasses classes = classesIn("testexamples/simpleimport");
245245
JavaClass javaClass = classes.get(ClassToImportOne.class);
246246

247+
assertThat(javaClass.isFullyImported()).isTrue();
247248
assertThat(javaClass.getName()).as("full name").isEqualTo(ClassToImportOne.class.getName());
248249
assertThat(javaClass.getSimpleName()).as("simple name").isEqualTo(ClassToImportOne.class.getSimpleName());
249250
assertThat(javaClass.getPackageName()).as("package name").isEqualTo(ClassToImportOne.class.getPackage().getName());
@@ -1714,7 +1715,7 @@ public void resolve_missing_dependencies_from_classpath_can_be_toogled() throws
17141715
}
17151716

17161717
@DataProvider
1717-
public static Object[][] classes_not_directly_imported() {
1718+
public static Object[][] classes_not_fully_imported() {
17181719
class Element {
17191720
}
17201721
@SuppressWarnings("unused")
@@ -1736,23 +1737,24 @@ class DependsOnArray {
17361737
}
17371738

17381739
@Test
1739-
@UseDataProvider("classes_not_directly_imported")
1740-
public void classes_not_directly_imported_have_empty_dependencies(@SuppressWarnings("unused") String description, JavaClass notDirectlyImported) {
1741-
assertThat(notDirectlyImported.getDirectDependenciesFromSelf()).isEmpty();
1742-
assertThat(notDirectlyImported.getDirectDependenciesToSelf()).isEmpty();
1743-
assertThat(notDirectlyImported.getFieldAccessesToSelf()).isEmpty();
1744-
assertThat(notDirectlyImported.getMethodCallsToSelf()).isEmpty();
1745-
assertThat(notDirectlyImported.getConstructorCallsToSelf()).isEmpty();
1746-
assertThat(notDirectlyImported.getAccessesToSelf()).isEmpty();
1747-
assertThat(notDirectlyImported.getFieldsWithTypeOfSelf()).isEmpty();
1748-
assertThat(notDirectlyImported.getMethodsWithParameterTypeOfSelf()).isEmpty();
1749-
assertThat(notDirectlyImported.getMethodsWithReturnTypeOfSelf()).isEmpty();
1750-
assertThat(notDirectlyImported.getMethodThrowsDeclarationsWithTypeOfSelf()).isEmpty();
1751-
assertThat(notDirectlyImported.getConstructorsWithParameterTypeOfSelf()).isEmpty();
1752-
assertThat(notDirectlyImported.getConstructorsWithThrowsDeclarationTypeOfSelf()).isEmpty();
1753-
assertThat(notDirectlyImported.getAnnotationsWithTypeOfSelf()).isEmpty();
1754-
assertThat(notDirectlyImported.getAnnotationsWithParameterTypeOfSelf()).isEmpty();
1755-
assertThat(notDirectlyImported.getInstanceofChecksWithTypeOfSelf()).isEmpty();
1740+
@UseDataProvider("classes_not_fully_imported")
1741+
public void classes_not_fully_imported_have_flag_fullyImported_false_and_empty_dependencies(@SuppressWarnings("unused") String description, JavaClass notFullyImported) {
1742+
assertThat(notFullyImported.isFullyImported()).isFalse();
1743+
assertThat(notFullyImported.getDirectDependenciesFromSelf()).isEmpty();
1744+
assertThat(notFullyImported.getDirectDependenciesToSelf()).isEmpty();
1745+
assertThat(notFullyImported.getFieldAccessesToSelf()).isEmpty();
1746+
assertThat(notFullyImported.getMethodCallsToSelf()).isEmpty();
1747+
assertThat(notFullyImported.getConstructorCallsToSelf()).isEmpty();
1748+
assertThat(notFullyImported.getAccessesToSelf()).isEmpty();
1749+
assertThat(notFullyImported.getFieldsWithTypeOfSelf()).isEmpty();
1750+
assertThat(notFullyImported.getMethodsWithParameterTypeOfSelf()).isEmpty();
1751+
assertThat(notFullyImported.getMethodsWithReturnTypeOfSelf()).isEmpty();
1752+
assertThat(notFullyImported.getMethodThrowsDeclarationsWithTypeOfSelf()).isEmpty();
1753+
assertThat(notFullyImported.getConstructorsWithParameterTypeOfSelf()).isEmpty();
1754+
assertThat(notFullyImported.getConstructorsWithThrowsDeclarationTypeOfSelf()).isEmpty();
1755+
assertThat(notFullyImported.getAnnotationsWithTypeOfSelf()).isEmpty();
1756+
assertThat(notFullyImported.getAnnotationsWithParameterTypeOfSelf()).isEmpty();
1757+
assertThat(notFullyImported.getInstanceofChecksWithTypeOfSelf()).isEmpty();
17561758
}
17571759

17581760
@Test

0 commit comments

Comments
 (0)