0
# Fast Reflection
1
2
High-performance reflection utilities that avoid the overhead of Java's reflection API through bytecode generation. These classes provide significant performance improvements for frequent method and constructor invocations.
3
4
## Core Imports
5
6
```java
7
import net.sf.cglib.reflect.FastClass;
8
import net.sf.cglib.reflect.FastMethod;
9
import net.sf.cglib.reflect.FastConstructor;
10
import net.sf.cglib.reflect.MethodDelegate;
11
import net.sf.cglib.reflect.ConstructorDelegate;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import java.lang.reflect.Constructor;
15
import net.sf.cglib.core.Signature;
16
```
17
18
## Capabilities
19
20
### FastClass
21
22
Main class providing fast access to class methods and constructors without reflection overhead.
23
24
```java { .api }
25
/**
26
* Provides fast access to class methods and constructors without reflection overhead
27
*/
28
public abstract class FastClass {
29
/**
30
* Create FastClass instance for given type
31
* @param type - Class to create FastClass for
32
* @return FastClass instance
33
*/
34
public static FastClass create(Class type);
35
36
/**
37
* Invoke method by index
38
* @param index - Method index (obtained from getIndex)
39
* @param obj - Object instance (null for static methods)
40
* @param args - Method arguments
41
* @return Method result
42
* @throws InvocationTargetException - If method throws exception
43
*/
44
public abstract Object invoke(int index, Object obj, Object[] args)
45
throws InvocationTargetException;
46
47
/**
48
* Create new instance using constructor by index
49
* @param index - Constructor index
50
* @param args - Constructor arguments
51
* @return New instance
52
* @throws InvocationTargetException - If constructor throws exception
53
*/
54
public abstract Object newInstance(int index, Object[] args)
55
throws InvocationTargetException;
56
57
/**
58
* Create new instance using default constructor
59
* @return New instance
60
* @throws InvocationTargetException - If constructor throws exception
61
*/
62
public abstract Object newInstance() throws InvocationTargetException;
63
64
/**
65
* Get method index by name and parameter types
66
* @param name - Method name
67
* @param parameterTypes - Parameter types
68
* @return Method index, or -1 if not found
69
*/
70
public abstract int getIndex(String name, Class[] parameterTypes);
71
72
/**
73
* Get constructor index by parameter types
74
* @param parameterTypes - Parameter types
75
* @return Constructor index, or -1 if not found
76
*/
77
public abstract int getIndex(Class[] parameterTypes);
78
79
/**
80
* Get method index from Signature
81
* @param sig - Method signature
82
* @return Method index, or -1 if not found
83
*/
84
public abstract int getIndex(Signature sig);
85
86
/**
87
* Get maximum method index
88
* @return Maximum method index
89
*/
90
public abstract int getMaxIndex();
91
92
/**
93
* Get Java Class being wrapped
94
* @return Java Class
95
*/
96
public Class getJavaClass();
97
98
/**
99
* Get method name by index
100
* @param index - Method index
101
* @return Method name
102
*/
103
public abstract String getName(int index);
104
105
/**
106
* Get parameter types by method index
107
* @param index - Method index
108
* @return Parameter types array
109
*/
110
public abstract Class[] getParameterTypes(int index);
111
112
/**
113
* Get return type by method index
114
* @param index - Method index
115
* @return Return type
116
*/
117
public abstract Class getReturnType(int index);
118
}
119
```
120
121
**Usage Examples:**
122
123
```java
124
import net.sf.cglib.reflect.FastClass;
125
import net.sf.cglib.reflect.FastMethod;
126
127
// Basic FastClass usage
128
FastClass fastClass = FastClass.create(MyService.class);
129
130
// Get method index
131
int methodIndex = fastClass.getIndex("processData", new Class[]{String.class, int.class});
132
133
// Create instance
134
Object instance = fastClass.newInstance();
135
136
// Invoke method by index (much faster than reflection)
137
Object result = fastClass.invoke(methodIndex, instance, new Object[]{"test", 42});
138
139
// Alternative: using default constructor index
140
int constructorIndex = fastClass.getIndex(new Class[0]);
141
Object instance2 = fastClass.newInstance(constructorIndex, new Object[0]);
142
```
143
144
### FastMethod
145
146
Fast method invocation wrapper providing efficient access to specific methods.
147
148
```java { .api }
149
/**
150
* Fast method invocation wrapper
151
*/
152
public class FastMethod {
153
/**
154
* Invoke the method
155
* @param obj - Object instance (null for static methods)
156
* @param args - Method arguments
157
* @return Method result
158
* @throws InvocationTargetException - If method throws exception
159
*/
160
public Object invoke(Object obj, Object[] args) throws InvocationTargetException;
161
162
/**
163
* Get method index in FastClass
164
* @return Method index
165
*/
166
public int getIndex();
167
168
/**
169
* Get method name
170
* @return Method name
171
*/
172
public String getName();
173
174
/**
175
* Get Java Method object
176
* @return Java Method
177
*/
178
public Method getJavaMethod();
179
180
/**
181
* Get parameter types
182
* @return Parameter types array
183
*/
184
public Class[] getParameterTypes();
185
186
/**
187
* Get return type
188
* @return Return type
189
*/
190
public Class getReturnType();
191
192
/**
193
* Get declaring class
194
* @return Declaring class
195
*/
196
public Class getDeclaringClass();
197
198
/**
199
* Get method modifiers
200
* @return Method modifiers
201
*/
202
public int getModifiers();
203
204
/**
205
* Get exception types
206
* @return Exception types array
207
*/
208
public Class[] getExceptionTypes();
209
}
210
```
211
212
### FastConstructor
213
214
Fast constructor invocation wrapper providing efficient access to specific constructors.
215
216
```java { .api }
217
/**
218
* Fast constructor invocation wrapper
219
*/
220
public class FastConstructor {
221
/**
222
* Create new instance using this constructor
223
* @param args - Constructor arguments
224
* @return New instance
225
* @throws InvocationTargetException - If constructor throws exception
226
*/
227
public Object newInstance(Object[] args) throws InvocationTargetException;
228
229
/**
230
* Get constructor index in FastClass
231
* @return Constructor index
232
*/
233
public int getIndex();
234
235
/**
236
* Get Java Constructor object
237
* @return Java Constructor
238
*/
239
public Constructor getJavaConstructor();
240
241
/**
242
* Get parameter types
243
* @return Parameter types array
244
*/
245
public Class[] getParameterTypes();
246
247
/**
248
* Get declaring class
249
* @return Declaring class
250
*/
251
public Class getDeclaringClass();
252
253
/**
254
* Get constructor modifiers
255
* @return Constructor modifiers
256
*/
257
public int getModifiers();
258
259
/**
260
* Get exception types
261
* @return Exception types array
262
*/
263
public Class[] getExceptionTypes();
264
}
265
```
266
267
### MethodDelegate
268
269
Creates strongly-typed delegates for method calls, providing type-safe method invocation.
270
271
```java { .api }
272
/**
273
* Creates strongly-typed delegates for method calls
274
*/
275
public abstract class MethodDelegate {
276
/**
277
* Create method delegate
278
* @param targetClass - Class containing the method
279
* @param methodName - Method name
280
* @param delegateClass - Interface to implement (single method)
281
* @return Delegate instance implementing the interface
282
*/
283
public static MethodDelegate create(Class targetClass, String methodName, Class delegateClass);
284
285
/**
286
* Create static method delegate
287
* @param delegateClass - Interface to implement
288
* @param implClass - Class containing static method
289
* @param methodName - Method name
290
* @return Delegate instance
291
*/
292
public static MethodDelegate createStatic(Class delegateClass, Class implClass, String methodName);
293
}
294
```
295
296
**Usage Examples:**
297
298
```java
299
// Define delegate interface
300
interface StringProcessor {
301
String process(String input);
302
}
303
304
// Create delegate for String.toUpperCase method
305
StringProcessor delegate = (StringProcessor) MethodDelegate.create(
306
String.class, "toUpperCase", StringProcessor.class);
307
308
// Use delegate (much faster than reflection)
309
String result = delegate.process("hello"); // Returns "HELLO"
310
311
// Static method delegate
312
interface MathOperation {
313
double compute(double a, double b);
314
}
315
316
MathOperation maxDelegate = (MathOperation) MethodDelegate.createStatic(
317
MathOperation.class, Math.class, "max");
318
double max = maxDelegate.compute(5.5, 3.2);
319
```
320
321
### ConstructorDelegate
322
323
Creates strongly-typed delegates for constructor calls.
324
325
```java { .api }
326
/**
327
* Creates strongly-typed delegates for constructor calls
328
*/
329
public abstract class ConstructorDelegate {
330
/**
331
* Create constructor delegate
332
* @param targetClass - Class to instantiate
333
* @param delegateClass - Interface to implement (single method returning targetClass)
334
* @return Delegate instance
335
*/
336
public static ConstructorDelegate create(Class targetClass, Class delegateClass);
337
}
338
```
339
340
**Usage Examples:**
341
342
```java
343
// Define constructor delegate interface
344
interface StringBuilderFactory {
345
StringBuilder create(String initial);
346
}
347
348
// Create delegate for StringBuilder(String) constructor
349
StringBuilderFactory factory = (StringBuilderFactory) ConstructorDelegate.create(
350
StringBuilder.class, StringBuilderFactory.class);
351
352
// Use delegate (faster than reflection)
353
StringBuilder sb = factory.create("initial value");
354
```