High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
—
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.
import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;
import net.sf.cglib.reflect.FastConstructor;
import net.sf.cglib.reflect.MethodDelegate;
import net.sf.cglib.reflect.ConstructorDelegate;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import net.sf.cglib.core.Signature;Main class providing fast access to class methods and constructors without reflection overhead.
/**
* Provides fast access to class methods and constructors without reflection overhead
*/
public abstract class FastClass {
/**
* Create FastClass instance for given type
* @param type - Class to create FastClass for
* @return FastClass instance
*/
public static FastClass create(Class type);
/**
* Invoke method by index
* @param index - Method index (obtained from getIndex)
* @param obj - Object instance (null for static methods)
* @param args - Method arguments
* @return Method result
* @throws InvocationTargetException - If method throws exception
*/
public abstract Object invoke(int index, Object obj, Object[] args)
throws InvocationTargetException;
/**
* Create new instance using constructor by index
* @param index - Constructor index
* @param args - Constructor arguments
* @return New instance
* @throws InvocationTargetException - If constructor throws exception
*/
public abstract Object newInstance(int index, Object[] args)
throws InvocationTargetException;
/**
* Create new instance using default constructor
* @return New instance
* @throws InvocationTargetException - If constructor throws exception
*/
public abstract Object newInstance() throws InvocationTargetException;
/**
* Get method index by name and parameter types
* @param name - Method name
* @param parameterTypes - Parameter types
* @return Method index, or -1 if not found
*/
public abstract int getIndex(String name, Class[] parameterTypes);
/**
* Get constructor index by parameter types
* @param parameterTypes - Parameter types
* @return Constructor index, or -1 if not found
*/
public abstract int getIndex(Class[] parameterTypes);
/**
* Get method index from Signature
* @param sig - Method signature
* @return Method index, or -1 if not found
*/
public abstract int getIndex(Signature sig);
/**
* Get maximum method index
* @return Maximum method index
*/
public abstract int getMaxIndex();
/**
* Get Java Class being wrapped
* @return Java Class
*/
public Class getJavaClass();
/**
* Get method name by index
* @param index - Method index
* @return Method name
*/
public abstract String getName(int index);
/**
* Get parameter types by method index
* @param index - Method index
* @return Parameter types array
*/
public abstract Class[] getParameterTypes(int index);
/**
* Get return type by method index
* @param index - Method index
* @return Return type
*/
public abstract Class getReturnType(int index);
}Usage Examples:
import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;
// Basic FastClass usage
FastClass fastClass = FastClass.create(MyService.class);
// Get method index
int methodIndex = fastClass.getIndex("processData", new Class[]{String.class, int.class});
// Create instance
Object instance = fastClass.newInstance();
// Invoke method by index (much faster than reflection)
Object result = fastClass.invoke(methodIndex, instance, new Object[]{"test", 42});
// Alternative: using default constructor index
int constructorIndex = fastClass.getIndex(new Class[0]);
Object instance2 = fastClass.newInstance(constructorIndex, new Object[0]);Fast method invocation wrapper providing efficient access to specific methods.
/**
* Fast method invocation wrapper
*/
public class FastMethod {
/**
* Invoke the method
* @param obj - Object instance (null for static methods)
* @param args - Method arguments
* @return Method result
* @throws InvocationTargetException - If method throws exception
*/
public Object invoke(Object obj, Object[] args) throws InvocationTargetException;
/**
* Get method index in FastClass
* @return Method index
*/
public int getIndex();
/**
* Get method name
* @return Method name
*/
public String getName();
/**
* Get Java Method object
* @return Java Method
*/
public Method getJavaMethod();
/**
* Get parameter types
* @return Parameter types array
*/
public Class[] getParameterTypes();
/**
* Get return type
* @return Return type
*/
public Class getReturnType();
/**
* Get declaring class
* @return Declaring class
*/
public Class getDeclaringClass();
/**
* Get method modifiers
* @return Method modifiers
*/
public int getModifiers();
/**
* Get exception types
* @return Exception types array
*/
public Class[] getExceptionTypes();
}Fast constructor invocation wrapper providing efficient access to specific constructors.
/**
* Fast constructor invocation wrapper
*/
public class FastConstructor {
/**
* Create new instance using this constructor
* @param args - Constructor arguments
* @return New instance
* @throws InvocationTargetException - If constructor throws exception
*/
public Object newInstance(Object[] args) throws InvocationTargetException;
/**
* Get constructor index in FastClass
* @return Constructor index
*/
public int getIndex();
/**
* Get Java Constructor object
* @return Java Constructor
*/
public Constructor getJavaConstructor();
/**
* Get parameter types
* @return Parameter types array
*/
public Class[] getParameterTypes();
/**
* Get declaring class
* @return Declaring class
*/
public Class getDeclaringClass();
/**
* Get constructor modifiers
* @return Constructor modifiers
*/
public int getModifiers();
/**
* Get exception types
* @return Exception types array
*/
public Class[] getExceptionTypes();
}Creates strongly-typed delegates for method calls, providing type-safe method invocation.
/**
* Creates strongly-typed delegates for method calls
*/
public abstract class MethodDelegate {
/**
* Create method delegate
* @param targetClass - Class containing the method
* @param methodName - Method name
* @param delegateClass - Interface to implement (single method)
* @return Delegate instance implementing the interface
*/
public static MethodDelegate create(Class targetClass, String methodName, Class delegateClass);
/**
* Create static method delegate
* @param delegateClass - Interface to implement
* @param implClass - Class containing static method
* @param methodName - Method name
* @return Delegate instance
*/
public static MethodDelegate createStatic(Class delegateClass, Class implClass, String methodName);
}Usage Examples:
// Define delegate interface
interface StringProcessor {
String process(String input);
}
// Create delegate for String.toUpperCase method
StringProcessor delegate = (StringProcessor) MethodDelegate.create(
String.class, "toUpperCase", StringProcessor.class);
// Use delegate (much faster than reflection)
String result = delegate.process("hello"); // Returns "HELLO"
// Static method delegate
interface MathOperation {
double compute(double a, double b);
}
MathOperation maxDelegate = (MathOperation) MethodDelegate.createStatic(
MathOperation.class, Math.class, "max");
double max = maxDelegate.compute(5.5, 3.2);Creates strongly-typed delegates for constructor calls.
/**
* Creates strongly-typed delegates for constructor calls
*/
public abstract class ConstructorDelegate {
/**
* Create constructor delegate
* @param targetClass - Class to instantiate
* @param delegateClass - Interface to implement (single method returning targetClass)
* @return Delegate instance
*/
public static ConstructorDelegate create(Class targetClass, Class delegateClass);
}Usage Examples:
// Define constructor delegate interface
interface StringBuilderFactory {
StringBuilder create(String initial);
}
// Create delegate for StringBuilder(String) constructor
StringBuilderFactory factory = (StringBuilderFactory) ConstructorDelegate.create(
StringBuilder.class, StringBuilderFactory.class);
// Use delegate (faster than reflection)
StringBuilder sb = factory.create("initial value");Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib