Package com.tngtech.archunit.core.domain
Class JavaPackage
java.lang.Object
com.tngtech.archunit.core.domain.JavaPackage
- All Implemented Interfaces:
HasDescription
,CanBeAnnotated
,HasAnnotations<JavaPackage>
,HasName
@PublicAPI(usage=ACCESS)
public final class JavaPackage
extends Object
implements HasName, HasAnnotations<JavaPackage>
Represents a package of Java classes as defined by the
Java Language Specification.
I.e. a namespace/group for related classes, where each package can also contain further subpackages.
Thus, packages define a hierarchical tree-like structure.
An example would be the package
ArchUnit will consider the "classes of a package" to be the classes residing directly within the package. Furthermore, "subpackages" of a package are considered packages that are residing directly within this package. On the contrary, ArchUnit will call the hierarchical tree-like structure consisting of all packages that can be reached by traversing subpackages, subpackages of subpackages, etc., as "package tree".
Take for example the classes
The package tree of
An example would be the package
java.lang
which contains java.lang.Object
.ArchUnit will consider the "classes of a package" to be the classes residing directly within the package. Furthermore, "subpackages" of a package are considered packages that are residing directly within this package. On the contrary, ArchUnit will call the hierarchical tree-like structure consisting of all packages that can be reached by traversing subpackages, subpackages of subpackages, etc., as "package tree".
Take for example the classes
com.example.TopLevel
com.example.first.First
com.example.first.nested.FirstNested
com.example.first.nested.deeper_nested.FirstDeeperNested
com.example.second.Second
com.example.second.nested.SecondNested
Then the package com.example
would contain only the class com.example.TopLevel
. It would also
contain two subpackages com.example.first
and com.example.second
(but not com.example.first.nested
as that is not directly contained within com.example
).The package tree of
com.example
would contain all packages (and classes within)
com.example
com.example.first
com.example.first.nested
com.example.first.nested.deeper_nested
com.example.second
com.example.second.nested
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static final class
Predefinedfunctions
to transformJavaPackage
.static interface
Nested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.CanBeAnnotated
CanBeAnnotated.Predicates, CanBeAnnotated.Utils
Nested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.HasName
HasName.AndFullName, HasName.Predicates, HasName.Utils
-
Method Summary
Modifier and TypeMethodDescriptionboolean
containsClass
(JavaClass clazz) boolean
containsClass
(Class<?> clazz) boolean
containsClassWithFullyQualifiedName
(String className) boolean
containsClassWithSimpleName
(String className) boolean
containsPackage
(String packageName) <A extends Annotation>
AgetAnnotationOfType
(Class<A> type) getAnnotationOfType
(String typeName) Set<? extends JavaAnnotation<JavaPackage>>
getClassWithFullyQualifiedName
(String className) getClassWithSimpleName
(String className) getName()
getPackage
(String packageName) boolean
isAnnotatedWith
(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returnstrue
, if this element is annotated with an annotation matching the given predicate.boolean
isAnnotatedWith
(Class<? extends Annotation> annotationType) Returnstrue
, if this element is annotated with the given annotation type.boolean
isAnnotatedWith
(String annotationTypeName) boolean
isMetaAnnotatedWith
(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returnstrue
, if this element is meta-annotated with an annotation matching the given predicate.boolean
isMetaAnnotatedWith
(Class<? extends Annotation> annotationType) Returnstrue
, if this element is meta-annotated with the given annotation type.boolean
isMetaAnnotatedWith
(String annotationTypeName) toString()
void
traversePackageTree
(Predicate<? super JavaClass> predicate, JavaPackage.ClassVisitor visitor) Traverses the package tree visiting each matching class.void
traversePackageTree
(Predicate<? super JavaPackage> predicate, JavaPackage.PackageVisitor visitor) Traverses the package tree visiting each matching package.<A extends Annotation>
Optional<A>tryGetAnnotationOfType
(Class<A> type) tryGetAnnotationOfType
(String typeName) Optional<? extends HasAnnotations<?>>
-
Method Details
-
getName
-
getRelativeName
- Returns:
- the (relative) name of this package, e.g.
lang
for packagejava.lang
-
getPackageInfo
- Returns:
- The
JavaClass
representing the compiledpackage-info.class
file of thisJavaPackage
(for details refer to the Java Language Specification). Will throw anIllegalArgumentException
if nopackage-info
exists in this package.
-
tryGetPackageInfo
- Returns:
- The
JavaClass
representing the compiledpackage-info.class
file of thisJavaPackage
orOptional.empty()
if nopackage-info
exists in this package (for details refer to the Java Language Specification).
-
getAnnotations
- Specified by:
getAnnotations
in interfaceHasAnnotations<JavaPackage>
- Returns:
- All annotations on the compiled
package-info.class
file (for details refer to the Java Language Specification).
-
getAnnotationOfType
- Specified by:
getAnnotationOfType
in interfaceHasAnnotations<JavaPackage>
- Type Parameters:
A
- The type of theAnnotation
to retrieve- Parameters:
type
- TheClass
of theAnnotation
to retrieve.- Returns:
- The
Annotation
of the given type on thepackage-info.class
of this package (for details refer to the Java Language Specification). Will throw anIllegalArgumentException
if either there is nopackage-info
or thepackage-info
is not annotated with the respective annotation type. - See Also:
-
getAnnotationOfType
- Specified by:
getAnnotationOfType
in interfaceHasAnnotations<JavaPackage>
- Parameters:
typeName
- The fully qualified class name of theAnnotation
type to retrieve.- Returns:
- The
JavaAnnotation
matching the given type on thepackage-info.class
of this package (for details refer to the Java Language Specification). Will throw anIllegalArgumentException
if either there is nopackage-info
or thepackage-info
is not annotated with the respective annotation type. - See Also:
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public <A extends Annotation> Optional<A> tryGetAnnotationOfType(Class<A> type) - Specified by:
tryGetAnnotationOfType
in interfaceHasAnnotations<JavaPackage>
- Type Parameters:
A
- The type of theAnnotation
to retrieve- Parameters:
type
- TheClass
of theAnnotation
to retrieve.- Returns:
- The
Annotation
of the given type on thepackage-info.class
of this package orOptional.empty()
if either there is nopackage-info
or thepackage-info
is not annotated with the respective annotation type. - See Also:
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public Optional<JavaAnnotation<JavaPackage>> tryGetAnnotationOfType(String typeName) - Specified by:
tryGetAnnotationOfType
in interfaceHasAnnotations<JavaPackage>
- Parameters:
typeName
- The fully qualified class name of theAnnotation
type to retrieve.- Returns:
- The
JavaAnnotation
matching the given type on thepackage-info.class
of this package orOptional.empty()
if either there is nopackage-info
or thepackage-info
is not annotated with the respective annotation type. - See Also:
-
isAnnotatedWith
Description copied from interface:CanBeAnnotated
Returnstrue
, if this element is annotated with the given annotation type.- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationType
- The type of the annotation to check for- Returns:
true
if and only if there is apackage-info.class
in this package that is annotated with anAnnotation
of the given type.
-
isAnnotatedWith
- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationTypeName
- Fully qualified class name of a specific type ofAnnotation
- Returns:
true
if and only if there is apackage-info.class
in this package that is annotated with anAnnotation
of the given type.- See Also:
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Description copied from interface:CanBeAnnotated
Returnstrue
, if this element is annotated with an annotation matching the given predicate.- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
predicate
- Qualifies matching annotations- Returns:
true
if and only if there is apackage-info.class
in this package that is annotated with anAnnotation
matching the given predicate.
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(Class<? extends Annotation> annotationType) Description copied from interface:CanBeAnnotated
Returnstrue
, if this element is meta-annotated with the given annotation type. A meta-annotation is an annotation that is declared on another annotation.This method also returns
true
if this element is directly annotated with the given annotation type.- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationType
- The type of the annotation to check for- Returns:
true
if and only if there is apackage-info.class
in this package that is meta-annotated with anAnnotation
of the given type. A meta-annotation is an annotation that is declared on another annotation.This method also returns
true
if this element is directly annotated with the given annotation type.
-
isMetaAnnotatedWith
- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationTypeName
- Fully qualified class name of a specific type ofAnnotation
- Returns:
true
if and only if there is apackage-info.class
in this package that is meta-annotated with anAnnotation
of the given type. A meta-annotation is an annotation that is declared on another annotation.This method also returns
true
if this element is directly annotated with the given annotation type.- See Also:
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Description copied from interface:CanBeAnnotated
Returnstrue
, if this element is meta-annotated with an annotation matching the given predicate. A meta-annotation is an annotation that is declared on another annotation.This method also returns
true
if this element is directly annotated with an annotation matching the given predicate.- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
predicate
- Qualifies matching annotations- Returns:
true
if and only if there is apackage-info.class
in this package that is annotated with anAnnotation
matching the given predicate. A meta-annotation is an annotation that is declared on another annotation.This method also returns
true
if this element is directly annotated with the given annotation type.
-
getParent
- Returns:
- the parent package, e.g.
java
for packagejava.lang
-
getClasses
- Returns:
- all classes directly contained in this package, but not classes in the lower levels of the package tree (compare
getClassesInPackageTree()
)
-
getClassesInPackageTree
- Returns:
- all classes contained in this
package tree
, i.e. all classes in this package, subpackages, subpackages of subpackages, and so on (comparegetClasses()
)
-
getSubpackages
- Returns:
- all (direct) subpackages contained in this package, e.g. for package
java
this would be[java.lang, java.io, ...]
(comparegetSubpackagesInTree()
)
-
getSubpackagesInTree
- Returns:
- all subpackages contained in the package tree of this package. I.e. all subpackages, subpackages
of subpackages, and so on. For package
java
this would be[java.lang, java.lang.annotation, java.util, java.util.concurrent, ...]
(comparegetSubpackages()
)
-
containsClass
-
containsClass
-
getClass
- Parameters:
clazz
- A JavaClass
- Returns:
- the class if (directly) contained in this package, otherwise an Exception is thrown
- See Also:
-
containsClassWithFullyQualifiedName
- Parameters:
className
- fully qualified name of a Java class- Returns:
true
if this package (directly) contains aJavaClass
with the given fully qualified name- See Also:
-
getClassWithFullyQualifiedName
- Parameters:
className
- fully qualified name of a Java class- Returns:
- the class if (directly) contained in this package, otherwise an Exception is thrown
- See Also:
-
containsClassWithSimpleName
- Parameters:
className
- simple name of a Java class- Returns:
true
if this package (directly) contains aJavaClass
with the given simple name- See Also:
-
getClassWithSimpleName
- Parameters:
className
- simple name of a Java class- Returns:
- the class if (directly) contained in this package, otherwise an Exception is thrown
- See Also:
-
containsPackage
- Parameters:
packageName
- (relative) name of a package, may consist of several parts, e.g.some.subpackage
- Returns:
- true if this package contains the supplied (sub-)package with the given (relative) name
- See Also:
-
getPackage
- Parameters:
packageName
- (relative) name of a package, may consist of several parts, e.g.some.subpackage
- Returns:
- the (sub-)package with the given (relative) name; throws an exception if there is no such package contained
- See Also:
-
getClassDependenciesFromThisPackage
- Returns:
- All
dependencies
that originate from aJavaClass
(directly) within this package to aJavaClass
outside of this package. For dependencies from the package tree (this package, subpackages, subpackages of subpackages, etc.) please refer togetClassDependenciesFromThisPackageTree()
. - See Also:
-
getClassDependenciesFromThisPackageTree
- Returns:
- All
dependencies
that originate from aJavaClass
within this package tree (this package, subpackages, subpackages of subpackages, etc.) to aJavaClass
outside of this package tree. To limit this to dependencies that originate (directly) from this package please refer togetClassDependenciesFromThisPackage()
. - See Also:
-
getClassDependenciesToThisPackage
- Returns:
- All
dependencies
that originate from aJavaClass
outside of this package to aJavaClass
(directly) within this package. For dependencies to this package tree (this package, subpackages, subpackages of subpackages, etc.) please refer togetClassDependenciesToThisPackageTree()
. - See Also:
-
getClassDependenciesToThisPackageTree
- Returns:
- All
dependencies
that originate from aJavaClass
outside of this package tree (this package, subpackages, subpackages of subpackages, etc.) to aJavaClass
within this package tree. To limit this to dependencies that directly target this package refer togetClassDependenciesToThisPackage()
. - See Also:
-
getPackageDependenciesFromThisPackage
- Returns:
- All
packages
that this package has a dependency on. I.e. allpackages
that contain a class such that a class (directly) in this package depends on that class. For example
For dependencies to all packages that any class in this package tree (this package, subpackages, subpackages of subpackages, etc.) depends on refer togetPackageDependenciesFromThisPackageTree()
. - See Also:
-
getPackageDependenciesFromThisPackageTree
- Returns:
- All
packages
that this package tree (this package, subpackages, subpackages of subpackages, etc.) has a dependency on. I.e. allpackages
that contain a class such that a class in this package tree depends on that class. For example
To limit this to only those packages that classes (directly) in this package depend on refer togetPackageDependenciesFromThisPackage()
. - See Also:
-
getPackageDependenciesToThisPackage
- Returns:
- All
packages
that have a dependency on this package. I.e. allpackages
that contain a class that depends on a class (directly) in this package. For example
For dependencies from all packages that depend on any class in this package tree (this package, subpackages, subpackages of subpackages, etc.) refer togetPackageDependenciesToThisPackageTree()
. - See Also:
-
getPackageDependenciesToThisPackageTree
- Returns:
- All
packages
that have a dependency on this package tree (this package, subpackages, subpackages of subpackages, etc.). I.e. allpackages
that contain a class that depends on a class in this package tree. For example
To limit this to only those packages that depend on classes (directly) in this package refer togetPackageDependenciesToThisPackage()
. - See Also:
-
traversePackageTree
@PublicAPI(usage=ACCESS) public void traversePackageTree(Predicate<? super JavaClass> predicate, JavaPackage.ClassVisitor visitor) Traverses the package tree visiting each matching class.- Parameters:
predicate
- determines which classes within the package tree should be visitedvisitor
- will receive each class in the package tree matching the given predicate- See Also:
-
traversePackageTree
@PublicAPI(usage=ACCESS) public void traversePackageTree(Predicate<? super JavaPackage> predicate, JavaPackage.PackageVisitor visitor) Traverses the package tree visiting each matching package.- Parameters:
predicate
- determines which packages within the package tree should be visitedvisitor
- will receive each package in the package tree matching the given predicate- See Also:
-
getDescription
- Specified by:
getDescription
in interfaceHasDescription
-
toString
-