Skip to content

Commit

Permalink
add JavaClass.isFullyImported()
Browse files Browse the repository at this point in the history
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 `isImported() == true`, because it was explicitly covered by the package to be imported, but `Bar` would have `isImported() == 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]>

reprase Javadoc like this?
  • Loading branch information
hankem committed Dec 16, 2020
1 parent d9dba01 commit 4786ed4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public Set<JavaMember> get() {
});
private JavaClassDependencies javaClassDependencies = new JavaClassDependencies(this); // just for stubs; will be overwritten for imported classes
private ReverseDependencies reverseDependencies = ReverseDependencies.EMPTY; // just for stubs; will be overwritten for imported classes
private boolean fullyImported = false;

JavaClass(JavaClassBuilder builder) {
source = checkNotNull(builder.getSource());
Expand Down Expand Up @@ -1149,6 +1150,17 @@ public Set<InstanceofCheck> getInstanceofChecksWithTypeOfSelf() {
return reverseDependencies.getInstanceofChecksWithTypeOf(this);
}

/**
* @return Whether this class has been fully imported, including all dependencies.<br>
* Classes that are only transitively imported are not necessarily fully imported.<br><br>
* Suppose you only import a class {@code Foo} that calls a method of class {@code Bar}.
* Then {@code Bar} is, as a dependency of the fully imported class {@code Foo}, only transitively imported.
*/
@PublicAPI(usage = ACCESS)
public boolean isFullyImported() {
return fullyImported;
}

/**
* @param clazz An arbitrary type
* @return true, if this {@link JavaClass} represents the same class as the supplied {@link Class}, otherwise false
Expand Down Expand Up @@ -1301,6 +1313,7 @@ JavaClassDependencies completeFrom(ImportContext context) {
codeUnit.completeFrom(context);
}
javaClassDependencies = new JavaClassDependencies(this);
fullyImported = true;
return javaClassDependencies;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void imports_simple_class_details() throws Exception {
ImportedClasses classes = classesIn("testexamples/simpleimport");
JavaClass javaClass = classes.get(ClassToImportOne.class);

assertThat(javaClass.isFullyImported()).isTrue();
assertThat(javaClass.getName()).as("full name").isEqualTo(ClassToImportOne.class.getName());
assertThat(javaClass.getSimpleName()).as("simple name").isEqualTo(ClassToImportOne.class.getSimpleName());
assertThat(javaClass.getPackageName()).as("package name").isEqualTo(ClassToImportOne.class.getPackage().getName());
Expand Down Expand Up @@ -1714,7 +1715,7 @@ public void resolve_missing_dependencies_from_classpath_can_be_toogled() throws
}

@DataProvider
public static Object[][] classes_not_directly_imported() {
public static Object[][] classes_not_fully_imported() {
class Element {
}
@SuppressWarnings("unused")
Expand All @@ -1736,23 +1737,24 @@ class DependsOnArray {
}

@Test
@UseDataProvider("classes_not_directly_imported")
public void classes_not_directly_imported_have_empty_dependencies(@SuppressWarnings("unused") String description, JavaClass notDirectlyImported) {
assertThat(notDirectlyImported.getDirectDependenciesFromSelf()).isEmpty();
assertThat(notDirectlyImported.getDirectDependenciesToSelf()).isEmpty();
assertThat(notDirectlyImported.getFieldAccessesToSelf()).isEmpty();
assertThat(notDirectlyImported.getMethodCallsToSelf()).isEmpty();
assertThat(notDirectlyImported.getConstructorCallsToSelf()).isEmpty();
assertThat(notDirectlyImported.getAccessesToSelf()).isEmpty();
assertThat(notDirectlyImported.getFieldsWithTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getMethodsWithParameterTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getMethodsWithReturnTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getMethodThrowsDeclarationsWithTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getConstructorsWithParameterTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getConstructorsWithThrowsDeclarationTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getAnnotationsWithTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getAnnotationsWithParameterTypeOfSelf()).isEmpty();
assertThat(notDirectlyImported.getInstanceofChecksWithTypeOfSelf()).isEmpty();
@UseDataProvider("classes_not_fully_imported")
public void classes_not_fully_imported_have_flag_fullyImported_false_and_empty_dependencies(@SuppressWarnings("unused") String description, JavaClass notFullyImported) {
assertThat(notFullyImported.isFullyImported()).isFalse();
assertThat(notFullyImported.getDirectDependenciesFromSelf()).isEmpty();
assertThat(notFullyImported.getDirectDependenciesToSelf()).isEmpty();
assertThat(notFullyImported.getFieldAccessesToSelf()).isEmpty();
assertThat(notFullyImported.getMethodCallsToSelf()).isEmpty();
assertThat(notFullyImported.getConstructorCallsToSelf()).isEmpty();
assertThat(notFullyImported.getAccessesToSelf()).isEmpty();
assertThat(notFullyImported.getFieldsWithTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getMethodsWithParameterTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getMethodsWithReturnTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getMethodThrowsDeclarationsWithTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getConstructorsWithParameterTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getConstructorsWithThrowsDeclarationTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getAnnotationsWithTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getAnnotationsWithParameterTypeOfSelf()).isEmpty();
assertThat(notFullyImported.getInstanceofChecksWithTypeOfSelf()).isEmpty();
}

@Test
Expand Down

0 comments on commit 4786ed4

Please sign in to comment.