-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve automatic resolution of class import dependencies #785
When importing classes there will always be some dependencies that are missing. E.g. we import package `com.myapp`, but naturally those classes will at least depend on classes in `java.lang`. ArchUnit has an automatic import dependency resolution process to pick up such missing classes from the classpath (by default). However, this resolution process was somewhat inconsistent. It would in a first run resolve all missing member types, then in a second run resolve all missing access targets, then supertypes and then annotations. Thus, e.g. classes that were discovered when resolving supertypes would not have their member types resolved anymore, which could lead to surprising behavior. Also, some dependencies were not automatically resolved, in particular: * generic type signatures * enclosing classes of nested classes * component types of array types * types of declared thrown exceptions * target types of instanceof checks * referenced class objects This PR consolidates the resolution process and fixes these shortcomings. It does so by registering all import dependencies already withing the ASM visitors. This way, once all the classes picked originally are imported we now have a set of class names that are needed as dependencies. When we run the importer again to resolve these missing types we will again transitively register the types these types were missing. By distinguishing 6 types of dependencies (member types, accesses, supertypes, enclosing types, annotations and generic type signatures) we can control how deeply we want to resolve each of these sort of dependencies. How many iterations we make for each such type is now configurable via `archunit.properties`. The defaults are configured in a way that the behavior is similar to before, except that the mentioned shortcomings are now fixed. I.e. we resolve one level for member types and accesses, so all the "used types" of the imported classes are present. The other types of dependencies we resolve until we have registered all needed types. That way (meta-)annotations, generic signatures, etc., are all fully resolved. This should provide a reasonable default behavior and also fix most issues that ever occurred because of inconsistent resolution behavior.
- Loading branch information
Showing
47 changed files
with
2,248 additions
and
1,227 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
archunit-integration-test/src/test/resources/archunit.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import.dependencyResolutionProcess.maxIterationsForEnclosingTypes=0 | ||
import.dependencyResolutionProcess.maxIterationsForGenericSignatureTypes=0 | ||
extension.archunit-example-extension.enabled=false | ||
extension.archunit-example-extension.example-prop=exampleValue | ||
freeze.store.default.path=../archunit-example/example-plain/src/test/resources/frozen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import.dependencyResolutionProcess.maxIterationsForEnclosingTypes=0 | ||
import.dependencyResolutionProcess.maxIterationsForGenericSignatureTypes=0 |
2 changes: 2 additions & 0 deletions
2
archunit-junit/junit5/engine/src/test/resources/archunit.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import.dependencyResolutionProcess.maxIterationsForEnclosingTypes=0 | ||
import.dependencyResolutionProcess.maxIterationsForGenericSignatureTypes=0 |
924 changes: 924 additions & 0 deletions
924
...est/java/com/tngtech/archunit/core/importer/ClassFileImporterAutomaticResolutionTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
...unit/core/importer/testexamples/annotations/AnotherAnnotationWithAnnotationParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.annotations; | ||
|
||
public @interface AnotherAnnotationWithAnnotationParameter { | ||
SomeAnnotationWithAnnotationParameter value(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...rchunit/core/importer/testexamples/annotations/SomeAnnotationWithAnnotationParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.annotations; | ||
|
||
public @interface SomeAnnotationWithAnnotationParameter { | ||
SomeAnnotationWithClassParameter value(); | ||
} |
5 changes: 5 additions & 0 deletions
5
...ech/archunit/core/importer/testexamples/annotations/SomeAnnotationWithClassParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.annotations; | ||
|
||
public @interface SomeAnnotationWithClassParameter { | ||
Class<?> value(); | ||
} |
4 changes: 4 additions & 0 deletions
4
...c/jdk9test/java/com/tngtech/archunit/core/importer/testexamples/classhierarchy/Child.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public class Child extends Parent implements ParentInterfaceDirect { | ||
} |
4 changes: 4 additions & 0 deletions
4
...test/java/com/tngtech/archunit/core/importer/testexamples/classhierarchy/GrandParent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public class GrandParent implements ParentInterfaceIndirect { | ||
} |
4 changes: 4 additions & 0 deletions
4
...ngtech/archunit/core/importer/testexamples/classhierarchy/GrandParentInterfaceDirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public interface GrandParentInterfaceDirect { | ||
} |
4 changes: 4 additions & 0 deletions
4
...tech/archunit/core/importer/testexamples/classhierarchy/GrandParentInterfaceIndirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public interface GrandParentInterfaceIndirect { | ||
} |
4 changes: 4 additions & 0 deletions
4
.../jdk9test/java/com/tngtech/archunit/core/importer/testexamples/classhierarchy/Parent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public class Parent extends GrandParent { | ||
} |
4 changes: 4 additions & 0 deletions
4
...com/tngtech/archunit/core/importer/testexamples/classhierarchy/ParentInterfaceDirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public interface ParentInterfaceDirect extends GrandParentInterfaceDirect { | ||
} |
4 changes: 4 additions & 0 deletions
4
...m/tngtech/archunit/core/importer/testexamples/classhierarchy/ParentInterfaceIndirect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.classhierarchy; | ||
|
||
public interface ParentInterfaceIndirect extends GrandParentInterfaceIndirect { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.