-
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 |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| import com.tngtech.archunit.base.ArchUnitException.InvalidSyntaxUsageException; | ||
| import com.tngtech.archunit.base.DescribedPredicate; | ||
| import com.tngtech.archunit.base.HasDescription; | ||
| import com.tngtech.archunit.core.domain.packageexamples.annotated.PackageLevelAnnotation; | ||
| import com.tngtech.archunit.core.domain.packageexamples.annotated.WithinAnnotatedPackage; | ||
| import com.tngtech.archunit.core.domain.testobjects.AAccessingB; | ||
| import com.tngtech.archunit.core.domain.testobjects.AExtendingSuperAImplementingInterfaceForA; | ||
| import com.tngtech.archunit.core.domain.testobjects.AReferencingB; | ||
|
|
@@ -1595,6 +1597,15 @@ public void function_getPackage() { | |
| .isEqualTo(javaClass.getPackageName()); | ||
| } | ||
|
|
||
| @Test | ||
| public void function_getPackageInfo() { | ||
|
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. looking at the previous tests in this class, I would expect that a test with this name would test |
||
| JavaClass javaClass = importClassWithContext(WithinAnnotatedPackage.class); | ||
|
|
||
| assertThat(javaClass.getPackage().isAnnotatedWith(PackageLevelAnnotation.class)) | ||
| .as("package info is available") | ||
| .isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void functions_get_members() { | ||
| JavaClass javaClass = importClassWithContext(ClassWithSeveralConstructorsFieldsAndMethods.class); | ||
|
|
||
| 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 |
||
| 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 |
||
|
|
||
| @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. |
||
|
|
||
| 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?