High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
—
Core proxy and class enhancement functionality for creating dynamic subclasses with method interception. This is the primary feature of CGLib used by most frameworks for AOP, testing, and ORM implementations.
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.FixedValue;
import net.sf.cglib.proxy.NoOp;
import net.sf.cglib.proxy.LazyLoader;
import net.sf.cglib.proxy.Dispatcher;
import net.sf.cglib.proxy.ProxyRefDispatcher;
import net.sf.cglib.proxy.InvocationHandler;
import net.sf.cglib.proxy.UndeclaredThrowableException;
import net.sf.cglib.core.NamingPolicy;
import java.lang.reflect.Method;
import java.io.Serializable;Main class for creating enhanced subclasses and proxy objects. The Enhancer generates a new class that extends the target class and allows method interception through callbacks.
/**
* Main class for creating enhanced subclasses and proxy objects
*/
public class Enhancer extends AbstractClassGenerator {
/**
* Set the class to be enhanced (superclass of generated class)
* @param superclass - The class to extend
*/
public void setSuperclass(Class superclass);
/**
* Set interfaces to be implemented by generated class
* @param interfaces - Array of interfaces to implement
*/
public void setInterfaces(Class[] interfaces);
/**
* Set single callback for all methods
* @param callback - Callback to handle method calls
*/
public void setCallback(Callback callback);
/**
* Set multiple callbacks for different methods
* @param callbacks - Array of callbacks
*/
public void setCallbacks(Callback[] callbacks);
/**
* Set filter to determine which callback to use for each method
* @param filter - CallbackFilter implementation
*/
public void setCallbackFilter(CallbackFilter filter);
/**
* Create instance using default constructor
* @return Enhanced instance
*/
public Object create();
/**
* Create instance using specific constructor
* @param argumentTypes - Constructor parameter types
* @param arguments - Constructor arguments
* @return Enhanced instance
*/
public Object create(Class[] argumentTypes, Object[] arguments);
/**
* Set naming policy for generated classes
* @param namingPolicy - Strategy for naming generated classes
*/
public void setNamingPolicy(NamingPolicy namingPolicy);
/**
* Set class loader for generated classes
* @param loader - ClassLoader to use
*/
public void setClassLoader(ClassLoader loader);
}Usage Examples:
import net.sf.cglib.proxy.*;
import java.lang.reflect.Method;
// Basic method interception
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(UserService.class);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
System.out.println("Before: " + method.getName());
Object result = proxy.invokeSuper(obj, args);
System.out.println("After: " + method.getName());
return result;
}
});
UserService service = (UserService) enhancer.create();
// Multiple callbacks with filter
enhancer.setCallbacks(new Callback[] {
new MethodInterceptor() { /* logging interceptor */ },
NoOp.INSTANCE // no interception
});
enhancer.setCallbackFilter(new CallbackFilter() {
@Override
public int accept(Method method) {
return method.getName().startsWith("get") ? 1 : 0;
}
});Primary callback interface for intercepting method calls on proxy objects.
/**
* Callback for intercepting method calls on proxy objects
*/
public interface MethodInterceptor extends Callback {
/**
* Intercept method calls on proxy objects
* @param obj - Enhanced object instance
* @param method - Method being called
* @param args - Method arguments
* @param proxy - MethodProxy for efficient super calls
* @return Method result
* @throws Throwable - Any exception from method execution
*/
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
throws Throwable;
}Additional callback interfaces for specific proxy behaviors.
/**
* Callback that returns a fixed value for all method calls
*/
public interface FixedValue extends Callback {
Object loadObject() throws Exception;
}
/**
* Callback that performs no operation, allowing original method execution
*/
public interface NoOp extends Callback {
NoOp INSTANCE = new NoOp() {};
}
/**
* Callback for lazy initialization of proxy objects
*/
public interface LazyLoader extends Callback {
Object loadObject() throws Exception;
}
/**
* Callback that dispatches calls to different objects
*/
public interface Dispatcher extends Callback {
Object loadObject() throws Exception;
}
/**
* Dispatcher that receives reference to proxy object
*/
public interface ProxyRefDispatcher extends Callback {
Object loadObject(Object proxy) throws Exception;
}
/**
* JDK-compatible invocation handler interface
*/
public interface InvocationHandler extends Callback {
Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
}Interface for determining which callback to use for each method.
/**
* Interface for determining which callback to use for each method
*/
public interface CallbackFilter {
/**
* Determine callback index for given method
* @param method - Method to filter
* @return Index of callback to use (corresponds to callbacks array)
*/
int accept(Method method);
}Interface implemented by all enhanced classes for callback management.
/**
* Interface implemented by enhanced classes for callback management
*/
public interface Factory {
/**
* Create new instance with single callback
* @param callback - Callback to use
* @return New instance
*/
Object newInstance(Callback callback);
/**
* Create new instance with multiple callbacks
* @param callbacks - Array of callbacks
* @return New instance
*/
Object newInstance(Callback[] callbacks);
/**
* Create new instance with constructor arguments and callbacks
* @param types - Constructor parameter types
* @param args - Constructor arguments
* @param callbacks - Array of callbacks
* @return New instance
*/
Object newInstance(Class[] types, Object[] args, Callback[] callbacks);
/**
* Set callback at specific index
* @param index - Callback index
* @param callback - Callback to set
*/
void setCallback(int index, Callback callback);
/**
* Set all callbacks
* @param callbacks - Array of callbacks
*/
void setCallbacks(Callback[] callbacks);
/**
* Get callback at specific index
* @param index - Callback index
* @return Callback at index
*/
Callback getCallback(int index);
/**
* Get all callbacks
* @return Array of all callbacks
*/
Callback[] getCallbacks();
}Efficient method invocation mechanism for proxy objects.
/**
* Efficient method invocation mechanism for proxy objects
*/
public class MethodProxy {
/**
* Invoke super method implementation
* @param obj - Object instance
* @param args - Method arguments
* @return Method result
* @throws Throwable - Any exception from method execution
*/
public Object invokeSuper(Object obj, Object[] args) throws Throwable;
/**
* Invoke original method (may cause infinite recursion if not careful)
* @param obj - Object instance
* @param args - Method arguments
* @return Method result
* @throws Throwable - Any exception from method execution
*/
public Object invoke(Object obj, Object[] args) throws Throwable;
/**
* Get name of super method
* @return Super method name
*/
public String getSuperName();
/**
* Get index of super method
* @return Super method index
*/
public int getSuperIndex();
}Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib