High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
—
Specialized utilities for JavaBean manipulation including high-performance property copying, Map-like access to bean properties, and dynamic bean generation at runtime.
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.ImmutableBean;
import net.sf.cglib.beans.BulkBean;
import net.sf.cglib.core.NamingPolicy;
import java.util.HashMap;
import java.util.Set;
import java.io.Serializable;High-performance utility for copying properties between JavaBean objects, much faster than reflection-based approaches.
/**
* High-performance bean copying utility
*/
public abstract class BeanCopier {
/**
* Create BeanCopier for copying between source and target types
* @param source - Source class type
* @param target - Target class type
* @param useConverter - Whether to use a converter for type conversion
* @return BeanCopier instance
*/
public static BeanCopier create(Class source, Class target, boolean useConverter);
/**
* Copy properties from source to target object
* @param from - Source object
* @param to - Target object
* @param converter - Converter for type conversion (null if useConverter was false)
*/
public abstract void copy(Object from, Object to, Converter converter);
/**
* Converter interface for custom type conversion during copying
*/
public interface Converter {
/**
* Convert value from source type to target type
* @param value - Source value
* @param target - Target property type
* @param context - Property name for context
* @return Converted value
*/
Object convert(Object value, Class target, Object context);
}
}Usage Examples:
import net.sf.cglib.beans.BeanCopier;
// Basic property copying (matching property names and types)
BeanCopier copier = BeanCopier.create(UserDto.class, User.class, false);
UserDto dto = new UserDto("John", "john@example.com", 25);
User user = new User();
copier.copy(dto, user, null);
// With custom converter for type conversion
BeanCopier converterCopier = BeanCopier.create(StringUser.class, User.class, true);
StringUser stringUser = new StringUser("Jane", "30", "jane@example.com");
User convertedUser = new User();
converterCopier.copy(stringUser, convertedUser, new BeanCopier.Converter() {
@Override
public Object convert(Object value, Class target, Object context) {
if (target == int.class && "age".equals(context)) {
return Integer.parseInt((String) value);
}
return value;
}
});Provides Map-like interface for accessing JavaBean properties, allowing dynamic property access without reflection.
/**
* Map-like interface for bean property access
*/
public class BeanMap extends HashMap {
/**
* Create BeanMap for given bean instance
* @param bean - Bean object to wrap
* @return BeanMap instance
*/
public static BeanMap create(Object bean);
/**
* Get the wrapped bean object
* @return Bean object
*/
public Object getBean();
/**
* Set new bean object to wrap
* @param bean - New bean object
*/
public void setBean(Object bean);
/**
* Get bean class
* @return Bean class
*/
public Class getBeanClass();
/**
* Get property value
* @param key - Property name
* @return Property value
*/
@Override
public Object get(Object key);
/**
* Set property value
* @param key - Property name
* @param value - Property value
* @return Previous value
*/
@Override
public Object put(Object key, Object value);
/**
* Get property type
* @param key - Property name
* @return Property type
*/
public Class getPropertyType(String key);
}Usage Examples:
import net.sf.cglib.beans.BeanMap;
// Create bean and wrap with BeanMap
User user = new User("Alice", "alice@example.com", 28);
BeanMap beanMap = BeanMap.create(user);
// Access properties like Map
String name = (String) beanMap.get("name");
String email = (String) beanMap.get("email");
Integer age = (Integer) beanMap.get("age");
// Modify properties
beanMap.put("name", "Alice Smith");
beanMap.put("age", 29);
// Check property types
Class nameType = beanMap.getPropertyType("name"); // Returns String.class
Class ageType = beanMap.getPropertyType("age"); // Returns int.class
// Iterate over properties
for (Object key : beanMap.keySet()) {
System.out.println(key + " = " + beanMap.get(key));
}Runtime generation of JavaBean classes with dynamic properties.
/**
* Runtime generation of bean classes with properties
*/
public class BeanGenerator extends AbstractClassGenerator {
/**
* Add property to generated bean class
* @param name - Property name
* @param type - Property type
*/
public void addProperty(String name, Class type);
/**
* Create instance of generated bean class
* @return Bean instance
*/
public Object create();
/**
* Create instance with specific ClassLoader
* @param loader - ClassLoader to use
* @return Bean instance
*/
public Object createClass(ClassLoader loader);
/**
* Set naming policy for generated class
* @param namingPolicy - Naming policy
*/
public void setNamingPolicy(NamingPolicy namingPolicy);
}Usage Examples:
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
// Generate bean class at runtime
BeanGenerator generator = new BeanGenerator();
generator.addProperty("firstName", String.class);
generator.addProperty("lastName", String.class);
generator.addProperty("age", Integer.class);
// Create instance
Object dynamicBean = generator.create();
// Use BeanMap to set properties
BeanMap beanMap = BeanMap.create(dynamicBean);
beanMap.put("firstName", "Dynamic");
beanMap.put("lastName", "User");
beanMap.put("age", 35);
System.out.println("Generated bean: " + beanMap);Creates immutable wrappers around mutable JavaBean objects.
/**
* Creates immutable wrappers around mutable beans
*/
public class ImmutableBean {
/**
* Create immutable wrapper for given bean
* @param bean - Bean to make immutable
* @return Immutable wrapper
*/
public static Object create(Object bean);
}Usage Examples:
import net.sf.cglib.beans.ImmutableBean;
// Create mutable bean
User mutableUser = new User("John", "john@example.com", 30);
// Create immutable wrapper
User immutableUser = (User) ImmutableBean.create(mutableUser);
// Reading works normally
String name = immutableUser.getName();
String email = immutableUser.getEmail();
// Attempts to modify will throw UnsupportedOperationException
try {
immutableUser.setName("Jane"); // Throws exception
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify immutable bean");
}Provides bulk property access for multiple properties at once, useful for performance-critical scenarios.
/**
* Bulk property access for multiple properties
*/
public abstract class BulkBean {
/**
* Create BulkBean for specific properties of a class
* @param target - Target class
* @param getters - Property getter names
* @param setters - Property setter names
* @param types - Property types
* @return BulkBean instance
*/
public static BulkBean create(Class target, String[] getters, String[] setters, Class[] types);
/**
* Get all property values at once
* @param bean - Bean instance
* @param values - Array to store values (must be correct size)
*/
public abstract void getPropertyValues(Object bean, Object[] values);
/**
* Set all property values at once
* @param bean - Bean instance
* @param values - Values to set
*/
public abstract void setPropertyValues(Object bean, Object[] values);
}Usage Examples:
import net.sf.cglib.beans.BulkBean;
// Define properties for bulk access
String[] getters = {"getName", "getEmail", "getAge"};
String[] setters = {"setName", "setEmail", "setAge"};
Class[] types = {String.class, String.class, int.class};
// Create BulkBean
BulkBean bulkBean = BulkBean.create(User.class, getters, setters, types);
// Get all properties at once
User user = new User("Bob", "bob@example.com", 25);
Object[] values = new Object[3];
bulkBean.getPropertyValues(user, values);
// values now contains ["Bob", "bob@example.com", 25]
// Set all properties at once
Object[] newValues = {"Robert", "robert@example.com", 26};
bulkBean.setPropertyValues(user, newValues);Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib