Class BeanUtil

java.lang.Object
cloud.opencode.base.reflect.bean.BeanUtil

public final class BeanUtil extends Object
Bean Utility Class Bean工具类

Provides low-level bean operation utilities with caching.

提供带缓存的底层bean操作工具。

Features | 主要功能:

  • Getter/setter discovery with caching - 带缓存的getter/setter发现
  • Property name extraction - 属性名提取
  • Bean introspection utilities - Bean内省工具

Usage Examples | 使用示例:

List<Method> getters = BeanUtil.getGetters(User.class);
List<Method> setters = BeanUtil.getSetters(User.class);
String propName = BeanUtil.getPropertyName(getter);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap for caching) - 线程安全: 是(使用ConcurrentHashMap缓存)
  • Null-safe: No (caller must ensure non-null arguments) - 空值安全: 否(调用方须确保非空参数)

Performance | 性能特性:

  • Time complexity: O(1) for cached lookups; O(m) for first access where m is the number of methods - 时间复杂度: 缓存命中时 O(1);首次访问为 O(m),m为方法数量
  • Space complexity: O(m) for the cached getter/setter maps per class - 空间复杂度: O(m),每类缓存 getter/setter 映射
Since:
JDK 25, opencode-base-reflect V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getGetters

      public static List<Method> getGetters(Class<?> clazz)
      Gets all getter methods (cached) 获取所有getter方法(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of getters | getter列表
    • getSetters

      public static List<Method> getSetters(Class<?> clazz)
      Gets all setter methods (cached) 获取所有setter方法(缓存)
      Parameters:
      clazz - the class | 类
      Returns:
      list of setters | setter列表
    • getGetter

      public static Method getGetter(Class<?> clazz, String propertyName)
      Gets getter for a property (cached) 获取属性的getter(缓存)
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      the getter or null | getter或null
    • getSetter

      public static Method getSetter(Class<?> clazz, String propertyName)
      Gets setter for a property (cached) 获取属性的setter(缓存)
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      the setter or null | setter或null
    • getPropertyValue

      public static Object getPropertyValue(Object bean, String propertyName)
      Gets property value using getter 使用getter获取属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      Returns:
      the value | 值
    • getPropertyValueSafe

      public static Optional<Object> getPropertyValueSafe(Object bean, String propertyName)
      Gets property value safely 安全获取属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      Returns:
      Optional of value | 值的Optional
    • setPropertyValue

      public static void setPropertyValue(Object bean, String propertyName, Object value)
      Sets property value using setter 使用setter设置属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      value - the value | 值
    • setPropertyValueSafe

      public static boolean setPropertyValueSafe(Object bean, String propertyName, Object value)
      Sets property value safely 安全设置属性值
      Parameters:
      bean - the bean | bean
      propertyName - the property name | 属性名
      value - the value | 值
      Returns:
      true if successful | 如果成功返回true
    • isGetter

      public static boolean isGetter(Method method)
      Checks if method is a getter 检查方法是否为getter
      Parameters:
      method - the method | 方法
      Returns:
      true if getter | 如果是getter返回true
    • isSetter

      public static boolean isSetter(Method method)
      Checks if method is a setter 检查方法是否为setter
      Parameters:
      method - the method | 方法
      Returns:
      true if setter | 如果是setter返回true
    • extractPropertyName

      public static String extractPropertyName(Method method)
      Extracts property name from getter/setter 从getter/setter提取属性名
      Parameters:
      method - the method | 方法
      Returns:
      the property name or null | 属性名或null
    • getReadablePropertyNames

      public static Set<String> getReadablePropertyNames(Class<?> clazz)
      Gets all readable property names 获取所有可读属性名
      Parameters:
      clazz - the class | 类
      Returns:
      set of property names | 属性名集合
    • getWritablePropertyNames

      public static Set<String> getWritablePropertyNames(Class<?> clazz)
      Gets all writable property names 获取所有可写属性名
      Parameters:
      clazz - the class | 类
      Returns:
      set of property names | 属性名集合
    • getAllPropertyNames

      public static Set<String> getAllPropertyNames(Class<?> clazz)
      Gets all property names (readable or writable) 获取所有属性名(可读或可写)
      Parameters:
      clazz - the class | 类
      Returns:
      set of property names | 属性名集合
    • getPropertyType

      public static Class<?> getPropertyType(Class<?> clazz, String propertyName)
      Gets property type from getter 从getter获取属性类型
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      the type or null | 类型或null
    • isReadable

      public static boolean isReadable(Class<?> clazz, String propertyName)
      Checks if property is readable 检查属性是否可读
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      true if readable | 如果可读返回true
    • isWritable

      public static boolean isWritable(Class<?> clazz, String propertyName)
      Checks if property is writable 检查属性是否可写
      Parameters:
      clazz - the class | 类
      propertyName - the property name | 属性名
      Returns:
      true if writable | 如果可写返回true
    • clearCache

      public static void clearCache()
      Clears all caches 清除所有缓存
    • clearCache

      public static void clearCache(Class<?> clazz)
      Clears cache for specific class 清除特定类的缓存
      Parameters:
      clazz - the class | 类