Class ClassFileImporter
java.lang.Object
com.tngtech.archunit.core.importer.ClassFileImporter
public final class ClassFileImporter
extends java.lang.Object
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:
ArchConfiguration
-
Constructor Summary
Constructors Constructor Description ClassFileImporter()
ClassFileImporter(ImportOptions importOptions)
-
Method Summary
Modifier and Type Method Description JavaClass
importClass(java.lang.Class<?> clazz)
Delegates toimportClasses(Collection)
JavaClasses
importClasses(java.lang.Class<?>... classes)
Delegates toimportClasses(Collection)
JavaClasses
importClasses(java.util.Collection<java.lang.Class<?>> classes)
Imports the class files of the supplied classes from the classpath / modulepath.JavaClasses
importClasspath()
Imports classes from the whole classpath without archives (JARs or JRTs).JavaClasses
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.JavaClasses
importJar(java.util.jar.JarFile jar)
For information about the impact of the imported classes on the evaluation of rules, as well as configuration and details, refer toClassFileImporter
.JavaClasses
importJars(java.lang.Iterable<java.util.jar.JarFile> jarFiles)
Imports all Java class files in the givenJAR files
.JavaClasses
importJars(java.util.jar.JarFile... jarFiles)
Delegates toimportJars(Iterable)
JavaClasses
importLocations(java.util.Collection<Location> locations)
Imports all class files at the givenlocations
.JavaClasses
importPackages(java.lang.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
.JavaClasses
importPackages(java.util.Collection<java.lang.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
.JavaClasses
importPackagesOf(java.lang.Class<?>... classes)
Delegates toimportPackagesOf(Collection)
JavaClasses
importPackagesOf(java.util.Collection<java.lang.Class<?>> classes)
Takes the packages of the supplied classes and delegates toimportPackages(String...)
JavaClasses
importPath(java.lang.String path)
JavaClasses
importPath(java.nio.file.Path path)
Delegates toimportPaths(Collection)
JavaClasses
importPaths(java.lang.String... paths)
JavaClasses
importPaths(java.nio.file.Path... paths)
Delegates toimportPaths(Collection)
JavaClasses
importPaths(java.util.Collection<java.nio.file.Path> paths)
Imports all class files at the givenfile paths
.JavaClasses
importUrl(java.net.URL url)
JavaClasses
importUrls(java.util.Collection<java.net.URL> urls)
ClassFileImporter
withImportOption(ImportOption option)
Allows filtering the class files to import by theirLocation
(i.e.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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
@PublicAPI(usage=ACCESS) public JavaClasses importPaths(java.util.Collection<java.nio.file.Path> paths)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
@PublicAPI(usage=ACCESS) public JavaClasses importJars(java.lang.Iterable<java.util.jar.JarFile> jarFiles)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
@PublicAPI(usage=ACCESS) public JavaClasses importPackages(java.util.Collection<java.lang.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
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
@PublicAPI(usage=ACCESS) public JavaClasses importPackagesOf(java.util.Collection<java.lang.Class<?>> classes)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
@PublicAPI(usage=ACCESS) public JavaClasses importClasses(java.util.Collection<java.lang.Class<?>> classes)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
@PublicAPI(usage=ACCESS) public JavaClasses importLocations(java.util.Collection<Location> locations)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
.
-