CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-all

A comprehensive Java utility library providing static method wrappers for common operations to reduce API learning costs and improve development efficiency

Pending
Overview
Eval results
Files

bean-object-manipulation.mddocs/

Bean and Object Manipulation

Comprehensive bean and object utilities through the BeanUtil and ObjectUtil classes, providing property access, copying, conversion, and dynamic manipulation.

Import

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.bean.DynaBean;
import cn.hutool.core.convert.Convert;

Bean Property Operations

Property Access

// Get property values
public static Object getProperty(Object bean, String expression);
public static <T> T getProperty(Object bean, String expression, Class<T> targetType);

// Set property values
public static void setProperty(Object bean, String expression, Object value);
public static void setProperty(Object bean, String expression, Object value, boolean ignoreError);

// Check if property exists
public static boolean hasProperty(Object bean, String expression);

Property Descriptors

// Get property descriptors
public static PropertyDescriptor[] getPropertyDescriptors(Class<?> beanClass);
public static PropertyDescriptor getPropertyDescriptor(Class<?> beanClass, String propertyName);

// Get property names
public static String[] getPropertyNames(Class<?> beanClass);
public static Collection<String> getPropertyNames(Object bean);

Usage Examples:

User user = new User();
user.setName("John");
user.getAddress().setCity("New York");

// Get properties
String name = BeanUtil.getProperty(user, "name");           // "John"
String city = BeanUtil.getProperty(user, "address.city");   // "New York"

// Set properties
BeanUtil.setProperty(user, "age", 30);
BeanUtil.setProperty(user, "address.zipCode", "10001");

// Check property existence
boolean hasName = BeanUtil.hasProperty(user, "name");       // true
boolean hasEmail = BeanUtil.hasProperty(user, "email");     // depends on User class

Bean Copying and Conversion

Bean Copying

// Copy properties between beans
public static void copyProperties(Object source, Object target);
public static void copyProperties(Object source, Object target, String... ignoreProperties);
public static void copyProperties(Object source, Object target, CopyOptions copyOptions);

// Copy with type conversion
public static <T> T copyProperties(Object source, Class<T> targetClass);
public static <T> T copyProperties(Object source, Class<T> targetClass, String... ignoreProperties);

Bean to Map Conversion

// Bean to Map
public static Map<String, Object> beanToMap(Object bean);
public static Map<String, Object> beanToMap(Object bean, boolean ignoreNullValue, 
                                           boolean ignoreError);

// Map to Bean
public static <T> T mapToBean(Map<?, ?> map, Class<T> beanClass, boolean ignoreError);
public static <T> T mapToBean(Map<?, ?> map, Class<T> beanClass, CopyOptions copyOptions);

// Fill bean from map
public static <T> T fillBean(T bean, Map<?, ?> map, boolean ignoreError);

Usage Examples:

// Copy between beans
User sourceUser = new User("John", 30);
User targetUser = new User();
BeanUtil.copyProperties(sourceUser, targetUser);

// Copy to different type
UserDTO dto = BeanUtil.copyProperties(sourceUser, UserDTO.class);

// Copy with exclusions
BeanUtil.copyProperties(sourceUser, targetUser, "password", "lastLogin");

// Bean to Map
Map<String, Object> userMap = BeanUtil.beanToMap(sourceUser);
// Result: {"name": "John", "age": 30, ...}

// Map to Bean
Map<String, Object> data = MapUtil.of("name", "Jane", "age", 25);
User user = BeanUtil.mapToBean(data, User.class, false);

Dynamic Bean (DynaBean)

DynaBean Class

public class DynaBean {
    // Creation  
    public static DynaBean create(Object bean);
    public static DynaBean create(Class<?> beanClass, Object... params);
    
    // Property access
    public Object get(String name);
    public <T> T get(String name, Class<T> type);
    public DynaBean set(String name, Object value);
    
    // Invoke methods
    public Object invoke(String methodName, Object... params);
    
    // Check existence
    public boolean containsKey(String name);
    
    // Get underlying bean
    public Object getBean();
    public Class<?> getBeanClass();
}

Usage Examples:

// Create dynamic bean wrapper
User user = new User();
DynaBean dynaBean = DynaBean.create(user);

// Dynamic property access
dynaBean.set("name", "John")
        .set("age", 30)
        .set("email", "john@example.com");

String name = dynaBean.get("name", String.class);

// Dynamic method invocation
dynaBean.invoke("setActive", true);
Boolean active = dynaBean.invoke("isActive");

// Create bean dynamically
DynaBean newBean = DynaBean.create(User.class);
newBean.set("name", "Dynamic User");
User actualUser = (User) newBean.getBean();

Object Utilities

Object Validation

// Null checks
public static boolean isNull(Object obj);
public static boolean isNotNull(Object obj);
public static boolean hasNull(Object... objs);
public static boolean isAllNull(Object... objs);

// Empty checks (for collections, arrays, strings)
public static boolean isEmpty(Object obj);
public static boolean isNotEmpty(Object obj);

// Default value operations
public static <T> T defaultIfNull(T object, T defaultValue);
public static <T> T defaultIfEmpty(T object, T defaultValue);

Object Comparison

// Equality checks
public static boolean equals(Object obj1, Object obj2);
public static boolean notEquals(Object obj1, Object obj2);

// Length/size operations
public static int length(Object obj);
public static boolean contains(Object obj, Object element);

Object Cloning

// Deep cloning
public static <T> T clone(T obj);
public static <T> T cloneIfPossible(T obj);

// Shallow cloning
public static <T> T cloneByStream(T obj);

Usage Examples:

// Null safety
String value = ObjectUtil.defaultIfNull(getName(), "Unknown");
boolean hasData = ObjectUtil.isNotEmpty(getDataList());

// Safe comparison
boolean same = ObjectUtil.equals(user1.getId(), user2.getId());

// Object length
int size = ObjectUtil.length(userList);      // Works with collections
int arrayLen = ObjectUtil.length(userArray); // Works with arrays
int strLen = ObjectUtil.length("hello");     // Works with strings

// Deep cloning
User originalUser = new User("John", Arrays.asList("skill1", "skill2"));
User clonedUser = ObjectUtil.clone(originalUser);
clonedUser.getSkills().add("skill3"); // Won't affect original

Type Conversion

Convert Class

// Basic type conversion
public static <T> T convert(Class<T> type, Object value);
public static <T> T convert(Class<T> type, Object value, T defaultValue);

// Collection conversion
public static <T> List<T> toList(Class<T> componentType, Object value);
public static <T> Set<T> toSet(Class<T> componentType, Object value);

// Array conversion
public static <T> T[] toArray(Class<T> componentType, Object value);

// Map conversion
public static <K, V> Map<K, V> toMap(Class<K> keyType, Class<V> valueType, Object value);

Usage Examples:

// Type conversion
String numberStr = "123";
Integer number = Convert.convert(Integer.class, numberStr);

Date dateValue = Convert.convert(Date.class, "2023-12-25");
Boolean boolValue = Convert.convert(Boolean.class, "true");

// Collection conversion
List<String> stringValues = Arrays.asList("1", "2", "3");
List<Integer> intValues = Convert.toList(Integer.class, stringValues);

// Array conversion
String[] stringArray = {"1", "2", "3"};
Integer[] intArray = Convert.toArray(Integer.class, stringArray);

// Default values
Integer result = Convert.convert(Integer.class, "invalid", 0);

Copy Options and Configuration

CopyOptions Class

public class CopyOptions {
    // Creation
    public static CopyOptions create();
    
    // Configuration
    public CopyOptions setIgnoreNullValue(boolean ignoreNullValue);
    public CopyOptions setIgnoreError(boolean ignoreError);
    public CopyOptions setIgnoreCase(boolean ignoreCase);
    public CopyOptions setFieldNameEditor(Editor<String> fieldNameEditor);
    public CopyOptions setPropertiesFilter(Filter<Field> propertiesFilter);
    
    // Field mapping
    public CopyOptions setFieldMapping(Map<String, String> fieldMapping);
    public CopyOptions addFieldMapping(String srcFieldName, String destFieldName);
    
    // Ignored properties
    public CopyOptions setIgnoreProperties(String... ignoreProperties);
}

Usage Examples:

// Custom copy options
CopyOptions options = CopyOptions.create()
    .setIgnoreNullValue(true)
    .setIgnoreError(true)
    .setIgnoreCase(true)
    .addFieldMapping("userName", "name")
    .setIgnoreProperties("password", "lastLogin");

// Copy with custom options
UserDTO dto = new UserDTO();
BeanUtil.copyProperties(user, dto, options);

// Field name transformation
CopyOptions transformOptions = CopyOptions.create()
    .setFieldNameEditor(fieldName -> StrUtil.toCamelCase(fieldName));

BeanUtil.copyProperties(sourceMap, targetBean, transformOptions);

Bean Validation and Introspection

Bean Utilities

// Check if object is a bean
public static boolean isBean(Class<?> clazz);
public static boolean hasProperty(Class<?> beanClass, String propertyName);

// Get readable/writable properties
public static Set<String> getReadablePropertyNames(Class<?> beanClass);
public static Set<String> getWritablePropertyNames(Class<?> beanClass);

// Property type information
public static Class<?> getPropertyType(Class<?> beanClass, String propertyName);

All bean operations handle edge cases gracefully:

  • Null Safety: Methods handle null beans and properties appropriately
  • Type Safety: Automatic type conversion where possible
  • Error Handling: Options to ignore errors or throw exceptions
  • Performance: Efficient caching of reflection metadata
  • Flexibility: Support for nested properties using dot notation

The bean utilities provide a complete solution for working with Java objects dynamically, supporting both compile-time type safety and runtime flexibility.

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-all

docs

additional-utilities.md

bean-object-manipulation.md

collection-utilities.md

core-string-operations.md

cryptographic-operations.md

database-access.md

date-time-handling.md

file-io-operations.md

http-client-operations.md

index.md

json-processing.md

tile.json