0
# CGLib
1
2
CGLib (Code Generation Library) is a powerful Java bytecode generation library that enables runtime class enhancement and dynamic proxy creation. It provides high-level APIs for generating and transforming Java bytecode, making it essential for frameworks implementing Aspect-Oriented Programming (AOP), testing frameworks, and data access layers.
3
4
## Package Information
5
6
- **Package Name**: cglib
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**:
10
```xml
11
<dependency>
12
<groupId>cglib</groupId>
13
<artifactId>cglib</artifactId>
14
<version>3.3.0</version>
15
</dependency>
16
```
17
18
## Core Imports
19
20
```java
21
import net.sf.cglib.proxy.Enhancer;
22
import net.sf.cglib.proxy.MethodInterceptor;
23
import net.sf.cglib.proxy.MethodProxy;
24
import net.sf.cglib.proxy.Callback;
25
```
26
27
For reflection utilities:
28
```java
29
import net.sf.cglib.reflect.FastClass;
30
import net.sf.cglib.reflect.FastMethod;
31
```
32
33
For bean utilities:
34
```java
35
import net.sf.cglib.beans.BeanCopier;
36
import net.sf.cglib.beans.BeanMap;
37
```
38
39
## Basic Usage
40
41
```java
42
import net.sf.cglib.proxy.*;
43
import java.lang.reflect.Method;
44
45
// Basic proxy creation with method interception
46
public class Example {
47
public static void main(String[] args) {
48
// Create an enhancer for generating proxy classes
49
Enhancer enhancer = new Enhancer();
50
enhancer.setSuperclass(MyService.class);
51
enhancer.setCallback(new MethodInterceptor() {
52
@Override
53
public Object intercept(Object obj, Method method, Object[] args,
54
MethodProxy proxy) throws Throwable {
55
System.out.println("Before: " + method.getName());
56
Object result = proxy.invokeSuper(obj, args);
57
System.out.println("After: " + method.getName());
58
return result;
59
}
60
});
61
62
// Create proxy instance
63
MyService proxy = (MyService) enhancer.create();
64
proxy.doSomething();
65
}
66
}
67
68
class MyService {
69
public void doSomething() {
70
System.out.println("Doing something...");
71
}
72
}
73
```
74
75
## Architecture
76
77
CGLib is built around several key components:
78
79
- **Proxy Generation**: Core functionality using ASM for bytecode generation
80
- **Callback System**: Pluggable interfaces for different proxy behaviors
81
- **Fast Reflection**: High-performance alternatives to Java reflection
82
- **Bean Utilities**: Specialized tools for JavaBean manipulation
83
- **Bytecode Transformation**: Lower-level APIs for custom transformations
84
85
The library generates subclasses at runtime that extend target classes, enabling method interception without requiring interfaces (unlike JDK dynamic proxies).
86
87
## Capabilities
88
89
### Proxy Generation
90
91
Core proxy and class enhancement functionality for creating dynamic subclasses with method interception. This is the primary feature of CGLib used by most frameworks.
92
93
```java { .api }
94
public class Enhancer {
95
public void setSuperclass(Class superclass);
96
public void setCallback(Callback callback);
97
public void setCallbacks(Callback[] callbacks);
98
public void setCallbackFilter(CallbackFilter filter);
99
public Object create();
100
public Object create(Class[] argumentTypes, Object[] arguments);
101
}
102
103
public interface MethodInterceptor extends Callback {
104
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
105
throws Throwable;
106
}
107
```
108
109
[Proxy Generation](./proxy-generation.md)
110
111
### Fast Reflection
112
113
High-performance reflection utilities that avoid the overhead of Java's reflection API through bytecode generation.
114
115
```java { .api }
116
public abstract class FastClass {
117
public static FastClass create(Class type);
118
public abstract Object invoke(int index, Object obj, Object[] args);
119
public abstract Object newInstance(int index, Object[] args);
120
public abstract int getIndex(String name, Class[] parameterTypes);
121
}
122
123
public class FastMethod {
124
public Object invoke(Object obj, Object[] args);
125
public int getIndex();
126
}
127
```
128
129
[Fast Reflection](./fast-reflection.md)
130
131
### Bean Utilities
132
133
Specialized utilities for JavaBean manipulation including property copying, Map-like access, and dynamic bean generation.
134
135
```java { .api }
136
public abstract class BeanCopier {
137
public static BeanCopier create(Class source, Class target, boolean useConverter);
138
public abstract void copy(Object from, Object to, Converter converter);
139
}
140
141
public class BeanMap extends HashMap {
142
public static BeanMap create(Object bean);
143
public Object getBean();
144
public void setBean(Object bean);
145
}
146
```
147
148
[Bean Utilities](./bean-utilities.md)
149
150
### String and Utility Classes
151
152
Utility classes for efficient string operations and specialized sorting algorithms.
153
154
```java { .api }
155
public abstract class StringSwitcher {
156
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
157
public abstract int intValue(String s);
158
}
159
160
public class ParallelSorter {
161
public static void sort(Object[] a, Object[] b);
162
}
163
```
164
165
[Utilities](./utilities.md)
166
167
### Bytecode Transformation
168
169
Advanced bytecode transformation utilities for custom class loading and runtime class modification. These provide lower-level access to CGLib's capabilities for specialized use cases.
170
171
```java { .api }
172
public abstract class ClassTransformer {
173
public abstract ClassVisitor transform(ClassVisitor cv);
174
}
175
176
public class TransformingClassLoader extends ClassLoader {
177
public TransformingClassLoader(ClassLoader parent, ClassFilter filter,
178
ClassTransformer transformer);
179
}
180
```
181
182
[Bytecode Transformation](./transform.md)
183
184
### Core Utilities
185
186
Core internal classes and utilities that provide foundational functionality for CGLib's bytecode generation, including method signatures and class generation utilities.
187
188
```java { .api }
189
public class Signature {
190
public Signature(String name, String returnType, String[] argumentTypes);
191
public String getName();
192
public String getDescriptor();
193
}
194
195
public class UndeclaredThrowableException extends RuntimeException {
196
public UndeclaredThrowableException(Throwable undeclaredThrowable);
197
}
198
```
199
200
[Core Utilities](./core.md)
201
202
## Types
203
204
```java { .api }
205
public interface Callback extends Serializable {
206
// Marker interface for all callback types
207
}
208
209
public interface CallbackFilter {
210
int accept(Method method);
211
}
212
213
public interface Factory {
214
Object newInstance(Callback callback);
215
Object newInstance(Callback[] callbacks);
216
Object newInstance(Class[] types, Object[] args, Callback[] callbacks);
217
void setCallback(int index, Callback callback);
218
void setCallbacks(Callback[] callbacks);
219
Callback getCallback(int index);
220
Callback[] getCallbacks();
221
}
222
223
public class MethodProxy {
224
public Object invokeSuper(Object obj, Object[] args) throws Throwable;
225
public Object invoke(Object obj, Object[] args) throws Throwable;
226
public String getSuperName();
227
public int getSuperIndex();
228
}
229
230
public abstract class AbstractClassGenerator {
231
public void setClassLoader(ClassLoader classLoader);
232
public ClassLoader getClassLoader();
233
public void setNamingPolicy(NamingPolicy namingPolicy);
234
public NamingPolicy getNamingPolicy();
235
}
236
237
public interface NamingPolicy {
238
String getClassName(String prefix, String source, Object key, Predicate names);
239
}
240
241
public class Signature {
242
public Signature(String name, String returnType, String[] argumentTypes);
243
public String getName();
244
public String getReturnType();
245
public String[] getArgumentTypes();
246
public String getDescriptor();
247
}
248
```