Class ClassFileImporter
java.lang.Object
com.tngtech.archunit.core.importer.ClassFileImporter
The central API to import
Note that information about a class is only complete, if all necessary classes are imported. For example, if class A is imported, and A accesses class B, but class B is not imported, the import will behave according to the configured
The resolution behavior, i.e. what the importer does if a class is missing from the context, can be configured by providing a respective
Whenever the configured
In short, if one wants to write rules about certain properties of classes, it is important to ensure that all relevant classes are imported, even if those might be classes from the JDK (like
For further information consult the ArchUnit user guide.
JavaClasses
from compiled Java class files.
Supports various types of Location
, e.g. Path
,
JarFile
or URL
. The Locations
that are scanned can be filtered by passing any number of
ImportOption
to withImportOption(ImportOption)
, which will then be ANDed (compare
ImportOptions
).
Note that information about a class is only complete, if all necessary classes are imported. For example, if class A is imported, and A accesses class B, but class B is not imported, the import will behave according to the configured
ClassResolver
. With the default configuration, the importer will use the ClassResolverFromClasspath
to attempt to import missing classes from the classpath, and by that complete any information necessary for rules.
In the above case this would mean, if class A accesses B, but B is missing
from the set of imported classes, the importer will try to locate the class on the classpath
and then import that class, thus acquiring more information like superclasses and interfaces.
However, it will not transitively go on to resolve access targets of these classes.
The resolution behavior, i.e. what the importer does if a class is missing from the context, can be configured by providing a respective
ClassResolver
. To use the simplest ClassResolver
possible,
namely one that does not import any further classes, simply configure
"resolveMissingDependenciesFromClassPath"=false
within your "archunit.properties".Whenever the configured
ClassResolver
does not import a class, the importer will simply create a stub with
all information known from the access, i.e. the fully qualified name of B and not much more.
In particular the stub class B will miss any information like super classes, interfaces, etc.
In short, if one wants to write rules about certain properties of classes, it is important to ensure that all relevant classes are imported, even if those might be classes from the JDK (like
RuntimeException
or Exception
).
For further information consult the ArchUnit user guide.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionimportClass
(Class<?> clazz) Delegates toimportClasses(Collection)
importClasses
(Class<?>... classes) Delegates toimportClasses(Collection)
importClasses
(Collection<Class<?>> classes) Imports the class files of the supplied classes from the classpath / modulepath.Imports classes from the whole classpath without archives (JARs or JRTs).importClasspath
(ImportOptions options) Imports classes from the whole classpath considering the suppliedImportOptions
.
Note that ArchUnit does not distinguish between the classpath and the modulepath for Java >= 9, thus all classes from the classpath or the modulepath will be considered.For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
.importJars
(Iterable<JarFile> jarFiles) Imports all Java class files in the givenJAR files
.importJars
(JarFile... jarFiles) Delegates toimportJars(Iterable)
importLocations
(Collection<Location> locations) Imports all class files at the givenlocations
.importPackages
(String... packages) Imports packages viaLocations.ofPackage(String)
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
.importPackages
(Collection<String> packages) Imports packages viaLocations.ofPackage(String)
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
.importPackagesOf
(Class<?>... classes) Delegates toimportPackagesOf(Collection)
importPackagesOf
(Collection<Class<?>> classes) Takes the packages of the supplied classes and delegates toimportPackages(String...)
importPath
(String path) importPath
(Path path) Delegates toimportPaths(Collection)
importPaths
(String... paths) importPaths
(Path... paths) Delegates toimportPaths(Collection)
importPaths
(Collection<Path> paths) Imports all class files at the givenfile paths
.importUrls
(Collection<URL> urls) withImportOption
(ImportOption option) Allows filtering the class files to import by theirLocation
(i.e.
-
Constructor Details
-
ClassFileImporter
-
ClassFileImporter
-
-
Method Details
-
withImportOption
Allows filtering the class files to import by theirLocation
(i.e. URI). Note that this object will not be modified, but instead a copy with adjusted behavior will be returned.- Parameters:
option
- Defines which class fileLocation
to import- Returns:
- A
ClassFileImporter
which filters the specified class file URIs
-
importPath
-
importPath
Delegates toimportPaths(Collection)
-
importPaths
-
importPaths
Delegates toimportPaths(Collection)
-
importPaths
Imports all class files at the givenfile paths
. If aPath
refers to a single class file, that file is imported. If a path refers to a directory, all class files from that directory as well as sub directories will be imported.
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importJar
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importJars
Delegates toimportJars(Iterable)
-
importJars
Imports all Java class files in the givenJAR files
. To import just parts of JAR archives, it is possible to refer to those classes byURL
(compareimportLocations(Collection)
).
For example to import the folder/com/my/subfolder
within/path/to/some.jar
it is possible to use the URLjar:file:///path/to/some.jar!/com/my/subfolder
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importPackages
Imports packages viaLocations.ofPackage(String)
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importPackages
Imports packages viaLocations.ofPackage(String)
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importPackagesOf
Delegates toimportPackagesOf(Collection)
-
importPackagesOf
Takes the packages of the supplied classes and delegates toimportPackages(String...)
-
importClasspath
Imports classes from the whole classpath without archives (JARs or JRTs).
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importClasspath
Imports classes from the whole classpath considering the suppliedImportOptions
.
Note that ArchUnit does not distinguish between the classpath and the modulepath for Java >= 9, thus all classes from the classpath or the modulepath will be considered.
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importClass
Delegates toimportClasses(Collection)
-
importClasses
Delegates toimportClasses(Collection)
-
importClasses
Imports the class files of the supplied classes from the classpath / modulepath.
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
. -
importUrl
-
importUrls
-
importLocations
Imports all class files at the givenlocations
. The behavior will depend on the type ofLocation
, i.e. the target of the underlying URI:- a single class file =< that file will be imported
- a directory =< all class files from that directory as well as sub directories will be imported
- a JAR file =< all class files from that archive will be imported
- a JAR URI representing a class (e.g. '
jar:file:///some.jar!/some/MyClass.class
') =< that file will be imported - a JAR URI representing a folder (e.g. '
jar:file:///some.jar!/some/pkg
') =< all class files from that folder as well as sub folders within the JAR archive will be imported
ClassFileImporter
.
-