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 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: