0
# AspectJ Runtime (aspectjrt)
1
2
AspectJ 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.
3
4
## Package Information
5
6
- **Package Name**: org.aspectj:aspectjrt
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Installation**: Add to your Maven dependencies:
10
11
```xml
12
<dependency>
13
<groupId>org.aspectj</groupId>
14
<artifactId>aspectjrt</artifactId>
15
<version>1.9.24</version>
16
</dependency>
17
```
18
19
For Gradle:
20
21
```gradle
22
implementation 'org.aspectj:aspectjrt:1.9.24'
23
```
24
25
## Core Imports
26
27
```java
28
import org.aspectj.lang.JoinPoint;
29
import org.aspectj.lang.ProceedingJoinPoint;
30
import org.aspectj.lang.annotation.Aspect;
31
import org.aspectj.lang.annotation.Before;
32
import org.aspectj.lang.annotation.Around;
33
```
34
35
## Basic Usage
36
37
```java
38
import org.aspectj.lang.JoinPoint;
39
import org.aspectj.lang.annotation.Aspect;
40
import org.aspectj.lang.annotation.Before;
41
42
@Aspect
43
public class LoggingAspect {
44
45
@Before("execution(* com.example.service.*.*(..))")
46
public void logMethodEntry(JoinPoint joinPoint) {
47
System.out.println("Entering method: " + joinPoint.getSignature().getName());
48
System.out.println("With args: " + Arrays.toString(joinPoint.getArgs()));
49
System.out.println("At: " + joinPoint.getSourceLocation());
50
}
51
}
52
```
53
54
## Architecture
55
56
AspectJ Runtime is organized around several key components that enable aspect-oriented programming:
57
58
- **Join Point System**: Core interfaces (`JoinPoint`, `ProceedingJoinPoint`) that provide reflective access to execution points in the program where aspects can be applied
59
- **Signature Framework**: Rich type system for representing method, constructor, field, and advice signatures with full reflection capabilities
60
- **Aspect Management**: Runtime support for aspect instantiation and lifecycle management across different instantiation models (singleton, per-object, per-type, etc.)
61
- **Annotation-Based Definition**: Complete @AspectJ annotation set for defining aspects, pointcuts, advice, and inter-type declarations using Java annotations
62
- **Reflection API**: Comprehensive type system (`AjType`, `AjTypeSystem`) for introspecting aspects, pointcuts, advice, and inter-type declarations at runtime
63
64
## Capabilities
65
66
### Join Point Introspection
67
68
Core 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.
69
70
```java { .api }
71
public interface JoinPoint {
72
Object getThis();
73
Object getTarget();
74
Object[] getArgs();
75
Signature getSignature();
76
SourceLocation getSourceLocation();
77
String getKind();
78
StaticPart getStaticPart();
79
String toString();
80
String toShortString();
81
String toLongString();
82
}
83
84
public interface ProceedingJoinPoint extends JoinPoint {
85
Object proceed() throws Throwable;
86
Object proceed(Object[] args) throws Throwable;
87
}
88
```
89
90
[Join Point Introspection](./join-point-introspection.md)
91
92
### Aspect Management
93
94
Runtime support for aspect instantiation, lifecycle management, and access across different instantiation models. Provides programmatic access to aspect instances and binding status.
95
96
```java { .api }
97
public class Aspects {
98
public static <T> T aspectOf(Class<T> aspectClass);
99
public static <T> T aspectOf(Class<T> aspectClass, Object perObject);
100
public static <T> T aspectOf(Class<T> aspectClass, Class<?> perTypeWithin);
101
public static <T> boolean hasAspect(Class<T> aspectClass);
102
public static <T> boolean hasAspect(Class<T> aspectClass, Object perObject);
103
public static <T> boolean hasAspect(Class<T> aspectClass, Class<?> perTypeWithin);
104
}
105
```
106
107
[Aspect Management](./aspect-management.md)
108
109
### Annotation-Based Aspect Definition
110
111
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.
112
113
```java { .api }
114
@Retention(RetentionPolicy.RUNTIME)
115
@Target(ElementType.TYPE)
116
public @interface Aspect {
117
String value() default "";
118
}
119
120
@Retention(RetentionPolicy.RUNTIME)
121
@Target(ElementType.METHOD)
122
public @interface Pointcut {
123
String value() default "";
124
String argNames() default "";
125
}
126
127
@Retention(RetentionPolicy.RUNTIME)
128
@Target(ElementType.METHOD)
129
public @interface Before {
130
String value();
131
String argNames() default "";
132
}
133
134
@Retention(RetentionPolicy.RUNTIME)
135
@Target(ElementType.METHOD)
136
public @interface Around {
137
String value();
138
String argNames() default "";
139
}
140
141
@Retention(RetentionPolicy.RUNTIME)
142
@Target(ElementType.METHOD)
143
public @interface After {
144
String value();
145
String argNames() default "";
146
}
147
148
@Retention(RetentionPolicy.RUNTIME)
149
@Target(ElementType.METHOD)
150
public @interface AfterReturning {
151
String value() default "";
152
String pointcut() default "";
153
String returning() default "";
154
String argNames() default "";
155
}
156
157
@Retention(RetentionPolicy.RUNTIME)
158
@Target(ElementType.METHOD)
159
public @interface AfterThrowing {
160
String value() default "";
161
String pointcut() default "";
162
String throwing() default "";
163
String argNames() default "";
164
}
165
```
166
167
[Annotation-Based Definition](./annotation-based-definition.md)
168
169
### Signature System
170
171
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.
172
173
```java { .api }
174
public interface Signature {
175
String getName();
176
int getModifiers();
177
Class getDeclaringType();
178
String toString();
179
String toShortString();
180
String toLongString();
181
}
182
183
public interface MethodSignature extends MemberSignature {
184
Class getReturnType();
185
Method getMethod();
186
Class[] getParameterTypes();
187
String[] getParameterNames();
188
Class[] getExceptionTypes();
189
}
190
191
public interface ConstructorSignature extends CodeSignature {
192
Constructor getConstructor();
193
}
194
195
public interface FieldSignature extends MemberSignature {
196
Class getFieldType();
197
Field getField();
198
}
199
```
200
201
[Signature System](./signature-system.md)
202
203
### Reflection API
204
205
AspectJ-specific type system that extends Java reflection to provide access to aspect-oriented features like pointcuts, advice, inter-type declarations, and per-clauses.
206
207
```java { .api }
208
public interface AjType<T> {
209
String getName();
210
Class<T> getJavaClass();
211
AjType<?>[] getInterfaces();
212
boolean isAspect();
213
Aspect getAspectAnnotation();
214
PerClause getPerClause();
215
Pointcut[] getPointcuts();
216
Advice[] getAdvice();
217
}
218
219
public class AjTypeSystem {
220
public static AjType<?> getAjType(Class<?> clazz);
221
public static AjType<?>[] getAjTypes(Class<?>[] classes);
222
}
223
```
224
225
[Reflection API](./reflection-api.md)
226
227
### Runtime Utilities
228
229
Supporting classes for control flow tracking, exception handling, and other runtime services that enable advanced AspectJ features.
230
231
```java { .api }
232
public class CFlow {
233
public CFlow();
234
public CFlow(Object _aspect);
235
public Object getAspect();
236
public void setAspect(Object _aspect);
237
public Object get(int index);
238
}
239
240
public class SoftException extends RuntimeException {
241
public SoftException(Throwable inner);
242
public Throwable getWrappedThrowable();
243
public Throwable getCause();
244
}
245
246
public class NoAspectBoundException extends RuntimeException {
247
public NoAspectBoundException(String aspectName, Throwable inner);
248
public Throwable getCause();
249
}
250
```
251
252
[Runtime Utilities](./runtime-utilities.md)
253
254
## Types
255
256
### Join Point Constants
257
258
```java { .api }
259
public interface JoinPoint {
260
String METHOD_EXECUTION = "method-execution";
261
String METHOD_CALL = "method-call";
262
String CONSTRUCTOR_EXECUTION = "constructor-execution";
263
String CONSTRUCTOR_CALL = "constructor-call";
264
String FIELD_GET = "field-get";
265
String FIELD_SET = "field-set";
266
String STATICINITIALIZATION = "staticinitialization";
267
String PREINITIALIZATION = "preinitialization";
268
String INITIALIZATION = "initialization";
269
String EXCEPTION_HANDLER = "exception-handler";
270
String SYNCHRONIZATION_LOCK = "lock";
271
String SYNCHRONIZATION_UNLOCK = "unlock";
272
String ADVICE_EXECUTION = "adviceexecution";
273
}
274
```
275
276
### Source Location
277
278
```java { .api }
279
public interface SourceLocation {
280
Class getWithinType();
281
String getFileName();
282
int getLine();
283
int getColumn();
284
}
285
```
286
287
### Per Clause Types
288
289
```java { .api }
290
public enum PerClauseKind {
291
SINGLETON, PERCFLOW, PERCFLOWBELOW, PERTARGET, PERTHIS, PERTYPEWITHIN
292
}
293
294
public interface PerClause {
295
PerClauseKind getKind();
296
}
297
```