Code generation library for creating dynamic proxies, bean utilities, and fast reflection alternatives with bundled dependencies
npx @tessl/cli install tessl/maven-cglib--cglib-nodep@3.3.0CGLib 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.
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</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.beans.BeanCopier;
import net.sf.cglib.reflect.FastClass;import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
// Create enhanced proxy class
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyClass.class);
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Before method: " + method.getName());
Object result = proxy.invokeSuper(obj, args);
System.out.println("After method: " + method.getName());
return result;
}
});
MyClass proxy = (MyClass) enhancer.create();
proxy.someMethod(); // Will be interceptedCGLib is organized around several key components:
Core proxy generation functionality for creating enhanced classes with method interception, callback support, and dynamic interface implementation.
public class Enhancer {
public static Object create(Class type, Callback callback);
public static Object create(Class superclass, Class[] interfaces, Callback callback);
public void setSuperclass(Class superclass);
public void setCallback(Callback callback);
public Object create();
}
public interface MethodInterceptor extends Callback {
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable;
}
public class MethodProxy {
public Object invoke(Object obj, Object[] args) throws Throwable;
public Object invokeSuper(Object obj, Object[] args) throws Throwable;
}High-performance JavaBean manipulation utilities including property copying, dynamic bean generation, and Map-based bean access.
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 abstract class BeanMap implements Map {
public static BeanMap create(Object bean);
public abstract Object get(Object bean, Object key);
public abstract Object put(Object bean, Object key, Object value);
}
public class BeanGenerator {
public void addProperty(String name, Class type);
public Object create();
}Fast alternatives to Java reflection for method and constructor invocation, plus method delegation utilities.
public abstract class FastClass {
public static FastClass create(Class type);
public abstract Object invoke(String name, Class[] parameterTypes, Object obj, Object[] args) throws InvocationTargetException;
public abstract Object newInstance() throws InvocationTargetException;
public abstract FastMethod getMethod(String name, Class[] parameterTypes);
}
public class FastMethod {
public Object invoke(Object obj, Object[] args) throws InvocationTargetException;
public Method getJavaMethod();
}
public abstract class MethodDelegate {
public static MethodDelegate create(Object target, String methodName, Class iface);
}Essential utilities for key generation, transformations, predicates, and low-level code generation support.
public abstract class KeyFactory {
public static KeyFactory create(Class keyInterface);
public abstract Object newInstance(Object[] args);
}
public interface Converter {
Object convert(Object value, Class target, Object context);
}
public interface Transformer {
Object transform(Object value);
}
public interface Predicate {
boolean evaluate(Object arg);
}Bytecode transformation utilities for modifying classes at load time and generating transformed versions of existing classes.
public class TransformingClassLoader extends AbstractClassLoader {
public TransformingClassLoader(ClassLoader parent, ClassFilter filter, ClassTransformerFactory t);
}
public abstract class ClassTransformer extends ClassVisitor {
public abstract void setTarget(ClassVisitor target);
}
public interface ClassFilter {
boolean accept(String className);
}High-performance utility classes for string switching and parallel array sorting operations.
public abstract class StringSwitcher {
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
public abstract int intValue(String s);
}
public abstract class ParallelSorter {
public static ParallelSorter create(Object[] arrays);
public abstract void quickSort(int index);
}public interface Callback {
// Marker interface for all callback types
}
public interface MethodInterceptor extends Callback {
Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable;
}
public interface FixedValue extends Callback {
Object loadObject() throws Exception;
}
public interface LazyLoader extends Callback {
Object loadObject() throws Exception;
}
public interface Dispatcher extends Callback {
Object loadObject() throws Exception;
}
public interface NoOp extends Callback {
// Marker interface - no methods
}
public interface CallbackFilter {
int accept(Method method);
}public interface Factory {
Object newInstance(Callback callback);
Object newInstance(Callback[] callbacks);
Callback getCallback(int index);
void setCallback(int index, Callback callback);
void setCallbacks(Callback[] callbacks);
Callback[] getCallbacks();
}public class BulkBeanException extends RuntimeException {
public BulkBeanException(String message, int index);
public int getIndex();
}
public class UndeclaredThrowableException extends RuntimeException {
public UndeclaredThrowableException(Throwable t);
public Throwable getUndeclaredThrowable();
}