0
# CGLib (cglib-nodep)
1
2
CGLib is a powerful Java code generation library that enables dynamic class creation, proxy generation, and bytecode manipulation at runtime. The cglib-nodep distribution bundles CGLib with all dependencies (including ASM) into a single JAR without external dependencies, making it ideal for applications that need to avoid dependency conflicts.
3
4
## Package Information
5
6
- **Package Name**: cglib-nodep
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**:
10
```xml
11
<dependency>
12
<groupId>cglib</groupId>
13
<artifactId>cglib-nodep</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.beans.BeanCopier;
25
import net.sf.cglib.reflect.FastClass;
26
```
27
28
## Basic Usage
29
30
```java
31
import net.sf.cglib.proxy.Enhancer;
32
import net.sf.cglib.proxy.MethodInterceptor;
33
import net.sf.cglib.proxy.MethodProxy;
34
35
// Create enhanced proxy class
36
Enhancer enhancer = new Enhancer();
37
enhancer.setSuperclass(MyClass.class);
38
enhancer.setCallback(new MethodInterceptor() {
39
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
40
System.out.println("Before method: " + method.getName());
41
Object result = proxy.invokeSuper(obj, args);
42
System.out.println("After method: " + method.getName());
43
return result;
44
}
45
});
46
47
MyClass proxy = (MyClass) enhancer.create();
48
proxy.someMethod(); // Will be intercepted
49
```
50
51
## Architecture
52
53
CGLib is organized around several key components:
54
55
- **Proxy Generation**: Dynamic creation of enhanced classes that extend existing classes or implement interfaces
56
- **Method Interception**: Callback-based system for intercepting and modifying method calls
57
- **Bean Utilities**: High-performance utilities for JavaBean manipulation (copying, mapping, generation)
58
- **Fast Reflection**: Optimized alternatives to Java reflection for method/constructor invocation
59
- **Code Generation**: Low-level bytecode generation framework using ASM (repackaged to avoid conflicts)
60
- **Runtime Enhancement**: Dynamic class modification and proxy creation without compile-time dependencies
61
62
## Capabilities
63
64
### Proxy and Enhancement
65
66
Core proxy generation functionality for creating enhanced classes with method interception, callback support, and dynamic interface implementation.
67
68
```java { .api }
69
public class Enhancer {
70
public static Object create(Class type, Callback callback);
71
public static Object create(Class superclass, Class[] interfaces, Callback callback);
72
public void setSuperclass(Class superclass);
73
public void setCallback(Callback callback);
74
public Object create();
75
}
76
77
public interface MethodInterceptor extends Callback {
78
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable;
79
}
80
81
public class MethodProxy {
82
public Object invoke(Object obj, Object[] args) throws Throwable;
83
public Object invokeSuper(Object obj, Object[] args) throws Throwable;
84
}
85
```
86
87
[Proxy and Enhancement](./proxy-enhancement.md)
88
89
### Bean Utilities
90
91
High-performance JavaBean manipulation utilities including property copying, dynamic bean generation, and Map-based bean access.
92
93
```java { .api }
94
public abstract class BeanCopier {
95
public static BeanCopier create(Class source, Class target, boolean useConverter);
96
public abstract void copy(Object from, Object to, Converter converter);
97
}
98
99
public abstract class BeanMap implements Map {
100
public static BeanMap create(Object bean);
101
public abstract Object get(Object bean, Object key);
102
public abstract Object put(Object bean, Object key, Object value);
103
}
104
105
public class BeanGenerator {
106
public void addProperty(String name, Class type);
107
public Object create();
108
}
109
```
110
111
[Bean Utilities](./bean-utilities.md)
112
113
### Reflection Utilities
114
115
Fast alternatives to Java reflection for method and constructor invocation, plus method delegation utilities.
116
117
```java { .api }
118
public abstract class FastClass {
119
public static FastClass create(Class type);
120
public abstract Object invoke(String name, Class[] parameterTypes, Object obj, Object[] args) throws InvocationTargetException;
121
public abstract Object newInstance() throws InvocationTargetException;
122
public abstract FastMethod getMethod(String name, Class[] parameterTypes);
123
}
124
125
public class FastMethod {
126
public Object invoke(Object obj, Object[] args) throws InvocationTargetException;
127
public Method getJavaMethod();
128
}
129
130
public abstract class MethodDelegate {
131
public static MethodDelegate create(Object target, String methodName, Class iface);
132
}
133
```
134
135
[Reflection Utilities](./reflection-utilities.md)
136
137
### Core Utilities
138
139
Essential utilities for key generation, transformations, predicates, and low-level code generation support.
140
141
```java { .api }
142
public abstract class KeyFactory {
143
public static KeyFactory create(Class keyInterface);
144
public abstract Object newInstance(Object[] args);
145
}
146
147
public interface Converter {
148
Object convert(Object value, Class target, Object context);
149
}
150
151
public interface Transformer {
152
Object transform(Object value);
153
}
154
155
public interface Predicate {
156
boolean evaluate(Object arg);
157
}
158
```
159
160
[Core Utilities](./core-utilities.md)
161
162
### Transformation Utilities
163
164
Bytecode transformation utilities for modifying classes at load time and generating transformed versions of existing classes.
165
166
```java { .api }
167
public class TransformingClassLoader extends AbstractClassLoader {
168
public TransformingClassLoader(ClassLoader parent, ClassFilter filter, ClassTransformerFactory t);
169
}
170
171
public abstract class ClassTransformer extends ClassVisitor {
172
public abstract void setTarget(ClassVisitor target);
173
}
174
175
public interface ClassFilter {
176
boolean accept(String className);
177
}
178
```
179
180
[Transformation Utilities](./transformation-utilities.md)
181
182
### Utility Classes
183
184
High-performance utility classes for string switching and parallel array sorting operations.
185
186
```java { .api }
187
public abstract class StringSwitcher {
188
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
189
public abstract int intValue(String s);
190
}
191
192
public abstract class ParallelSorter {
193
public static ParallelSorter create(Object[] arrays);
194
public abstract void quickSort(int index);
195
}
196
```
197
198
[Utility Classes](./utility-classes.md)
199
200
## Types
201
202
### Core Callback Types
203
204
```java { .api }
205
public interface Callback {
206
// Marker interface for all callback types
207
}
208
209
public interface MethodInterceptor extends Callback {
210
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable;
211
}
212
213
public interface FixedValue extends Callback {
214
Object loadObject() throws Exception;
215
}
216
217
public interface LazyLoader extends Callback {
218
Object loadObject() throws Exception;
219
}
220
221
public interface Dispatcher extends Callback {
222
Object loadObject() throws Exception;
223
}
224
225
public interface NoOp extends Callback {
226
// Marker interface - no methods
227
}
228
229
public interface CallbackFilter {
230
int accept(Method method);
231
}
232
```
233
234
### Factory Interface
235
236
```java { .api }
237
public interface Factory {
238
Object newInstance(Callback callback);
239
Object newInstance(Callback[] callbacks);
240
Callback getCallback(int index);
241
void setCallback(int index, Callback callback);
242
void setCallbacks(Callback[] callbacks);
243
Callback[] getCallbacks();
244
}
245
```
246
247
### Exception Types
248
249
```java { .api }
250
public class BulkBeanException extends RuntimeException {
251
public BulkBeanException(String message, int index);
252
public int getIndex();
253
}
254
255
public class UndeclaredThrowableException extends RuntimeException {
256
public UndeclaredThrowableException(Throwable t);
257
public Throwable getUndeclaredThrowable();
258
}
259
```