Code generation library for creating dynamic proxies, bean utilities, and fast reflection alternatives with bundled dependencies
—
High-performance JavaBean manipulation utilities including property copying, dynamic bean generation, Map-based bean access, and bulk operations. These utilities provide alternatives to reflection-based bean operations with significantly better performance.
High-performance copying of JavaBean properties between objects of different types.
/**
* High-performance copying of JavaBean properties between objects
*/
public abstract class BeanCopier {
/**
* Create a BeanCopier for copying between specific types
* @param source Source class
* @param target Target class
* @param useConverter Whether to use a Converter
* @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 property transformation (or null)
*/
public abstract void copy(Object from, Object to, Converter converter);
}Usage Examples:
// Simple property copying
BeanCopier copier = BeanCopier.create(SourceBean.class, TargetBean.class, false);
SourceBean source = new SourceBean();
source.setName("John");
source.setAge(30);
TargetBean target = new TargetBean();
copier.copy(source, target, null);
// target now has name="John", age=30
// Using converter for type transformation
BeanCopier converterCopier = BeanCopier.create(User.class, UserDTO.class, true);
Converter dateConverter = new Converter() {
public Object convert(Object value, Class target, Object context) {
if (value instanceof Date && target == String.class) {
return new SimpleDateFormat("yyyy-MM-dd").format((Date) value);
}
return value;
}
};
User user = new User();
user.setBirthDate(new Date());
UserDTO dto = new UserDTO();
converterCopier.copy(user, dto, dateConverter);Provides a Map-based view of JavaBean objects, allowing property access via Map operations.
/**
* Map-based view of JavaBean objects
*/
public abstract class BeanMap implements Map {
// Property access flags
public static final int REQUIRE_GETTER = 1;
public static final int REQUIRE_SETTER = 2;
/**
* Create BeanMap for an object
* @param bean Target bean object
* @return BeanMap instance
*/
public static BeanMap create(Object bean);
/**
* Create new BeanMap instance for different bean
* @param bean New target bean
* @return New BeanMap instance
*/
public abstract BeanMap newInstance(Object bean);
/**
* Get property type
* @param name Property name
* @return Property type
*/
public abstract Class getPropertyType(String name);
/**
* Get property value from specific bean
* @param bean Target bean
* @param key Property name
* @return Property value
*/
public abstract Object get(Object bean, Object key);
/**
* Set property value on specific bean
* @param bean Target bean
* @param key Property name
* @param value New value
* @return Previous value
*/
public abstract Object put(Object bean, Object key, Object value);
/**
* Set the target bean for this map
* @param bean Target bean
*/
public void setBean(Object bean);
/**
* Get the current target bean
* @return Current bean
*/
public Object getBean();
// Map interface methods inherited:
// Object get(Object key)
// Object put(Object key, Object value)
// boolean containsKey(Object key)
// boolean containsValue(Object value)
// int size()
// boolean isEmpty()
// Set keySet()
// Collection values()
// Set entrySet()
// void clear()
// Object remove(Object key)
// void putAll(Map m)
}Usage Examples:
// Use bean as a Map
Person person = new Person("Alice", 25);
BeanMap map = BeanMap.create(person);
// Get properties via Map interface
String name = (String) map.get("name"); // "Alice"
Integer age = (Integer) map.get("age"); // 25
// Set properties via Map interface
map.put("name", "Bob");
map.put("age", 30);
// person object now has name="Bob", age=30
// Iterate over properties
for (Object key : map.keySet()) {
System.out.println(key + " = " + map.get(key));
}
// Check property types
Class nameType = map.getPropertyType("name"); // String.class
Class ageType = map.getPropertyType("age"); // Integer.class
// Use same BeanMap with different bean
Person person2 = new Person();
BeanMap map2 = map.newInstance(person2);
map2.put("name", "Charlie");Dynamically creates JavaBean classes with specified properties at runtime.
/**
* Dynamically creates JavaBean classes with properties
*/
public class BeanGenerator {
/**
* Default constructor
*/
public BeanGenerator();
/**
* Set superclass for generated bean
* @param superclass Superclass to extend
*/
public void setSuperclass(Class superclass);
/**
* Add property to generated bean
* @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 the bean class (without instantiating)
* @return Generated class
*/
public Object createClass();
/**
* Add properties from a Map
* @param gen BeanGenerator instance
* @param props Map of property names to types
*/
public static void addProperties(BeanGenerator gen, Map props);
/**
* Add properties from existing class
* @param gen BeanGenerator instance
* @param type Class to copy properties from
*/
public static void addProperties(BeanGenerator gen, Class type);
/**
* Add properties from PropertyDescriptors
* @param gen BeanGenerator instance
* @param descriptors Array of PropertyDescriptors
*/
public static void addProperties(BeanGenerator gen, PropertyDescriptor[] descriptors);
}Usage Examples:
// Create dynamic bean class
BeanGenerator generator = new BeanGenerator();
generator.addProperty("name", String.class);
generator.addProperty("age", Integer.class);
generator.addProperty("active", Boolean.class);
// Create instance
Object bean = generator.create();
// Use with BeanMap for property access
BeanMap map = BeanMap.create(bean);
map.put("name", "Dynamic Bean");
map.put("age", 42);
map.put("active", true);
System.out.println(map.get("name")); // "Dynamic Bean"
// Create bean extending existing class
BeanGenerator extendingGen = new BeanGenerator();
extendingGen.setSuperclass(BaseEntity.class);
extendingGen.addProperty("customField", String.class);
Object extendedBean = extendingGen.create();
// Add properties from existing class
BeanGenerator copyGen = new BeanGenerator();
BeanGenerator.addProperties(copyGen, Person.class);
copyGen.addProperty("extraField", Date.class);
Object enrichedBean = copyGen.create();
// Add properties from Map
Map<String, Class> propertyMap = new HashMap<>();
propertyMap.put("title", String.class);
propertyMap.put("score", Double.class);
BeanGenerator mapGen = new BeanGenerator();
BeanGenerator.addProperties(mapGen, propertyMap);
Object mapBean = mapGen.create();Efficient bulk property access operations for JavaBeans using arrays.
/**
* Efficient bulk property access for JavaBeans
*/
public abstract class BulkBean {
/**
* Create BulkBean for specific class and properties
* @param target Target class
* @param getters Array of getter method names
* @param setters Array of setter method names
* @param types Array of property types
* @return BulkBean instance
*/
public static BulkBean create(Class target, String[] getters, String[] setters, Class[] types);
/**
* Get all property values into array
* @param bean Target bean
* @param values Array to fill with values
*/
public abstract void getPropertyValues(Object bean, Object[] values);
/**
* Set all property values from array
* @param bean Target bean
* @param values Array of values to set
*/
public abstract void setPropertyValues(Object bean, Object[] values);
/**
* Get property values as new array
* @param bean Target bean
* @return Array of property values
*/
public Object[] getPropertyValues(Object bean);
/**
* Get property types
* @return Array of property types
*/
public Class[] getPropertyTypes();
/**
* Get getter method names
* @return Array of getter names
*/
public String[] getGetters();
/**
* Get setter method names
* @return Array of setter names
*/
public String[] getSetters();
}Usage Examples:
// Create BulkBean for efficient property access
BulkBean bulkBean = BulkBean.create(
Person.class,
new String[]{"getName", "getAge", "getEmail"}, // getters
new String[]{"setName", "setAge", "setEmail"}, // setters
new Class[]{String.class, Integer.class, String.class} // types
);
Person person = new Person("John", 30, "john@example.com");
// Get all values at once
Object[] values = new Object[3];
bulkBean.getPropertyValues(person, values);
// values[0] = "John", values[1] = 30, values[2] = "john@example.com"
// Or get as new array
Object[] allValues = bulkBean.getPropertyValues(person);
// Set all values at once
Person newPerson = new Person();
bulkBean.setPropertyValues(newPerson, new Object[]{"Jane", 25, "jane@example.com"});
// Useful for batch operations
List<Person> people = getPersonList();
for (Person p : people) {
Object[] personValues = bulkBean.getPropertyValues(p);
// Process values in bulk
processValues(personValues);
}Exception thrown during bulk bean operations.
/**
* Exception for bulk bean operations
*/
public class BulkBeanException extends RuntimeException {
/**
* Constructor with message and index
* @param message Error message
* @param index Index of failing property
*/
public BulkBeanException(String message, int index);
/**
* Constructor with cause and index
* @param cause Underlying cause
* @param index Index of failing property
*/
public BulkBeanException(Throwable cause, int index);
/**
* Get the index of the failing property
* @return Property index
*/
public int getIndex();
/**
* Get the underlying cause
* @return Throwable cause
*/
public Throwable getCause();
}Creates immutable wrappers around mutable JavaBeans.
/**
* Creates immutable wrappers around mutable beans
*/
public class ImmutableBean {
/**
* Create immutable wrapper for a bean
* @param bean Bean to wrap
* @return Immutable wrapper
*/
public static Object create(Object bean);
}Usage Example:
// Create mutable bean
Person mutablePerson = new Person("Alice", 30);
mutablePerson.setAge(31); // This works
// Create immutable wrapper
Person immutablePerson = (Person) ImmutableBean.create(mutablePerson);
// Reading works fine
String name = immutablePerson.getName(); // "Alice"
int age = immutablePerson.getAge(); // 31
// But writing throws exception
try {
immutablePerson.setAge(32); // Throws exception
} catch (Exception e) {
System.out.println("Cannot modify immutable bean");
}
// Original bean is still mutable
mutablePerson.setAge(35); // Still worksUtility class providing an immutable set implementation used internally by BeanMap.
/**
* Immutable set implementation for BeanMap keys
*/
public class FixedKeySet extends AbstractSet {
/**
* Constructor
* @param keys Array of keys
*/
public FixedKeySet(String[] keys);
/**
* Get iterator over keys
* @return Iterator
*/
public Iterator iterator();
/**
* Get set size
* @return Size
*/
public int size();
}/**
* Interface for converting values during bean copying
*/
public interface Converter {
/**
* Convert a value
* @param value Source value
* @param target Target type
* @param context Context information (property name)
* @return Converted value
*/
Object convert(Object value, Class target, Object context);
}Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib-nodep