Class ClassFileImporter


  • public final class ClassFileImporter
    extends java.lang.Object
    The central API to import JavaClasses. 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 just 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. The stub class B will also miss information like "accesses to self" and similar. 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).

    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. ArchUnit provides a second ClassResolver to import referenced classes more widely, namely the ClassResolverFromClasspath. This resolver will 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 ClassResolverFromClasspath can easily be activated by setting
    "resolveMissingDependenciesFromClassPath"=true
    within your "/archunit.properties". For further information consult the ArchUnit user guide.
    See Also:
    ArchConfiguration