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
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic final classPredefinedfunctionsto transformJavaPackage.static interfaceNested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.CanBeAnnotated
CanBeAnnotated.Predicates, CanBeAnnotated.UtilsNested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.HasName
HasName.AndFullName, HasName.Predicates, HasName.Utils -
Method Summary
Modifier and TypeMethodDescriptionbooleancontainsClass(JavaClass clazz) booleancontainsClass(Class<?> clazz) booleancontainsClassWithFullyQualifiedName(String className) booleancontainsClassWithSimpleName(String className) booleancontainsPackage(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) booleanisAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returnstrue, if this element is annotated with an annotation matching the given predicate.booleanisAnnotatedWith(Class<? extends Annotation> annotationType) Returnstrue, if this element is annotated with the given annotation type.booleanisAnnotatedWith(String annotationTypeName) booleanisMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returnstrue, if this element is meta-annotated with an annotation matching the given predicate.booleanisMetaAnnotatedWith(Class<? extends Annotation> annotationType) Returnstrue, if this element is meta-annotated with the given annotation type.booleanisMetaAnnotatedWith(String annotationTypeName) toString()voidtraversePackageTree(Predicate<? super JavaClass> predicate, JavaPackage.ClassVisitor visitor) Traverses the package tree visiting each matching class.voidtraversePackageTree(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.
langfor packagejava.lang
-
getPackageInfo
- Returns:
- The
JavaClassrepresenting the compiledpackage-info.classfile of thisJavaPackage(for details refer to the Java Language Specification). Will throw anIllegalArgumentExceptionif nopackage-infoexists in this package.
-
tryGetPackageInfo
- Returns:
- The
JavaClassrepresenting the compiledpackage-info.classfile of thisJavaPackageorOptional.empty()if nopackage-infoexists in this package (for details refer to the Java Language Specification).
-
getAnnotations
- Specified by:
getAnnotationsin interfaceHasAnnotations<JavaPackage>- Returns:
- All annotations on the compiled
package-info.classfile (for details refer to the Java Language Specification).
-
getAnnotationOfType
- Specified by:
getAnnotationOfTypein interfaceHasAnnotations<JavaPackage>- Type Parameters:
A- The type of theAnnotationto retrieve- Parameters:
type- TheClassof theAnnotationto retrieve.- Returns:
- The
Annotationof the given type on thepackage-info.classof this package (for details refer to the Java Language Specification). Will throw anIllegalArgumentExceptionif either there is nopackage-infoor thepackage-infois not annotated with the respective annotation type. - See Also:
-
getAnnotationOfType
- Specified by:
getAnnotationOfTypein interfaceHasAnnotations<JavaPackage>- Parameters:
typeName- The fully qualified class name of theAnnotationtype to retrieve.- Returns:
- The
JavaAnnotationmatching the given type on thepackage-info.classof this package (for details refer to the Java Language Specification). Will throw anIllegalArgumentExceptionif either there is nopackage-infoor thepackage-infois 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:
tryGetAnnotationOfTypein interfaceHasAnnotations<JavaPackage>- Type Parameters:
A- The type of theAnnotationto retrieve- Parameters:
type- TheClassof theAnnotationto retrieve.- Returns:
- The
Annotationof the given type on thepackage-info.classof this package orOptional.empty()if either there is nopackage-infoor thepackage-infois not annotated with the respective annotation type. - See Also:
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public Optional<JavaAnnotation<JavaPackage>> tryGetAnnotationOfType(String typeName) - Specified by:
tryGetAnnotationOfTypein interfaceHasAnnotations<JavaPackage>- Parameters:
typeName- The fully qualified class name of theAnnotationtype to retrieve.- Returns:
- The
JavaAnnotationmatching the given type on thepackage-info.classof this package orOptional.empty()if either there is nopackage-infoor thepackage-infois not annotated with the respective annotation type. - See Also:
-
isAnnotatedWith
Description copied from interface:CanBeAnnotatedReturnstrue, if this element is annotated with the given annotation type.- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated- Parameters:
annotationType- The type of the annotation to check for- Returns:
trueif and only if there is apackage-info.classin this package that is annotated with anAnnotationof the given type.
-
isAnnotatedWith
- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated- Parameters:
annotationTypeName- Fully qualified class name of a specific type ofAnnotation- Returns:
trueif and only if there is apackage-info.classin this package that is annotated with anAnnotationof the given type.- See Also:
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Description copied from interface:CanBeAnnotatedReturnstrue, if this element is annotated with an annotation matching the given predicate.- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated- Parameters:
predicate- Qualifies matching annotations- Returns:
trueif and only if there is apackage-info.classin this package that is annotated with anAnnotationmatching the given predicate.
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(Class<? extends Annotation> annotationType) Description copied from interface:CanBeAnnotatedReturnstrue, 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
trueif this element is directly annotated with the given annotation type.- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated- Parameters:
annotationType- The type of the annotation to check for- Returns:
trueif and only if there is apackage-info.classin this package that is meta-annotated with anAnnotationof the given type. A meta-annotation is an annotation that is declared on another annotation.This method also returns
trueif this element is directly annotated with the given annotation type.
-
isMetaAnnotatedWith
- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated- Parameters:
annotationTypeName- Fully qualified class name of a specific type ofAnnotation- Returns:
trueif and only if there is apackage-info.classin this package that is meta-annotated with anAnnotationof the given type. A meta-annotation is an annotation that is declared on another annotation.This method also returns
trueif 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:CanBeAnnotatedReturnstrue, 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
trueif this element is directly annotated with an annotation matching the given predicate.- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated- Parameters:
predicate- Qualifies matching annotations- Returns:
trueif and only if there is apackage-info.classin this package that is annotated with anAnnotationmatching the given predicate. A meta-annotation is an annotation that is declared on another annotation.This method also returns
trueif this element is directly annotated with the given annotation type.
-
getParent
- Returns:
- the parent package, e.g.
javafor 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
javathis 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
javathis 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:
trueif this package (directly) contains aJavaClasswith 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:
trueif this package (directly) contains aJavaClasswith 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
dependenciesthat originate from aJavaClass(directly) within this package to aJavaClassoutside of this package. For dependencies from the package tree (this package, subpackages, subpackages of subpackages, etc.) please refer togetClassDependenciesFromThisPackageTree(). - See Also:
-
getClassDependenciesFromThisPackageTree
- Returns:
- All
dependenciesthat originate from aJavaClasswithin this package tree (this package, subpackages, subpackages of subpackages, etc.) to aJavaClassoutside of this package tree. To limit this to dependencies that originate (directly) from this package please refer togetClassDependenciesFromThisPackage(). - See Also:
-
getClassDependenciesToThisPackage
- Returns:
- All
dependenciesthat originate from aJavaClassoutside 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
dependenciesthat originate from aJavaClassoutside of this package tree (this package, subpackages, subpackages of subpackages, etc.) to aJavaClasswithin this package tree. To limit this to dependencies that directly target this package refer togetClassDependenciesToThisPackage(). - See Also:
-
getPackageDependenciesFromThisPackage
- Returns:
- All
packagesthat this package has a dependency on. I.e. allpackagesthat 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
packagesthat this package tree (this package, subpackages, subpackages of subpackages, etc.) has a dependency on. I.e. allpackagesthat 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
packagesthat have a dependency on this package. I.e. allpackagesthat 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
packagesthat have a dependency on this package tree (this package, subpackages, subpackages of subpackages, etc.). I.e. allpackagesthat 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:
getDescriptionin interfaceHasDescription
-
toString
-