High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
—
Core internal classes and utilities that provide foundational functionality for CGLib's bytecode generation. While primarily internal, some classes offer public APIs for advanced use cases and integration with other bytecode generation scenarios.
Represents method and constructor signatures for bytecode generation, providing a structured way to handle method identification and comparison.
/**
* Represents method signatures for bytecode generation
*/
public class Signature {
/**
* Create method signature
* @param name - Method name
* @param returnType - Return type descriptor
* @param argumentTypes - Argument type descriptors
*/
public Signature(String name, String returnType, String[] argumentTypes);
/**
* Get method name
* @return Method name
*/
public String getName();
/**
* Get return type descriptor
* @return Return type descriptor string
*/
public String getReturnType();
/**
* Get argument type descriptors
* @return Array of argument type descriptor strings
*/
public String[] getArgumentTypes();
/**
* Get method descriptor string
* @return Complete method descriptor
*/
public String getDescriptor();
/**
* Check equality with another signature
* @param obj - Object to compare
* @return true if signatures are equal
*/
@Override
public boolean equals(Object obj);
/**
* Get hash code for signature
* @return Hash code
*/
@Override
public int hashCode();
/**
* Get string representation
* @return String representation of signature
*/
@Override
public String toString();
}Usage Examples:
import net.sf.cglib.core.Signature;
// Create signature for method: String getName()
Signature getterSig = new Signature("getName", "Ljava/lang/String;", new String[0]);
// Create signature for method: void setAge(int age)
Signature setterSig = new Signature("setAge", "V", new String[]{"I"});
// Create signature for method: List<String> findItems(String query, int limit)
Signature findSig = new Signature("findItems",
"Ljava/util/List;",
new String[]{"Ljava/lang/String;", "I"});
// Compare signatures
boolean isEqual = getterSig.equals(otherSignature);
// Use in collections (implements proper hashCode/equals)
Set<Signature> methodSignatures = new HashSet<>();
methodSignatures.add(getterSig);
methodSignatures.add(setterSig);Low-level class generation utilities providing foundational bytecode generation capabilities used by higher-level CGLib components.
/**
* Low-level class generation utilities
*/
public abstract class ClassGenerator extends AbstractClassGenerator {
/**
* Generate class bytecode
* @param strategy - Generation strategy
* @return Generated class
* @throws Exception - If generation fails
*/
protected abstract Class generate(ClassLoaderData data) throws Exception;
/**
* Get default ClassLoader
* @return Default ClassLoader
*/
protected ClassLoader getDefaultClassLoader();
/**
* Generate class name
* @return Generated class name
*/
protected String generateClassName();
/**
* Create class generator
* @param source - Source object for generation context
*/
protected ClassGenerator(Source source);
}Base class for all CGLib class generators, providing common functionality for bytecode generation.
/**
* Base class for all CGLib class generators
*/
public abstract class AbstractClassGenerator implements ClassGenerator {
/**
* Set class loader for generated classes
* @param classLoader - ClassLoader to use
*/
public void setClassLoader(ClassLoader classLoader);
/**
* Get class loader for generated classes
* @return ClassLoader being used
*/
public ClassLoader getClassLoader();
/**
* Set naming policy for generated classes
* @param namingPolicy - NamingPolicy implementation
*/
public void setNamingPolicy(NamingPolicy namingPolicy);
/**
* Get naming policy
* @return Current NamingPolicy
*/
public NamingPolicy getNamingPolicy();
/**
* Set generation strategy
* @param strategy - GeneratorStrategy implementation
*/
public void setStrategy(GeneratorStrategy strategy);
/**
* Get generation strategy
* @return Current GeneratorStrategy
*/
public GeneratorStrategy getStrategy();
/**
* Create instance of generated class
* @param key - Cache key for class generation
* @return Generated class instance
*/
protected Object create(Object key);
}Policy interface for generating names of dynamically created classes.
/**
* Policy interface for generating names of dynamically created classes
*/
public interface NamingPolicy {
/**
* Generate class name
* @param prefix - Prefix for class name
* @param source - Source class name
* @param key - Generation key object
* @param names - Predicate to check for name conflicts
* @return Generated class name
*/
String getClassName(String prefix, String source, Object key, Predicate names);
/**
* Default naming policy instance
*/
NamingPolicy DEFAULT = new DefaultNamingPolicy();
}Usage Examples:
import net.sf.cglib.core.NamingPolicy;
import net.sf.cglib.core.Predicate;
// Custom naming policy
NamingPolicy customPolicy = new NamingPolicy() {
@Override
public String getClassName(String prefix, String source, Object key, Predicate names) {
return "com.mycompany.generated." + prefix + "$$" + source + "$$" + key.hashCode();
}
};
// Use with Enhancer
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyClass.class);
enhancer.setNamingPolicy(customPolicy);
enhancer.setCallback(interceptor);
Object proxy = enhancer.create();/**
* Runtime exception for undeclared checked exceptions in proxy methods
*/
public class UndeclaredThrowableException extends RuntimeException {
/**
* Create exception with undeclared throwable
* @param undeclaredThrowable - The undeclared exception
*/
public UndeclaredThrowableException(Throwable undeclaredThrowable);
/**
* Get the undeclared throwable
* @return Undeclared throwable cause
*/
public Throwable getUndeclaredThrowable();
}
/**
* Exception thrown when code generation fails
*/
public class CodeGenerationException extends RuntimeException {
/**
* Create exception with message
* @param message - Error message
*/
public CodeGenerationException(String message);
/**
* Create exception with message and cause
* @param message - Error message
* @param cause - Underlying cause
*/
public CodeGenerationException(String message, Throwable cause);
}import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.ClassVisitor;
/**
* Strategy interface for generating classes
*/
public interface GeneratorStrategy {
/**
* Generate class bytecode
* @param cg - ClassGenerator instance
* @return Generated class bytecode
* @throws Exception - If generation fails
*/
byte[] generate(ClassGenerator cg) throws Exception;
/**
* Default strategy instance
*/
GeneratorStrategy DEFAULT = new DefaultGeneratorStrategy();
}
/**
* Predicate interface for filtering operations
*/
public interface Predicate {
/**
* Evaluate predicate
* @param arg - Argument to evaluate
* @return true if predicate matches, false otherwise
*/
boolean evaluate(Object arg);
}Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib