High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
npx @tessl/cli install tessl/maven-cglib--cglib@3.3.0CGLib (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.
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import net.sf.cglib.proxy.Callback;For reflection utilities:
import net.sf.cglib.reflect.FastClass;
import net.sf.cglib.reflect.FastMethod;For bean utilities:
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;import net.sf.cglib.proxy.*;
import java.lang.reflect.Method;
// Basic proxy creation with method interception
public class Example {
public static void main(String[] args) {
// Create an enhancer for generating proxy classes
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyService.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;
}
});
// Create proxy instance
MyService proxy = (MyService) enhancer.create();
proxy.doSomething();
}
}
class MyService {
public void doSomething() {
System.out.println("Doing something...");
}
}CGLib is built around several key components:
The library generates subclasses at runtime that extend target classes, enabling method interception without requiring interfaces (unlike JDK dynamic proxies).
Core proxy and class enhancement functionality for creating dynamic subclasses with method interception. This is the primary feature of CGLib used by most frameworks.
public class Enhancer {
public void setSuperclass(Class superclass);
public void setCallback(Callback callback);
public void setCallbacks(Callback[] callbacks);
public void setCallbackFilter(CallbackFilter filter);
public Object create();
public Object create(Class[] argumentTypes, Object[] arguments);
}
public interface MethodInterceptor extends Callback {
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
throws Throwable;
}High-performance reflection utilities that avoid the overhead of Java's reflection API through bytecode generation.
public abstract class FastClass {
public static FastClass create(Class type);
public abstract Object invoke(int index, Object obj, Object[] args);
public abstract Object newInstance(int index, Object[] args);
public abstract int getIndex(String name, Class[] parameterTypes);
}
public class FastMethod {
public Object invoke(Object obj, Object[] args);
public int getIndex();
}Specialized utilities for JavaBean manipulation including property copying, Map-like access, and dynamic bean generation.
public abstract class BeanCopier {
public static BeanCopier create(Class source, Class target, boolean useConverter);
public abstract void copy(Object from, Object to, Converter converter);
}
public class BeanMap extends HashMap {
public static BeanMap create(Object bean);
public Object getBean();
public void setBean(Object bean);
}Utility classes for efficient string operations and specialized sorting algorithms.
public abstract class StringSwitcher {
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
public abstract int intValue(String s);
}
public class ParallelSorter {
public static void sort(Object[] a, Object[] b);
}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.
public abstract class ClassTransformer {
public abstract ClassVisitor transform(ClassVisitor cv);
}
public class TransformingClassLoader extends ClassLoader {
public TransformingClassLoader(ClassLoader parent, ClassFilter filter,
ClassTransformer transformer);
}Core internal classes and utilities that provide foundational functionality for CGLib's bytecode generation, including method signatures and class generation utilities.
public class Signature {
public Signature(String name, String returnType, String[] argumentTypes);
public String getName();
public String getDescriptor();
}
public class UndeclaredThrowableException extends RuntimeException {
public UndeclaredThrowableException(Throwable undeclaredThrowable);
}public interface Callback extends Serializable {
// Marker interface for all callback types
}
public interface CallbackFilter {
int accept(Method method);
}
public interface Factory {
Object newInstance(Callback callback);
Object newInstance(Callback[] callbacks);
Object newInstance(Class[] types, Object[] args, Callback[] callbacks);
void setCallback(int index, Callback callback);
void setCallbacks(Callback[] callbacks);
Callback getCallback(int index);
Callback[] getCallbacks();
}
public class MethodProxy {
public Object invokeSuper(Object obj, Object[] args) throws Throwable;
public Object invoke(Object obj, Object[] args) throws Throwable;
public String getSuperName();
public int getSuperIndex();
}
public abstract class AbstractClassGenerator {
public void setClassLoader(ClassLoader classLoader);
public ClassLoader getClassLoader();
public void setNamingPolicy(NamingPolicy namingPolicy);
public NamingPolicy getNamingPolicy();
}
public interface NamingPolicy {
String getClassName(String prefix, String source, Object key, Predicate names);
}
public class Signature {
public Signature(String name, String returnType, String[] argumentTypes);
public String getName();
public String getReturnType();
public String[] getArgumentTypes();
public String getDescriptor();
}