The AspectJ runtime is a small library necessary to run Java programs enhanced by AspectJ aspects during a previous compile-time or post-compile-time (binary weaving) build step.
npx @tessl/cli install tessl/maven-org-aspectj--aspectjrt@1.9.0AspectJ Runtime provides the essential runtime infrastructure for aspect-oriented programming in Java. This small library is necessary to run Java programs enhanced by AspectJ aspects during a previous compile-time or post-compile-time (binary weaving) build step. It includes core classes for join point introspection, aspect management, annotation-based aspect definition, and runtime utilities that enable crosscutting concerns like logging, security, performance monitoring, and error handling to be cleanly modularized and applied to Java programs.
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.24</version>
</dependency>For Gradle:
implementation 'org.aspectj:aspectjrt:1.9.24'import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Around;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void logMethodEntry(JoinPoint joinPoint) {
System.out.println("Entering method: " + joinPoint.getSignature().getName());
System.out.println("With args: " + Arrays.toString(joinPoint.getArgs()));
System.out.println("At: " + joinPoint.getSourceLocation());
}
}AspectJ Runtime is organized around several key components that enable aspect-oriented programming:
JoinPoint, ProceedingJoinPoint) that provide reflective access to execution points in the program where aspects can be appliedAjType, AjTypeSystem) for introspecting aspects, pointcuts, advice, and inter-type declarations at runtimeCore runtime interfaces that provide reflective access to join points - the specific points in program execution where aspects can be applied. Includes access to execution context, arguments, target objects, and signature information.
public interface JoinPoint {
Object getThis();
Object getTarget();
Object[] getArgs();
Signature getSignature();
SourceLocation getSourceLocation();
String getKind();
StaticPart getStaticPart();
String toString();
String toShortString();
String toLongString();
}
public interface ProceedingJoinPoint extends JoinPoint {
Object proceed() throws Throwable;
Object proceed(Object[] args) throws Throwable;
}Runtime support for aspect instantiation, lifecycle management, and access across different instantiation models. Provides programmatic access to aspect instances and binding status.
public class Aspects {
public static <T> T aspectOf(Class<T> aspectClass);
public static <T> T aspectOf(Class<T> aspectClass, Object perObject);
public static <T> T aspectOf(Class<T> aspectClass, Class<?> perTypeWithin);
public static <T> boolean hasAspect(Class<T> aspectClass);
public static <T> boolean hasAspect(Class<T> aspectClass, Object perObject);
public static <T> boolean hasAspect(Class<T> aspectClass, Class<?> perTypeWithin);
}Complete set of annotations for defining aspects using the @AspectJ syntax. Supports all AspectJ language features including advice, pointcuts, inter-type declarations, and aspect instantiation models.
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Aspect {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Pointcut {
String value() default "";
String argNames() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Before {
String value();
String argNames() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Around {
String value();
String argNames() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface After {
String value();
String argNames() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AfterReturning {
String value() default "";
String pointcut() default "";
String returning() default "";
String argNames() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AfterThrowing {
String value() default "";
String pointcut() default "";
String throwing() default "";
String argNames() default "";
}Rich type system for representing signatures of methods, constructors, fields, advice, and other program elements. Provides complete reflection capabilities with access to modifiers, parameter types, return types, and exception specifications.
public interface Signature {
String getName();
int getModifiers();
Class getDeclaringType();
String toString();
String toShortString();
String toLongString();
}
public interface MethodSignature extends MemberSignature {
Class getReturnType();
Method getMethod();
Class[] getParameterTypes();
String[] getParameterNames();
Class[] getExceptionTypes();
}
public interface ConstructorSignature extends CodeSignature {
Constructor getConstructor();
}
public interface FieldSignature extends MemberSignature {
Class getFieldType();
Field getField();
}AspectJ-specific type system that extends Java reflection to provide access to aspect-oriented features like pointcuts, advice, inter-type declarations, and per-clauses.
public interface AjType<T> {
String getName();
Class<T> getJavaClass();
AjType<?>[] getInterfaces();
boolean isAspect();
Aspect getAspectAnnotation();
PerClause getPerClause();
Pointcut[] getPointcuts();
Advice[] getAdvice();
}
public class AjTypeSystem {
public static AjType<?> getAjType(Class<?> clazz);
public static AjType<?>[] getAjTypes(Class<?>[] classes);
}Supporting classes for control flow tracking, exception handling, and other runtime services that enable advanced AspectJ features.
public class CFlow {
public CFlow();
public CFlow(Object _aspect);
public Object getAspect();
public void setAspect(Object _aspect);
public Object get(int index);
}
public class SoftException extends RuntimeException {
public SoftException(Throwable inner);
public Throwable getWrappedThrowable();
public Throwable getCause();
}
public class NoAspectBoundException extends RuntimeException {
public NoAspectBoundException(String aspectName, Throwable inner);
public Throwable getCause();
}public interface JoinPoint {
String METHOD_EXECUTION = "method-execution";
String METHOD_CALL = "method-call";
String CONSTRUCTOR_EXECUTION = "constructor-execution";
String CONSTRUCTOR_CALL = "constructor-call";
String FIELD_GET = "field-get";
String FIELD_SET = "field-set";
String STATICINITIALIZATION = "staticinitialization";
String PREINITIALIZATION = "preinitialization";
String INITIALIZATION = "initialization";
String EXCEPTION_HANDLER = "exception-handler";
String SYNCHRONIZATION_LOCK = "lock";
String SYNCHRONIZATION_UNLOCK = "unlock";
String ADVICE_EXECUTION = "adviceexecution";
}public interface SourceLocation {
Class getWithinType();
String getFileName();
int getLine();
int getColumn();
}public enum PerClauseKind {
SINGLETON, PERCFLOW, PERCFLOWBELOW, PERTARGET, PERTHIS, PERTYPEWITHIN
}
public interface PerClause {
PerClauseKind getKind();
}