Package com.tngtech.archunit.core.domain
Class AccessTarget
java.lang.Object
com.tngtech.archunit.core.domain.AccessTarget
- All Implemented Interfaces:
HasDescription
,CanBeAnnotated
,HasName
,HasName.AndFullName
,HasOwner<JavaClass>
- Direct Known Subclasses:
AccessTarget.CodeUnitAccessTarget
,AccessTarget.FieldAccessTarget
@PublicAPI(usage=ACCESS)
public abstract class AccessTarget
extends Object
implements HasName.AndFullName, CanBeAnnotated, HasOwner<JavaClass>, HasDescription
Represents the target of a
To determine the respective member targeted by a
Note that it is possible that ArchUnit will not find any member matching this
JavaAccess
. ArchUnit distinguishes between an 'access target' and a concrete field/method/constructor,
because the bytecode does not reflect the exact member that an access will resolve to. Take for example
class Caller {
void call(Target target) {
target.call();
}
}
class Target extends TargetParent {
}
class TargetParent {
void call() {}
}
Then the bytecode will have encoded Caller -> Target.call()
, but there is no method call()
in Target
since
it needs to be resolved from the parent. This can be a lot more complex for interface inheritance, where multiple inheritance is
possible.To determine the respective member targeted by a
JavaAccess
ArchUnit follows the logic of the Reflection API
(e.g. Class.getMethod(String, Class[])
). This only applies to how members are located, not what members are located
(e.g. ArchUnit will find a JavaCodeUnit
with name "<init>",
even if the Reflection API explicitly excludes this bytecode-only method and represents the constructor as a method with the simple class
name instead).
Note that it is possible that ArchUnit will not find any member matching this
AccessTarget
. This is due to the fact that any
numbers of referenced classes can be missing from the import. E.g. some method Foo.origin()
of some imported
class Foo
might call a method Bar.target()
. But if Bar
is missing from the import (i.e. the bytecode of Bar.class
has not been scanned together with Foo.class
),
there will not be a JavaMethod
representing Bar.target()
.
So even though we can derive an AccessTarget
that is Bar.target()
from Foo's
bytecode (including method name and parameters), we cannot associate any JavaMethod
with that target.- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
Represents anAccessTarget
where the target is a code unit.static class
Represents anAccessTarget
where the target is a code unit.static class
Represents anAccessTarget
where the target is a code unit.static final class
Represents aAccessTarget.CodeUnitCallTarget
where the target is a constructor.static final class
Represents aAccessTarget.CodeUnitReferenceTarget
where the target is a constructor.static final class
Represents anAccessTarget
where the target is a field.static final class
Predefinedfunctions
to transformAccessTarget
.static final class
Represents aAccessTarget.CodeUnitCallTarget
where the target is a method.static final class
Represents aAccessTarget.CodeUnitReferenceTarget
where the target is a method.static final class
Predefinedpredicates
targetingAccessTarget
.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 TypeMethodDescriptionboolean
getName()
getOwner()
int
hashCode()
boolean
isAnnotatedWith
(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returns true, if one of the resolved targets is annotated with an annotation matching the predicate.
NOTE: If the target was not imported, this method will always return false.boolean
isAnnotatedWith
(Class<? extends Annotation> annotationType) Returns true, if one of the resolved targets is annotated with the given annotation type.
NOTE: If the target was not imported, this method will always return false.boolean
isAnnotatedWith
(String annotationTypeName) boolean
isMetaAnnotatedWith
(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returns true, if one of the resolved targets is meta-annotated with an annotation matching the predicate.
NOTE: If the target was not imported, this method will always return false.boolean
isMetaAnnotatedWith
(Class<? extends Annotation> annotationType) Returns true, if one of the resolved targets is meta-annotated with an annotation of the given type.
NOTE: If the target was not imported, this method will always return false.boolean
isMetaAnnotatedWith
(String annotationTypeName) Optional<? extends JavaMember>
Attempts to resolve the targeted member (method, field or constructor).toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.tngtech.archunit.base.HasDescription
getDescription
-
Method Details
-
getName
-
getOwner
-
getFullName
- Specified by:
getFullName
in interfaceHasName.AndFullName
- Returns:
- The full name of this
AccessTarget
, i.e. a string containing${declaringClass}.${name}
for a field and${declaringClass}.${name}(${parameterTypes})
for a code unit
-
resolveMember
Attempts to resolve the targeted member (method, field or constructor). This will be a single element if the target has been imported, orOptional.empty()
if the target was not imported. For further information refer toAccessTarget
.- Returns:
- The member that matches the access target or empty if it was not imported
- See Also:
-
isAnnotatedWith
Returns true, if one of the resolved targets is annotated with the given annotation type.
NOTE: If the target was not imported, this method will always return false.- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationType
- The type of the annotation to check for- Returns:
- true if one of the resolved targets is annotated with the given type
-
isAnnotatedWith
- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationTypeName
- Fully qualified class name of a specific type ofAnnotation
- See Also:
-
isAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returns true, if one of the resolved targets is annotated with an annotation matching the predicate.
NOTE: If the target was not imported, this method will always return false.- Specified by:
isAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
predicate
- Qualifies matching annotations- Returns:
- true if one of the resolved targets is annotated with an annotation matching the predicate
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(Class<? extends Annotation> annotationType) Returns true, if one of the resolved targets is meta-annotated with an annotation of the given type.
NOTE: If the target was not imported, this method will always return false.- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationType
- Type of the annotation to look for- Returns:
- true if one of the resolved targets is meta-annotated with an annotation with the given type
-
isMetaAnnotatedWith
- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
annotationTypeName
- Fully qualified class name of a specific type ofAnnotation
- See Also:
-
isMetaAnnotatedWith
@PublicAPI(usage=ACCESS) public boolean isMetaAnnotatedWith(DescribedPredicate<? super JavaAnnotation<?>> predicate) Returns true, if one of the resolved targets is meta-annotated with an annotation matching the predicate.
NOTE: If the target was not imported, this method will always return false.- Specified by:
isMetaAnnotatedWith
in interfaceCanBeAnnotated
- Parameters:
predicate
- Qualifies matching annotations- Returns:
- true if one of the resolved targets is meta-annotated with an annotation matching the predicate
-
hashCode
public int hashCode() -
equals
-
toString
-