Package com.tngtech.archunit.core.domain
Class JavaClass
java.lang.Object
com.tngtech.archunit.core.domain.JavaClass
- All Implemented Interfaces:
HasDescription
,JavaType
,CanBeAnnotated
,HasAnnotations<JavaClass>
,HasModifiers
,HasName
,HasName.AndFullName
,HasSourceCodeLocation
,HasTypeParameters<JavaClass>
public class JavaClass extends java.lang.Object implements JavaType, HasName.AndFullName, HasTypeParameters<JavaClass>, HasAnnotations<JavaClass>, HasModifiers, HasSourceCodeLocation
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JavaClass.Functions
static class
JavaClass.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, HasName.Utils
-
Method Summary
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<JavaMember>
getAllMembers()
java.util.Set<JavaMethod>
getAllMethods()
java.util.Set<JavaClass>
getAllRawInterfaces()
java.util.List<JavaClass>
getAllRawSuperclasses()
java.util.Set<JavaClass>
getAllSubclasses()
java.util.Set<JavaClass>
getAllSubClasses()
Deprecated.java.util.List<JavaClass>
getAllSuperClasses()
Deprecated.UsegetAllRawSuperclasses()
instead.<A extends java.lang.annotation.Annotation>
AgetAnnotationOfType(java.lang.Class<A> type)
JavaAnnotation<JavaClass>
getAnnotationOfType(java.lang.String typeName)
java.util.Set<JavaAnnotation<JavaClass>>
getAnnotations()
java.util.Set<JavaAnnotation<?>>
getAnnotationsWithParameterTypeOfSelf()
java.util.Set<JavaAnnotation<?>>
getAnnotationsWithTypeOfSelf()
JavaClass
getBaseComponentType()
The base component type is the class'component type
if it is a one-dimensional array, the repeated application ofgetComponentType()
if it is a multi-dimensional array, or the class itself if it is no array.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()
JavaCodeUnit
getCodeUnitWithParameterTypeNames(java.lang.String name, java.lang.String... parameters)
Same asgetCodeUnitWithParameterTypes(String, Class[])
, but with parameter signature specified as full class namesJavaCodeUnit
getCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters)
JavaCodeUnit
getCodeUnitWithParameterTypes(java.lang.String name, java.lang.Class<?>... parameters)
JavaCodeUnit
getCodeUnitWithParameterTypes(java.lang.String name, java.util.List<java.lang.Class<?>> parameters)
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.JavaConstructor
getConstructor()
JavaConstructor
getConstructor(java.lang.Class<?>... parameters)
JavaConstructor
getConstructor(java.lang.String... parameters)
Same asgetConstructor(Class[])
, but with parameter signature specified as full class names.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.String
getDescription()
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()
Returns the enclosing class if this class is nested within another class.Optional<JavaCodeUnit>
getEnclosingCodeUnit()
Returns the enclosingJavaCodeUnit
if this class is declared within the context of aJavaCodeUnit
, e.g.JavaEnumConstant
getEnumConstant(java.lang.String name)
java.util.Set<JavaEnumConstant>
getEnumConstants()
JavaField
getField(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.String
getFullName()
java.util.Set<InstanceofCheck>
getInstanceofChecksWithTypeOfSelf()
java.util.Set<JavaType>
getInterfaces()
java.util.Set<JavaMember>
getMembers()
JavaMethod
getMethod(java.lang.String name)
JavaMethod
getMethod(java.lang.String name, java.lang.Class<?>... parameters)
JavaMethod
getMethod(java.lang.String name, java.lang.String... parameters)
Same asgetMethod(String, Class[])
, but with parameter signature specified as fully qualified class names.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.String
getName()
JavaPackage
getPackage()
java.lang.String
getPackageName()
java.util.Set<JavaClass>
getRawInterfaces()
Optional<JavaClass>
getRawSuperclass()
java.util.Set<ReferencedClassObject>
getReferencedClassObjects()
java.lang.String
getSimpleName()
Optional<Source>
getSource()
SourceCodeLocation
getSourceCodeLocation()
Optional<JavaStaticInitializer>
getStaticInitializer()
java.util.Set<JavaClass>
getSubclasses()
java.util.Set<JavaClass>
getSubClasses()
Deprecated.UsegetSubclasses()
instead.Optional<JavaType>
getSuperclass()
Optional<JavaClass>
getSuperClass()
Deprecated.UsegetRawSuperclass()
insteadjava.util.Set<Dependency>
getTransitiveDependenciesFromSelf()
Returns the transitive closure of all dependencies originating from this class, i.e.java.util.List<JavaTypeVariable<JavaClass>>
getTypeParameters()
boolean
isAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate)
boolean
isAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)
boolean
isAnnotatedWith(java.lang.String annotationTypeName)
boolean
isAnnotation()
boolean
isAnonymous()
Deprecated.useisAnonymousClass()
insteadboolean
isAnonymousClass()
An anonymous class is an inner class that is automatically derived from a class creation expression with a declared class body, e.g.boolean
isArray()
boolean
isAssignableFrom(DescribedPredicate<? super JavaClass> predicate)
boolean
isAssignableFrom(java.lang.Class<?> type)
boolean
isAssignableFrom(java.lang.String typeName)
boolean
isAssignableTo(DescribedPredicate<? super JavaClass> predicate)
boolean
isAssignableTo(java.lang.Class<?> type)
boolean
isAssignableTo(java.lang.String typeName)
boolean
isEnum()
boolean
isEquivalentTo(java.lang.Class<?> clazz)
boolean
isFullyImported()
boolean
isInnerClass()
An inner class is a nested class that is not explicitly or implicitly declared static.
Example:boolean
isInterface()
boolean
isLocalClass()
A local class is a nested class that is not a member of any class and that has a name.
Example:boolean
isMemberClass()
A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration.
Example:boolean
isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate)
boolean
isMetaAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> type)
boolean
isMetaAnnotatedWith(java.lang.String typeName)
boolean
isNestedClass()
A nested class is any class whose declaration occurs within the body of another class or interface.
Example:boolean
isPrimitive()
boolean
isRecord()
Returns whether this class is a record according to the Java Language Specification.boolean
isTopLevelClass()
A top level class is a class that is not a nested class, i.e.static java.util.List<java.lang.String>
namesOf(java.lang.Class<?>... paramTypes)
Deprecated.useFormatters.formatNamesOf(Class[])
insteadstatic java.util.List<java.lang.String>
namesOf(java.lang.Iterable<java.lang.Class<?>> paramTypes)
Deprecated.useFormatters.formatNamesOf(Iterable)
insteadjava.lang.Class<?>
reflect()
Resolves the respectiveClass
from the classpath.
NOTE: This method will throw an exception, if the respectiveClass
or any of its dependencies can't be found on the classpath.JavaClass
toErasure()
Converts thisJavaType
into the erased type (compare the Java Language Specification).java.lang.String
toString()
<A extends java.lang.annotation.Annotation>
Optional<A>tryGetAnnotationOfType(java.lang.Class<A> type)
Optional<JavaAnnotation<JavaClass>>
tryGetAnnotationOfType(java.lang.String typeName)
Same astryGetAnnotationOfType(Class)
, but takes the type name.Optional<JavaCodeUnit>
tryGetCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters)
Same asgetCodeUnitWithParameterTypeNames(String, List)
, but will returnOptional.absent()
if there is no suchJavaCodeUnit
.Optional<JavaCodeUnit>
tryGetCodeUnitWithParameterTypes(java.lang.String name, java.util.List<java.lang.Class<?>> parameters)
Same asgetCodeUnitWithParameterTypes(String, List)
, but will returnOptional.absent()
if there is no suchJavaCodeUnit
.Optional<JavaClass>
tryGetComponentType()
Returns the component type of this class, if this class is an array, otherwiseOptional.absent()
.Optional<JavaConstructor>
tryGetConstructor()
Optional<JavaConstructor>
tryGetConstructor(java.lang.Class<?>... parameters)
Optional<JavaConstructor>
tryGetConstructor(java.lang.String... parameters)
Same astryGetConstructor(Class[])
, but with parameter signature specified as fully qualified class names.Optional<JavaEnumConstant>
tryGetEnumConstant(java.lang.String 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)
Same astryGetMethod(String, Class[])
, but with parameter signature specified as fully qualified class names.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Method Details
-
getSource
-
getSourceCodeLocation
- Specified by:
getSourceCodeLocation
in interfaceHasSourceCodeLocation
- Returns:
- The
SourceCodeLocation
of this object, i.e. how to locate the respective object within the set of source files.
-
getDescription
- Specified by:
getDescription
in interfaceHasDescription
-
getName
-
getFullName
- Specified by:
getFullName
in interfaceHasName.AndFullName
- Returns:
- The fully qualified name of this
JavaClass
, i.e. the result is the same as invokinggetName()
-
getSimpleName
-
getPackage
-
getPackageName
-
isPrimitive
-
isInterface
-
isEnum
-
isAnnotation
-
isRecord
Returns whether this class is a record according to the Java Language Specification.Records were added as preview feature with JDK 14/15 and were released as regular feature with JDK 16.
See also JEP 395: Records
-
tryGetEnumConstant
@PublicAPI(usage=ACCESS) public Optional<JavaEnumConstant> tryGetEnumConstant(java.lang.String name) -
getEnumConstant
-
getEnumConstants
-
isArray
-
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.IllegalStateException
- if this class is no array
-
tryGetComponentType
Returns the component type of this class, if this class is an array, otherwiseOptional.absent()
. The component type is the type of the elements of an array type. ConsiderString[]
, then the component type would beString
. Likewise forString[][]
the component type would beString[]
.- Returns:
- The component type, if this type is an array, otherwise
Optional.absent()
-
getBaseComponentType
The base component type is the class'component type
if it is a one-dimensional array, the repeated application ofgetComponentType()
if it is a multi-dimensional array, or the class itself if it is no array. For example, the base component type ofint
,int[]
,int[][]
, ... is alwaysint
.- Returns:
- The base component type of this class
-
isTopLevelClass
A top level class is a class that is not a nested class, i.e. not declared within the body of another class.
Example:
Of all these class declarations onlypublic class TopLevel { class NestedNonStatic {} static class NestedStatic {} void method() { class NestedLocal {} new NestedAnonymous() {} } }
TopLevel
is a top level class, since all other classes are declared within the body ofTopLevel
and are thereby nested classes.
Compare e.g. Java Language Specification- Returns:
true
if this class is a top level class, i.e. not nested inside of any other class,false
otherwise- See Also:
isNestedClass()
,isMemberClass()
,isInnerClass()
,isLocalClass()
,isAnonymousClass()
-
isNestedClass
A nested class is any class whose declaration occurs within the body of another class or interface.
Example:
All classespublic class TopLevel { class NestedNonStatic {} static class NestedStatic {} void method() { class NestedLocal {} new NestedAnonymous() {} } }
NestedNonStatic
,NestedStatic
,NestedLocal
and the class the compiler creates for the anonymous class derived from"new NestedAnonymous() {}"
(which will have some generated name likeTopLevel$1
) are considered nested classes.TopLevel
on the other side is no nested class.
Compare e.g. Java Language Specification- Returns:
true
if this class is nested, i.e. declared within another class,false
otherwise (i.e. for top-level classes)- See Also:
isTopLevelClass()
,isMemberClass()
,isInnerClass()
,isLocalClass()
,isAnonymousClass()
-
isMemberClass
A member class is a class whose declaration is directly enclosed in the body of another class or interface declaration.
Example:
Bothpublic class TopLevel { class MemberClassNonStatic {} static class MemberClassStatic {} void method() { class NoMemberLocal {} new NoMemberAnonymous() {} } }
MemberClassNonStatic
andMemberClassStatic
are member classes, since they are directly declared within the body ofTopLevel
. On the other handNoMemberLocal
and the class the compiler creates for the anonymous class derived from"new NoMemberAnonymous() {}"
(which will have some generated name likeTopLevel$1
), as well asTopLevel
itself, are not considered member classes.
Compare e.g. Java Language Specification- Returns:
true
if this class is a member class, i.e. directly declared within the body of another class,false
otherwise- See Also:
isTopLevelClass()
,isNestedClass()
,isInnerClass()
,isLocalClass()
,isAnonymousClass()
-
isInnerClass
An inner class is a nested class that is not explicitly or implicitly declared static.
Example:
The classespublic class TopLevel { class InnerMemberClass {} static class NoInnerClassSinceDeclaredStatic {} interface NoInnerClassSinceInterface {} void method() { class InnerLocalClass {} new InnerAnonymousClass() {} } }
InnerMemberClass
,InnerLocalClass
and the class the compiler creates for the anonymous class derived from"new InnerAnonymousClass() {}"
(which will have some generated name likeTopLevel$1
) are inner classes since they are nested but not static. On the other handNoInnerClassSinceDeclaredStatic
,NoInnerClassSinceInterface
andTopLevel
are no inner classes, because the former two explicitly or implicitly have thestatic
modifier while the latter one is a top level class.
Compare e.g. Java Language Specification- Returns:
true
if this class is an inner class (i.e. nested but non-static)false
otherwise- See Also:
isTopLevelClass()
,isNestedClass()
,isMemberClass()
,isLocalClass()
,isAnonymousClass()
-
isLocalClass
A local class is a nested class that is not a member of any class and that has a name.
Example:
Only The classpublic class TopLevel { class InnerClass {} static class NestedStaticClass {} void method() { class LocalClass {} new AnonymousClass() {} } }
LocalClass
is a local class, since it is a nested class that is not a member class, but it has the name "LocalClass".
All the other classesTopLevel
,InnerClass
,NestedStaticClass
and the class the compiler creates for the anonymous class derived from"new AnonymousClass() {}"
(which will have some generated name likeTopLevel$1
) are considered non-local, since they either are top level, directly declared within the body ofTopLevel
or are anonymous and thus have no name.
Compare e.g. Java Language Specification- Returns:
true
if this class is local class,false
otherwise- See Also:
isTopLevelClass()
,isNestedClass()
,isMemberClass()
,isInnerClass()
,isAnonymousClass()
-
isAnonymousClass
An anonymous class is an inner class that is automatically derived from a class creation expression with a declared class body, e.g.new Example(){ <some-body> }
.
The compiler will automatically create a class backing this instance, typically with an autogenerated name likeSomeClass$1
, whereSomeClass
is the class holding the class creation expression.
Example:
Only the class the compiler creates for the anonymous class derived frompublic class TopLevel { class InnerClass {} static class NestedStaticClass {} void method() { class LocalClass {} new AnonymousClass() {} } }
"new AnonymousClass() {}"
(which will have some generated name likeTopLevel$1
) is considered an anonymous class.
All the other classesTopLevel
,InnerClass
,NestedStaticClass
andLocalClass
are considered non-anonymous.
Compare e.g. Java Language Specification- Returns:
true
if this class is an anonymous class,false
otherwise- See Also:
isTopLevelClass()
,isNestedClass()
,isMemberClass()
,isInnerClass()
,isLocalClass()
-
getModifiers
- Specified by:
getModifiers
in interfaceHasModifiers
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotationType)- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
-
isAnnotatedWith
- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate)- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> type)- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
-
isMetaAnnotatedWith
- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate)- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
-
getAnnotationOfType
@PublicAPI(usage=ACCESS) public <A extends java.lang.annotation.Annotation> A getAnnotationOfType(java.lang.Class<A> type)- Specified by:
getAnnotationOfType
in interfaceHasAnnotations<JavaClass>
- Parameters:
type
- A given annotation type to matchJavaAnnotations
against- Returns:
- An
Annotation
of the given annotation type - Throws:
java.lang.IllegalArgumentException
- if the class is not annotated with the given type- See Also:
isAnnotatedWith(Class)
,tryGetAnnotationOfType(Class)
-
getAnnotationOfType
@PublicAPI(usage=ACCESS) public JavaAnnotation<JavaClass> getAnnotationOfType(java.lang.String typeName)- Specified by:
getAnnotationOfType
in interfaceHasAnnotations<JavaClass>
-
getAnnotations
- Specified by:
getAnnotations
in interfaceHasAnnotations<JavaClass>
-
tryGetAnnotationOfType
@PublicAPI(usage=ACCESS) public <A extends java.lang.annotation.Annotation> Optional<A> tryGetAnnotationOfType(java.lang.Class<A> type)- Specified by:
tryGetAnnotationOfType
in interfaceHasAnnotations<JavaClass>
- Parameters:
type
- A given annotation type to matchJavaAnnotations
against- Returns:
- An
Optional
containing anAnnotation
of 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<JavaClass>> tryGetAnnotationOfType(java.lang.String typeName)Same astryGetAnnotationOfType(Class)
, but takes the type name.- Specified by:
tryGetAnnotationOfType
in interfaceHasAnnotations<JavaClass>
-
getTypeParameters
- Specified by:
getTypeParameters
in interfaceHasTypeParameters<JavaClass>
- Returns:
- the type parameters of this object, e.g. for any generic method
this would return the<A, B> B someMethod(A a) {..}
JavaTypeVariables
[A, B]
.
If this object is non-generic, e.g. a method
an empty list will be returned.void someMethod() {..}
-
getReferencedClassObjects
-
toErasure
Description copied from interface:JavaType
Converts thisJavaType
into the erased type (compare the Java Language Specification). In particular this will result in- the class itself, if this type is a
JavaClass
- the
JavaClass
equivalent toObject
, if this type is an unboundJavaTypeVariable
- the
JavaClass
equivalent to the erasure of the left most bound, if this type is a boundJavaTypeVariable
- if this type is a
JavaGenericArrayType
, the erasure will be theJavaClass
equivalent to the array type that has the erasure of the generic component type of this type as its component type; e.g. take the generic array typeT[][]
whereT
is unbound, then the erasure will be the array typeObject[][]
- the class itself, if this type is a
-
getRawSuperclass
-
getSuperclass
-
getSuperClass
Deprecated.UsegetRawSuperclass()
instead -
getClassHierarchy
- Returns:
- The complete class hierarchy, i.e. the class itself and the result of
getAllRawSuperclasses()
-
getAllRawSuperclasses
- 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.
-
getAllSuperClasses
Deprecated.UsegetAllRawSuperclasses()
instead. -
getSubclasses
-
getSubClasses
Deprecated.UsegetSubclasses()
instead. -
getInterfaces
-
getRawInterfaces
-
getAllRawInterfaces
-
getAllClassesSelfIsAssignableTo
- Returns:
- All classes, this class is assignable to, in particular
- self
- superclasses this class extends
- interfaces this class implements
-
getEnclosingClass
Returns the enclosing class if this class is nested within another class. OtherwiseOptional.absent()
.
Take for example
Thenclass OuterClass { class InnerClass{ } }
InnerClass.
getEnclosingClass()
would returnOptional.of(OuterClass)
. WhileOuterClass.
getEnclosingClass()
would returnOptional.absent()
. -
getEnclosingCodeUnit
Returns the enclosingJavaCodeUnit
if this class is declared within the context of aJavaCodeUnit
, e.g. a method or a constructor. OtherwiseOptional.absent()
.
Take for example
Thenclass OuterClass { OuterClass() { // creates a new anonymous class within the scope of the constructor new Serializable() {}; } void someMethod() { // creates a new local class within the scope of the method class LocalClass {} } class InnerClass {} }
anonymousSerializable.
getEnclosingCodeUnit()
would return theJavaConstructor
OuterClass()
andLocalClass.
getEnclosingCodeUnit()
would return theJavaMethod
void someMethod()
.
On the other handOuterClass.
getEnclosingCodeUnit()
orInnerClass.
getEnclosingCodeUnit()
would returnOptional.absent()
, since they are not defined within the context of aJavaCodeUnit
. -
getAllSubclasses
-
getAllSubClasses
Deprecated.UsegetAllSubclasses()
instead. -
getMembers
-
getAllMembers
-
getFields
-
getAllFields
-
getField
- Returns:
- The field with the given name.
- Throws:
java.lang.IllegalArgumentException
- If this class does not have such a field.
-
tryGetField
- Returns:
- The field with the given name, if this class has such a field, otherwise
Optional.absent()
.
-
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_NAME
orSTATIC_INITIALIZER_NAME
parameters
- The parameter signature of the method specified asClass
Objects- 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) -
tryGetCodeUnitWithParameterTypes
@PublicAPI(usage=ACCESS) public Optional<JavaCodeUnit> tryGetCodeUnitWithParameterTypes(java.lang.String name, java.util.List<java.lang.Class<?>> parameters)Same asgetCodeUnitWithParameterTypes(String, List)
, but will returnOptional.absent()
if there is no suchJavaCodeUnit
. -
getCodeUnitWithParameterTypeNames
@PublicAPI(usage=ACCESS) public JavaCodeUnit getCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters) -
tryGetCodeUnitWithParameterTypeNames
@PublicAPI(usage=ACCESS) public Optional<JavaCodeUnit> tryGetCodeUnitWithParameterTypeNames(java.lang.String name, java.util.List<java.lang.String> parameters)Same asgetCodeUnitWithParameterTypeNames(String, List)
, but will returnOptional.absent()
if there is no suchJavaCodeUnit
. -
getMethod
- Returns:
- The method with the given name and with zero parameters.
- Throws:
java.lang.IllegalArgumentException
- If this class does not have such a method.
-
getMethod
@PublicAPI(usage=ACCESS) public JavaMethod getMethod(java.lang.String name, java.lang.Class<?>... parameters)- Returns:
- The method with the given name and the given parameter types.
- Throws:
java.lang.IllegalArgumentException
- If this class does not have such a method.
-
getMethod
@PublicAPI(usage=ACCESS) public JavaMethod getMethod(java.lang.String name, java.lang.String... parameters)Same asgetMethod(String, Class[])
, but with parameter signature specified as fully qualified class names. -
tryGetMethod
- Returns:
- The method with the given name and with zero parameters,
if this class has such a method, otherwise
Optional.absent()
.
-
tryGetMethod
@PublicAPI(usage=ACCESS) public Optional<JavaMethod> tryGetMethod(java.lang.String name, java.lang.Class<?>... parameters)- Returns:
- The method with the given name and the given parameter types,
if this class has such a method, otherwise
Optional.absent()
.
-
tryGetMethod
@PublicAPI(usage=ACCESS) public Optional<JavaMethod> tryGetMethod(java.lang.String name, java.lang.String... parameters)Same astryGetMethod(String, Class[])
, but with parameter signature specified as fully qualified class names. -
getMethods
-
getAllMethods
-
getConstructor
- Returns:
- The constructor with zero parameters.
- Throws:
java.lang.IllegalArgumentException
- If this class does not have such a constructor.
-
getConstructor
- Returns:
- The constructor with the given parameter types.
- Throws:
java.lang.IllegalArgumentException
- If this class does not have a constructor with the given parameter types.
-
getConstructor
Same asgetConstructor(Class[])
, but with parameter signature specified as full class names. -
tryGetConstructor
- Returns:
- The constructor with zero parameters,
if this class has such a constructor, otherwise
Optional.absent()
.
-
tryGetConstructor
@PublicAPI(usage=ACCESS) public Optional<JavaConstructor> tryGetConstructor(java.lang.Class<?>... parameters)- Returns:
- The constructor with the given parameter types,
if this class has such a constructor, otherwise
Optional.absent()
.
-
tryGetConstructor
@PublicAPI(usage=ACCESS) public Optional<JavaConstructor> tryGetConstructor(java.lang.String... parameters)Same astryGetConstructor(Class[])
, but with parameter signature specified as fully qualified class names. -
getConstructors
-
getAllConstructors
-
getStaticInitializer
-
getAccessesFromSelf
-
getAllAccessesFromSelf
- Returns:
- Set of all
JavaAccess
in the class hierarchy, as opposed to the accesses this class directly performs.
-
getFieldAccessesFromSelf
-
getCallsFromSelf
Returns all calls of this class to methods or constructors. -
getMethodCallsFromSelf
-
getConstructorCallsFromSelf
-
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)
-
getTransitiveDependenciesFromSelf
Returns the transitive closure of all dependencies originating from this class, i.e. its direct dependencies and the dependencies from all imported target classes.- Returns:
- all transitive dependencies (including direct dependencies) from this class
- See Also:
getDirectDependenciesFromSelf()
-
getDirectDependenciesToSelf
LikegetDirectDependenciesFromSelf()
, but instead returns all dependencies where this class is target.- Returns:
- Dependencies where this class is the target.
-
getFieldAccessesToSelf
-
getMethodCallsToSelf
-
getConstructorCallsToSelf
-
getAccessesToSelf
-
getFieldsWithTypeOfSelf
- Returns:
- Fields of all imported classes that have the type of this class.
-
getMethodsWithParameterTypeOfSelf
- Returns:
- Methods of all imported classes that have a parameter type of this class.
-
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:
ThrowsDeclarations
of 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:
ThrowsDeclarations
of all imported classes that have the type of this class.
-
getAnnotationsWithTypeOfSelf
- Returns:
- All imported
JavaAnnotations
that have the annotation type of this class.
-
getAnnotationsWithParameterTypeOfSelf
@PublicAPI(usage=ACCESS) public java.util.Set<JavaAnnotation<?>> getAnnotationsWithParameterTypeOfSelf()- Returns:
- All imported
JavaAnnotations
that have a parameter with type of this class.
-
getInstanceofChecksWithTypeOfSelf
- Returns:
- All imported
InstanceofChecks
that check if another class is an instance of this class.
-
isFullyImported
- Returns:
- Whether this class has been fully imported, including all dependencies.
Classes that are only transitively imported are not necessarily fully imported.
Suppose you only import a classFoo
that calls a method of classBar
. ThenBar
is, as a dependency of the fully imported classFoo
, only transitively imported.
-
isEquivalentTo
- Parameters:
clazz
- An arbitrary type- Returns:
- true, if this
JavaClass
represents the same class as the suppliedClass
, otherwise false
-
isAssignableFrom
-
isAssignableFrom
-
isAssignableFrom
@PublicAPI(usage=ACCESS) public boolean isAssignableFrom(DescribedPredicate<? super JavaClass> predicate) -
isAssignableTo
-
isAssignableTo
-
isAssignableTo
@PublicAPI(usage=ACCESS) public boolean isAssignableTo(DescribedPredicate<? super JavaClass> predicate) -
reflect
Resolves the respectiveClass
from the classpath.
NOTE: This method will throw an exception, if the respectiveClass
or any of its dependencies can't be found on the classpath.- Returns:
- The
Class
equivalent to thisJavaClass
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-
namesOf
@Deprecated @PublicAPI(usage=ACCESS) public static java.util.List<java.lang.String> namesOf(java.lang.Class<?>... paramTypes)Deprecated.useFormatters.formatNamesOf(Class[])
instead -
namesOf
@Deprecated @PublicAPI(usage=ACCESS) public static java.util.List<java.lang.String> namesOf(java.lang.Iterable<java.lang.Class<?>> paramTypes)Deprecated.useFormatters.formatNamesOf(Iterable)
instead -
isAnonymous
Deprecated.useisAnonymousClass()
instead
-
getAllSubclasses()
instead.