Package com.tngtech.archunit.core.domain
Class JavaClass
- java.lang.Object
-
- com.tngtech.archunit.core.domain.JavaClass
-
- All Implemented Interfaces:
HasDescription,CanBeAnnotated,HasAnnotations,HasModifiers,HasName,HasName.AndFullName,HasSourceCodeLocation
public class JavaClass extends java.lang.Object implements HasName.AndFullName, HasAnnotations, HasModifiers, HasDescription, HasSourceCodeLocation
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJavaClass.Functionsstatic classJavaClass.Predicates-
Nested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.CanBeAnnotated
CanBeAnnotated.Utils
-
Nested classes/interfaces inherited from interface com.tngtech.archunit.core.domain.properties.HasName
HasName.AndFullName
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Set<JavaAccess<?>>getAccessesFromSelf()java.util.Set<JavaAccess<?>>getAccessesToSelf()java.util.Set<JavaAccess<?>>getAllAccessesFromSelf()java.util.Set<JavaClass>getAllClassesSelfIsAssignableTo()java.util.Set<JavaConstructor>getAllConstructors()java.util.Set<JavaField>getAllFields()java.util.Set<JavaClass>getAllInterfaces()java.util.Set<JavaMember>getAllMembers()java.util.Set<JavaMethod>getAllMethods()java.util.Set<JavaClass>getAllSubClasses()java.util.List<JavaClass>getAllSuperClasses()<A extends java.lang.annotation.Annotation>
AgetAnnotationOfType(java.lang.Class<A> type)JavaAnnotationgetAnnotationOfType(java.lang.String typeName)java.util.Set<JavaAnnotation>getAnnotations()java.util.Set<JavaCall<?>>getCallsFromSelf()Returns all calls of this class to methods or constructors.java.util.List<JavaClass>getClassHierarchy()java.util.Set<JavaCodeUnit>getCodeUnits()JavaCodeUnitgetCodeUnitWithParameterTypeNames(java.lang.String name, java.lang.String... parameters)Same asgetCodeUnitWithParameterTypes(String, Class[]), but with parameter signature specified as full class namesJavaCodeUnitgetCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters)JavaCodeUnitgetCodeUnitWithParameterTypes(java.lang.String name, java.lang.Class<?>... parameters)JavaCodeUnitgetCodeUnitWithParameterTypes(java.lang.String name, java.util.List<java.lang.Class<?>> parameters)JavaClassgetComponentType()This is a convenience method fortryGetComponentType()in cases where clients know that this type is certainly an array type and thus the component type present.JavaConstructorgetConstructor()JavaConstructorgetConstructor(java.lang.Class<?>... parameters)JavaConstructorgetConstructor(java.lang.String... parameters)java.util.Set<JavaConstructorCall>getConstructorCallsFromSelf()java.util.Set<JavaConstructorCall>getConstructorCallsToSelf()java.util.Set<JavaConstructor>getConstructors()java.util.Set<JavaConstructor>getConstructorsWithParameterTypeOfSelf()java.util.Set<ThrowsDeclaration<JavaConstructor>>getConstructorsWithThrowsDeclarationTypeOfSelf()java.lang.StringgetDescription()java.util.Set<Dependency>getDirectDependenciesFromSelf()Returns all dependencies originating directly from this class (i.e.java.util.Set<Dependency>getDirectDependenciesToSelf()LikegetDirectDependenciesFromSelf(), but instead returns all dependencies where this class is target.Optional<JavaClass>getEnclosingClass()JavaFieldgetField(java.lang.String name)java.util.Set<JavaFieldAccess>getFieldAccessesFromSelf()java.util.Set<JavaFieldAccess>getFieldAccessesToSelf()java.util.Set<JavaField>getFields()java.util.Set<JavaField>getFieldsWithTypeOfSelf()java.lang.StringgetFullName()java.util.Set<JavaClass>getInterfaces()java.util.Set<JavaMember>getMembers()JavaMethodgetMethod(java.lang.String name)JavaMethodgetMethod(java.lang.String name, java.lang.Class<?>... parameters)JavaMethodgetMethod(java.lang.String name, java.lang.String... parameters)java.util.Set<JavaMethodCall>getMethodCallsFromSelf()java.util.Set<JavaMethodCall>getMethodCallsToSelf()java.util.Set<JavaMethod>getMethods()java.util.Set<JavaMethod>getMethodsWithParameterTypeOfSelf()java.util.Set<JavaMethod>getMethodsWithReturnTypeOfSelf()java.util.Set<ThrowsDeclaration<JavaMethod>>getMethodThrowsDeclarationsWithTypeOfSelf()java.util.Set<JavaModifier>getModifiers()java.lang.StringgetName()JavaPackagegetPackage()java.lang.StringgetPackageName()java.lang.StringgetSimpleName()Optional<Source>getSource()SourceCodeLocationgetSourceCodeLocation()Optional<JavaStaticInitializer>getStaticInitializer()java.util.Set<JavaClass>getSubClasses()Optional<JavaClass>getSuperClass()booleanisAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate)booleanisAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)booleanisAnnotatedWith(java.lang.String annotationTypeName)booleanisAnonymous()booleanisArray()booleanisAssignableFrom(DescribedPredicate<? super JavaClass> predicate)booleanisAssignableFrom(java.lang.Class<?> type)booleanisAssignableFrom(java.lang.String typeName)booleanisAssignableTo(DescribedPredicate<? super JavaClass> predicate)booleanisAssignableTo(java.lang.Class<?> type)booleanisAssignableTo(java.lang.String typeName)booleanisEnum()booleanisEquivalentTo(java.lang.Class<?> clazz)booleanisInnerClass()booleanisInterface()booleanisMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate)booleanisMetaAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> type)booleanisMetaAnnotatedWith(java.lang.String typeName)booleanisNestedClass()booleanisPrimitive()static java.util.List<java.lang.String>namesOf(java.lang.Class<?>... paramTypes)static java.util.List<java.lang.String>namesOf(java.util.List<java.lang.Class<?>> paramTypes)java.lang.Class<?>reflect()Resolves the respectiveClassfrom the classpath.
NOTE: This method will throw an exception, if the respectiveClassor any of its dependencies can't be found on the classpath.java.lang.StringtoString()<A extends java.lang.annotation.Annotation>
Optional<A>tryGetAnnotationOfType(java.lang.Class<A> type)Optional<JavaAnnotation>tryGetAnnotationOfType(java.lang.String typeName)Same astryGetAnnotationOfType(Class), but takes the type name.Optional<JavaField>tryGetField(java.lang.String name)Optional<JavaMethod>tryGetMethod(java.lang.String name)Optional<JavaMethod>tryGetMethod(java.lang.String name, java.lang.Class<?>... parameters)Optional<JavaMethod>tryGetMethod(java.lang.String name, java.lang.String... parameters)
-
-
-
Method Detail
-
getSource
@PublicAPI(usage=ACCESS) public Optional<Source> getSource()
-
getSourceCodeLocation
@PublicAPI(usage=ACCESS) public SourceCodeLocation getSourceCodeLocation()
- Specified by:
getSourceCodeLocationin interfaceHasSourceCodeLocation- Returns:
- The
SourceCodeLocationof this object, i.e. how to locate the respective object within the set of source files.
-
getDescription
@PublicAPI(usage=ACCESS) public java.lang.String getDescription()
- Specified by:
getDescriptionin interfaceHasDescription
-
getName
@PublicAPI(usage=ACCESS) public java.lang.String getName()
-
getFullName
@PublicAPI(usage=ACCESS) public java.lang.String getFullName()
- Specified by:
getFullNamein interfaceHasName.AndFullName- Returns:
- The fully qualified name of this
JavaClass, i.e. the result is the same as invokinggetName()
-
getSimpleName
@PublicAPI(usage=ACCESS) public java.lang.String getSimpleName()
-
getPackage
@PublicAPI(usage=ACCESS) public JavaPackage getPackage()
-
getPackageName
@PublicAPI(usage=ACCESS) public java.lang.String getPackageName()
-
isPrimitive
@PublicAPI(usage=ACCESS) public boolean isPrimitive()
-
isInterface
@PublicAPI(usage=ACCESS) public boolean isInterface()
-
isEnum
@PublicAPI(usage=ACCESS) public boolean isEnum()
-
isArray
@PublicAPI(usage=ACCESS) public boolean isArray()
-
getComponentType
@PublicAPI(usage=ACCESS) public JavaClass getComponentType()
This is a convenience method fortryGetComponentType()in cases where clients know that this type is certainly an array type and thus the component type present.- Returns:
- The result of
tryGetComponentType() - Throws:
java.lang.IllegalArgumentException- if this class is no array
-
isNestedClass
@PublicAPI(usage=ACCESS) public boolean isNestedClass()
- Returns:
- Returns true if this class is declared within another class. Returns false for top-level classes.
-
isInnerClass
@PublicAPI(usage=ACCESS) public boolean isInnerClass()
- Returns:
- Returns true if this class is a non-static nested class. Returns false otherwise.
-
getModifiers
@PublicAPI(usage=ACCESS) public java.util.Set<JavaModifier> getModifiers()
- Specified by:
getModifiersin interfaceHasModifiers
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(java.lang.String annotationTypeName)
- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate)
- Specified by:
isAnnotatedWithin interfaceCanBeAnnotated
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> type)
- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(java.lang.String typeName)
- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation> predicate)
- Specified by:
isMetaAnnotatedWithin interfaceCanBeAnnotated
-
getAnnotationOfType
@PublicAPI(usage=ACCESS) public <A extends java.lang.annotation.Annotation> A getAnnotationOfType(java.lang.Class<A> type)
- Specified by:
getAnnotationOfTypein interfaceHasAnnotations- Parameters:
type- A given annotation type to matchJavaAnnotationsagainst- Returns:
- An
Annotationof the given annotation type - Throws:
java.lang.IllegalArgumentException- if the class is note annotated with the given type- See Also:
isAnnotatedWith(Class),tryGetAnnotationOfType(Class)
-
getAnnotationOfType
@PublicAPI(usage=ACCESS) public JavaAnnotation getAnnotationOfType(java.lang.String typeName)
- Specified by:
getAnnotationOfTypein interfaceHasAnnotations
-
getAnnotations
@PublicAPI(usage=ACCESS) public java.util.Set<JavaAnnotation> getAnnotations()
- Specified by:
getAnnotationsin interfaceHasAnnotations
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public <A extends java.lang.annotation.Annotation> Optional<A> tryGetAnnotationOfType(java.lang.Class<A> type)
- Specified by:
tryGetAnnotationOfTypein interfaceHasAnnotations- Parameters:
type- A given annotation type to matchJavaAnnotationsagainst- Returns:
- An
Optionalcontaining anAnnotationof the given annotation type, if this class is annotated with the given type, otherwise Optional.absent() - See Also:
isAnnotatedWith(Class),getAnnotationOfType(Class)
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public Optional<JavaAnnotation> tryGetAnnotationOfType(java.lang.String typeName)
Same astryGetAnnotationOfType(Class), but takes the type name.- Specified by:
tryGetAnnotationOfTypein interfaceHasAnnotations
-
getSuperClass
@PublicAPI(usage=ACCESS) public Optional<JavaClass> getSuperClass()
-
getClassHierarchy
@PublicAPI(usage=ACCESS) public java.util.List<JavaClass> getClassHierarchy()
- Returns:
- The complete class hierarchy, i.e. the class itself and the result of
getAllSuperClasses()
-
getAllSuperClasses
@PublicAPI(usage=ACCESS) public java.util.List<JavaClass> getAllSuperClasses()
- Returns:
- All super classes sorted ascending by distance in the class hierarchy, i.e. first the direct super class, then the super class of the super class and so on. Includes Object.class in the result.
-
getSubClasses
@PublicAPI(usage=ACCESS) public java.util.Set<JavaClass> getSubClasses()
-
getInterfaces
@PublicAPI(usage=ACCESS) public java.util.Set<JavaClass> getInterfaces()
-
getAllInterfaces
@PublicAPI(usage=ACCESS) public java.util.Set<JavaClass> getAllInterfaces()
-
getAllClassesSelfIsAssignableTo
@PublicAPI(usage=ACCESS) public java.util.Set<JavaClass> getAllClassesSelfIsAssignableTo()
- Returns:
- All classes, this class is assignable to, in particular
- self
- superclasses this class extends
- interfaces this class implements
-
getEnclosingClass
@PublicAPI(usage=ACCESS) public Optional<JavaClass> getEnclosingClass()
-
getAllSubClasses
@PublicAPI(usage=ACCESS) public java.util.Set<JavaClass> getAllSubClasses()
-
getMembers
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMember> getMembers()
-
getAllMembers
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMember> getAllMembers()
-
getFields
@PublicAPI(usage=ACCESS) public java.util.Set<JavaField> getFields()
-
getAllFields
@PublicAPI(usage=ACCESS) public java.util.Set<JavaField> getAllFields()
-
getField
@PublicAPI(usage=ACCESS) public JavaField getField(java.lang.String name)
-
tryGetField
@PublicAPI(usage=ACCESS) public Optional<JavaField> tryGetField(java.lang.String name)
-
getCodeUnits
@PublicAPI(usage=ACCESS) public java.util.Set<JavaCodeUnit> getCodeUnits()
-
getCodeUnitWithParameterTypes
@PublicAPI(usage=ACCESS) public JavaCodeUnit getCodeUnitWithParameterTypes(java.lang.String name, java.lang.Class<?>... parameters)
- Parameters:
name- The name of the code unit, can be a method name, but alsoCONSTRUCTOR_NAMEorSTATIC_INITIALIZER_NAMEparameters- The parameter signature of the method specified asClassObjects- Returns:
- A code unit (method, constructor or static initializer) with the given signature
-
getCodeUnitWithParameterTypeNames
@PublicAPI(usage=ACCESS) public JavaCodeUnit getCodeUnitWithParameterTypeNames(java.lang.String name, java.lang.String... parameters)
Same asgetCodeUnitWithParameterTypes(String, Class[]), but with parameter signature specified as full class names
-
getCodeUnitWithParameterTypes
@PublicAPI(usage=ACCESS) public JavaCodeUnit getCodeUnitWithParameterTypes(java.lang.String name, java.util.List<java.lang.Class<?>> parameters)
-
getCodeUnitWithParameterTypeNames
@PublicAPI(usage=ACCESS) public JavaCodeUnit getCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters)
-
getMethod
@PublicAPI(usage=ACCESS) public JavaMethod getMethod(java.lang.String name)
-
getMethod
@PublicAPI(usage=ACCESS) public JavaMethod getMethod(java.lang.String name, java.lang.Class<?>... parameters)
-
getMethod
@PublicAPI(usage=ACCESS) public JavaMethod getMethod(java.lang.String name, java.lang.String... parameters)
-
tryGetMethod
@PublicAPI(usage=ACCESS) public Optional<JavaMethod> tryGetMethod(java.lang.String name)
-
tryGetMethod
@PublicAPI(usage=ACCESS) public Optional<JavaMethod> tryGetMethod(java.lang.String name, java.lang.Class<?>... parameters)
-
tryGetMethod
@PublicAPI(usage=ACCESS) public Optional<JavaMethod> tryGetMethod(java.lang.String name, java.lang.String... parameters)
-
getMethods
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethod> getMethods()
-
getAllMethods
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethod> getAllMethods()
-
getConstructor
@PublicAPI(usage=ACCESS) public JavaConstructor getConstructor()
-
getConstructor
@PublicAPI(usage=ACCESS) public JavaConstructor getConstructor(java.lang.Class<?>... parameters)
-
getConstructor
@PublicAPI(usage=ACCESS) public JavaConstructor getConstructor(java.lang.String... parameters)
-
getConstructors
@PublicAPI(usage=ACCESS) public java.util.Set<JavaConstructor> getConstructors()
-
getAllConstructors
@PublicAPI(usage=ACCESS) public java.util.Set<JavaConstructor> getAllConstructors()
-
getStaticInitializer
@PublicAPI(usage=ACCESS) public Optional<JavaStaticInitializer> getStaticInitializer()
-
getAccessesFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaAccess<?>> getAccessesFromSelf()
-
getAllAccessesFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaAccess<?>> getAllAccessesFromSelf()
- Returns:
- Set of all
JavaAccessin the class hierarchy, as opposed to the accesses this class directly performs.
-
getFieldAccessesFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaFieldAccess> getFieldAccessesFromSelf()
-
getCallsFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaCall<?>> getCallsFromSelf()
Returns all calls of this class to methods or constructors.
-
getMethodCallsFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethodCall> getMethodCallsFromSelf()
-
getConstructorCallsFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaConstructorCall> getConstructorCallsFromSelf()
-
getDirectDependenciesFromSelf
@PublicAPI(usage=ACCESS) public java.util.Set<Dependency> getDirectDependenciesFromSelf()
Returns all dependencies originating directly from this class (i.e. not just from a superclass), where a dependency can be- field access
- method call
- constructor call
- extending a class
- implementing an interface
- referencing in throws declaration
- Returns:
- All dependencies originating directly from this class (i.e. where this class is the origin)
-
getDirectDependenciesToSelf
@PublicAPI(usage=ACCESS) public java.util.Set<Dependency> getDirectDependenciesToSelf()
LikegetDirectDependenciesFromSelf(), but instead returns all dependencies where this class is target.- Returns:
- Dependencies where this class is the target.
-
getFieldAccessesToSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaFieldAccess> getFieldAccessesToSelf()
-
getMethodCallsToSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethodCall> getMethodCallsToSelf()
-
getConstructorCallsToSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaConstructorCall> getConstructorCallsToSelf()
-
getAccessesToSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaAccess<?>> getAccessesToSelf()
-
getFieldsWithTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaField> getFieldsWithTypeOfSelf()
- Returns:
- Fields of all imported classes that have the type of this class.
-
getMethodsWithParameterTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethod> getMethodsWithParameterTypeOfSelf()
- Returns:
- Methods of all imported classes that have a parameter type of this class.
-
getMethodsWithReturnTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaMethod> getMethodsWithReturnTypeOfSelf()
- Returns:
- Methods of all imported classes that have a return type of this class.
-
getMethodThrowsDeclarationsWithTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<ThrowsDeclaration<JavaMethod>> getMethodThrowsDeclarationsWithTypeOfSelf()
- Returns:
ThrowsDeclarationsof all imported classes that have the type of this class.
-
getConstructorsWithParameterTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaConstructor> getConstructorsWithParameterTypeOfSelf()
- Returns:
- Constructors of all imported classes that have a parameter type of this class.
-
getConstructorsWithThrowsDeclarationTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<ThrowsDeclaration<JavaConstructor>> getConstructorsWithThrowsDeclarationTypeOfSelf()
- Returns:
ThrowsDeclarationsof all imported classes that have the type of this class.
-
isEquivalentTo
@PublicAPI(usage=ACCESS) public boolean isEquivalentTo(java.lang.Class<?> clazz)
- Parameters:
clazz- An arbitrary type- Returns:
- true, if this
JavaClassrepresents the same class as the suppliedClass, otherwise false
-
isAssignableFrom
@PublicAPI(usage=ACCESS) public boolean isAssignableFrom(java.lang.Class<?> type)
-
isAssignableFrom
@PublicAPI(usage=ACCESS) public boolean isAssignableFrom(java.lang.String typeName)
-
isAssignableFrom
@PublicAPI(usage=ACCESS) public boolean isAssignableFrom(DescribedPredicate<? super JavaClass> predicate)
-
isAssignableTo
@PublicAPI(usage=ACCESS) public boolean isAssignableTo(java.lang.Class<?> type)
-
isAssignableTo
@PublicAPI(usage=ACCESS) public boolean isAssignableTo(java.lang.String typeName)
-
isAssignableTo
@PublicAPI(usage=ACCESS) public boolean isAssignableTo(DescribedPredicate<? super JavaClass> predicate)
-
reflect
@PublicAPI(usage=ACCESS) public java.lang.Class<?> reflect()
Resolves the respectiveClassfrom the classpath.
NOTE: This method will throw an exception, if the respectiveClassor any of its dependencies can't be found on the classpath.- Returns:
- The
Classequivalent to thisJavaClass
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
namesOf
@PublicAPI(usage=ACCESS) public static java.util.List<java.lang.String> namesOf(java.lang.Class<?>... paramTypes)
-
namesOf
@PublicAPI(usage=ACCESS) public static java.util.List<java.lang.String> namesOf(java.util.List<java.lang.Class<?>> paramTypes)
-
isAnonymous
@PublicAPI(usage=ACCESS) public boolean isAnonymous()
-
-