-
Notifications
You must be signed in to change notification settings - Fork 334
Allow package info classes to be resolved as a dependency #1565
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.tngtech.archunit.core.domain.packageexamples.annotated; | ||
|
|
||
| public class WithinAnnotatedPackage { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
|
|
@@ -22,17 +23,20 @@ | |
| import com.tngtech.archunit.core.domain.JavaConstructorCall; | ||
| import com.tngtech.archunit.core.domain.JavaConstructorReference; | ||
| import com.tngtech.archunit.core.domain.JavaEnumConstant; | ||
| import com.tngtech.archunit.core.domain.JavaField; | ||
| import com.tngtech.archunit.core.domain.JavaFieldAccess; | ||
| import com.tngtech.archunit.core.domain.JavaMethod; | ||
| import com.tngtech.archunit.core.domain.JavaMethodCall; | ||
| import com.tngtech.archunit.core.domain.JavaMethodReference; | ||
| import com.tngtech.archunit.core.domain.JavaPackage; | ||
| import com.tngtech.archunit.core.domain.JavaParameterizedType; | ||
| import com.tngtech.archunit.core.domain.JavaType; | ||
| import com.tngtech.archunit.core.domain.JavaWildcardType; | ||
| import com.tngtech.archunit.core.domain.ReferencedClassObject; | ||
| import com.tngtech.archunit.core.domain.ThrowsDeclaration; | ||
| import com.tngtech.archunit.core.domain.properties.HasAnnotations; | ||
| import com.tngtech.archunit.core.importer.DependencyResolutionProcessTestUtils.ImporterWithAdjustedResolutionRuns; | ||
| import com.tngtech.archunit.core.importer.testexamples.OtherClass; | ||
| import com.tngtech.archunit.core.importer.testexamples.SomeAnnotation; | ||
| import com.tngtech.archunit.core.importer.testexamples.annotatedclassimport.ClassWithUnimportedAnnotation; | ||
| import com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters; | ||
|
|
@@ -44,9 +48,11 @@ | |
| import com.tngtech.archunit.core.importer.testexamples.annotationresolution.SomeAnnotationWithAnnotationParameter; | ||
| import com.tngtech.archunit.core.importer.testexamples.annotationresolution.SomeAnnotationWithClassParameter; | ||
| import com.tngtech.archunit.core.importer.testexamples.classhierarchyresolution.Child; | ||
| import com.tngtech.archunit.core.importer.testexamples.packageinforesolution.ClassThatReferencesOtherClass; | ||
| import com.tngtech.java.junit.dataprovider.DataProvider; | ||
| import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
| import com.tngtech.java.junit.dataprovider.UseDataProvider; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
|
|
||
|
|
@@ -59,6 +65,7 @@ | |
| import static com.tngtech.archunit.core.importer.DependencyResolutionProcess.MAX_ITERATIONS_FOR_GENERIC_SIGNATURE_TYPES_PROPERTY_NAME; | ||
| import static com.tngtech.archunit.core.importer.DependencyResolutionProcess.MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME; | ||
| import static com.tngtech.archunit.core.importer.DependencyResolutionProcess.MAX_ITERATIONS_FOR_SUPERTYPES_PROPERTY_NAME; | ||
| import static com.tngtech.archunit.core.importer.DependencyResolutionProcess.MAX_ITERATIONS_FOR_PACKAGE_INFO_PROPERTY_NAME; | ||
| import static com.tngtech.archunit.core.importer.testexamples.SomeEnum.OTHER_VALUE; | ||
| import static com.tngtech.archunit.core.importer.testexamples.SomeEnum.SOME_VALUE; | ||
| import static com.tngtech.archunit.core.importer.testexamples.annotatedparameters.ClassWithMethodWithAnnotatedParameters.methodWithOneAnnotatedParameterWithTwoAnnotations; | ||
|
|
@@ -620,6 +627,33 @@ class Innermost { | |
| assertThatType(outermost).matches(Outermost.class); | ||
| } | ||
|
|
||
| @Test | ||
| public void automatically_resolves_packages() { | ||
| JavaClass otherClass = new ClassFileImporter() | ||
| .importClass(OtherClass.class); | ||
|
|
||
| JavaPackage checkedClassPackageInfo = otherClass.getPackage(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like these exist in JavaPackageTest: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but since you re-create the JavaPackage, it might be worth checking that the re-created package behaves as expected in these methods |
||
| assertThat(checkedClassPackageInfo.getAnnotations()).hasSize(1); | ||
| assertThat(checkedClassPackageInfo.isAnnotatedWith(SomeAnnotation.class)).isTrue(); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When running
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is already the normal behavior in scanning a package. Ill add this assertion to JavaPackageTest.test_getPackageInfo() to show this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about packages without a package-info file? I would prefer to never have package-info in this result |
||
|
|
||
| @Test | ||
| public void automatically_resolves_dependency_packages() { | ||
| JavaClass checkedClass = ImporterWithAdjustedResolutionRuns | ||
| .disableAllIterationsExcept(MAX_ITERATIONS_FOR_PACKAGE_INFO_PROPERTY_NAME, MAX_ITERATIONS_FOR_MEMBER_TYPES_PROPERTY_NAME) | ||
| .importClass(ClassThatReferencesOtherClass.class); | ||
|
|
||
| Optional<JavaField> firstField = checkedClass.getAllFields().stream().findFirst(); | ||
| assertThat(firstField).isPresent(); | ||
| JavaClass otherClass = firstField.get().getRawType(); | ||
| assertThat(otherClass).isFullyImported(true); | ||
|
|
||
| JavaPackage otherClassPackageInfo = otherClass.getPackage(); | ||
|
|
||
| Assertions.assertThat(otherClassPackageInfo.getAnnotations()).hasSize(1); | ||
| Assertions.assertThat(otherClassPackageInfo.isAnnotatedWith(SomeAnnotation.class)).isTrue(); | ||
| } | ||
|
|
||
| @DataProvider | ||
| public static Object[][] data_automatically_resolves_generic_type_parameter_bounds() { | ||
| @SuppressWarnings("unused") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.tngtech.archunit.core.importer.testexamples.packageinforesolution; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this new package name.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is important that this class is in a different package than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already suspected that, but "package in for resolution" doesn't really tell me anything. |
||
|
|
||
| import com.tngtech.archunit.core.importer.testexamples.OtherClass; | ||
|
|
||
| public class ClassThatReferencesOtherClass { | ||
| OtherClass otherClass; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the class resolution yet, but this looks to me as if the package is re-created with just the current class and the
package-infoclass.If you look e.g. at the package
testexamples, you have these classes (and more):SomeClassOtherClasspackage-infoassuming we are now in the
JavaClassrepresentingSomeClass, I expect thatgetPackage().getClasses()contains all three of these classes, but I don't see how it could return anything else thenSet.of(SomeClass, package-info), as this statement replaces the package and just passes these 2 classes into the new factory.I don't see any call in
JavaPackage.fromchecking the package of the class, which could contain the information.It is also not clear to me how the dependencies and subpackages are build if all the other classes in teh package are missing.
Wouldn't it be much cleaner if we could resolve the
package-infoclass when initially building theJavaPackage?Why should each
JavaClassin that package be responsible for resolving thepackage-info?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because
initially building JavaPackageis only something that happens if the package-info class is in the initial list of files passed to ClassfileImporter. when class dependencies are resolved, if the other class lives in another (non-imported) package, only the package name is known. this method that is added allows us to resolve the rest of the package info for this package, even if the entire package was not included in the import.