Class BeanUtils
- java.lang.Object
-
- io.microsphere.beans.BeanUtils
-
-
Field Summary
Fields Modifier and Type Field Description static intBEAN_METADATA_CACHE_SIZEThe cache size ofBeanMetadatastatic java.lang.StringBEAN_METADATA_CACHE_SIZE_PROPERTY_NAMEThe property name of cache size ofBeanMetadata: "microsphere.bean.metadata.cache.size"static intBEAN_PROPERTIES_MAX_RESOLVED_DEPTHThe maximum levels of resolving properties of a bean, default is 100static java.lang.StringBEAN_PROPERTIES_MAX_RESOLVED_DEPTH_PROPERTY_NAMEThe property name for the maximum depth of resolving properties of a bean.static intDEFAULT_BEAN_METADATA_CACHE_SIZEThe default cache size ofBeanMetadata, default value : 64static intDEFAULT_BEAN_PROPERTIES_MAX_RESOLVED_DEPTHThe default maximum levels of resolving properties of a bean, default is 100
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.beans.PropertyDescriptorfindPropertyDescriptor(BeanMetadata beanMetadata, java.lang.String propertyName)Finds thePropertyDescriptorfor a specified property of a given bean.static java.lang.reflect.MethodfindWriteMethod(BeanMetadata beanMetadata, java.lang.String propertyName)Finds the write method (setter) for a specified property of a given bean.static BeanMetadatagetBeanMetadata(java.lang.Class<?> beanClass)Get theBeanMetadatafor the specifiedClass.static java.util.Map<java.lang.String,java.lang.Object>resolvePropertiesAsMap(java.lang.Object bean)Resolves the properties of a given Java Bean and returns them as aMap.static java.util.Map<java.lang.String,java.lang.Object>resolvePropertiesAsMap(java.lang.Object bean, int maxResolvedDepth)Resolves the properties of a given Java Bean up to a specified maximum depth and returns them as aMap.protected static java.util.Map<java.lang.String,java.lang.Object>resolvePropertiesAsMap(java.lang.Object bean, MutableInteger resolvedDepth, int maxResolvedDepth)Resolves the properties of a given Java Bean recursively up to a specified maximum depth, tracking the current depth with aMutableInteger, and returns them as aMap.
-
-
-
Field Detail
-
BEAN_PROPERTIES_MAX_RESOLVED_DEPTH_PROPERTY_NAME
public static final java.lang.String BEAN_PROPERTIES_MAX_RESOLVED_DEPTH_PROPERTY_NAME
The property name for the maximum depth of resolving properties of a bean.This property is used to configure the maximum depth to which nested properties of a Java Bean will be resolved. This helps in preventing stack overflow errors when dealing with deeply nested objects. The default value is defined by
DEFAULT_BEAN_PROPERTIES_MAX_RESOLVED_DEPTH_PROPERTY_VALUE.Example Usage
// Setting the system property to limit the resolution depth System.setProperty(BEAN_PROPERTIES_MAX_RESOLVED_DEPTH_PROPERTY_NAME, "50");- See Also:
- Constant Field Values
-
DEFAULT_BEAN_PROPERTIES_MAX_RESOLVED_DEPTH
public static final int DEFAULT_BEAN_PROPERTIES_MAX_RESOLVED_DEPTH
The default maximum levels of resolving properties of a bean, default is 100
-
BEAN_PROPERTIES_MAX_RESOLVED_DEPTH
@ConfigurationProperty(name="microsphere.bean.properties.max-resolved-depth", defaultValue="100", description="The maximum depth of resolving properties of a bean in order to avoid stack overflow, default is 100") public static final int BEAN_PROPERTIES_MAX_RESOLVED_DEPTH
The maximum levels of resolving properties of a bean, default is 100
-
DEFAULT_BEAN_METADATA_CACHE_SIZE
public static final int DEFAULT_BEAN_METADATA_CACHE_SIZE
The default cache size ofBeanMetadata, default value : 64
-
BEAN_METADATA_CACHE_SIZE_PROPERTY_NAME
public static final java.lang.String BEAN_METADATA_CACHE_SIZE_PROPERTY_NAME
The property name of cache size ofBeanMetadata: "microsphere.bean.metadata.cache.size"- See Also:
- Constant Field Values
-
BEAN_METADATA_CACHE_SIZE
@ConfigurationProperty(name="microsphere.bean.metadata.cache.size", defaultValue="64", description="The cache size of BeanMetadata, default is 64") public static final int BEAN_METADATA_CACHE_SIZE
The cache size ofBeanMetadata
-
-
Method Detail
-
resolvePropertiesAsMap
@Nonnull @Immutable public static java.util.Map<java.lang.String,java.lang.Object> resolvePropertiesAsMap(java.lang.Object bean)
Resolves the properties of a given Java Bean and returns them as aMap.This method introspects the provided bean, extracts its properties using
PropertyDescriptors, and constructs a map where each key is the uncapitalized property name and each value is the resolved property value. The resolution process handles nested objects, arrays, lists, sets, and maps recursively up to a maximum depth defined byBEAN_PROPERTIES_MAX_RESOLVED_DEPTH.Example Usage
public class Person { private String name; private Address address; // Getters and setters... } public class Address { private String city; // Getters and setters... } Person person = new Person(); person.setName("John Doe"); Address address = new Address(); address.setCity("New York"); person.setAddress(address); Map<String, Object> properties = BeanUtils.resolvePropertiesAsMap(person); // Resulting map: // { // "name": "John Doe", // "address": { // "city": "New York" // } // }- Parameters:
bean- the Java Bean whose properties are to be resolved; may benull- Returns:
- an unmodifiable
Mapcontaining the resolved properties, or an empty map if the bean isnullor maximum resolution depth is reached - See Also:
resolvePropertiesAsMap(Object, int),BEAN_PROPERTIES_MAX_RESOLVED_DEPTH
-
resolvePropertiesAsMap
@Nonnull @Immutable public static java.util.Map<java.lang.String,java.lang.Object> resolvePropertiesAsMap(java.lang.Object bean, int maxResolvedDepth)
Resolves the properties of a given Java Bean up to a specified maximum depth and returns them as aMap.This method introspects the provided bean, extracts its properties using
PropertyDescriptors, and constructs a map where each key is the uncapitalized property name and each value is the resolved property value. The resolution process handles nested objects, arrays, lists, sets, and maps recursively, but will stop resolving further once the specifiedmaxResolvedDepthis reached.Example Usage
public class Person { private String name; private Address address; // Getters and setters... } public class Address { private String city; private Country country; // Getters and setters... } public class Country { private String name; // Getters and setters... } Person person = new Person(); person.setName("John Doe"); Address address = new Address(); address.setCity("New York"); Country country = new Country(); country.setName("USA"); address.setCountry(country); person.setAddress(address); Map<String, Object> properties = BeanUtils.resolvePropertiesAsMap(person, 2); // Resulting map (depth limited to 2): // { // "name": "John Doe", // "address": { // "city": "New York", // "country": {} // country's properties not resolved due to depth limit // } // }- Parameters:
bean- the Java Bean whose properties are to be resolved; may benullmaxResolvedDepth- the maximum depth to which nested properties should be resolved; must be non-negative- Returns:
- an unmodifiable
Mapcontaining the resolved properties, or an empty map if the bean isnullor maximum resolution depth is reached - Throws:
java.lang.IllegalArgumentException- ifmaxResolvedDepthis negative- See Also:
resolvePropertiesAsMap(Object),resolvePropertiesAsMap(Object, MutableInteger, int)
-
getBeanMetadata
@Nonnull public static BeanMetadata getBeanMetadata(java.lang.Class<?> beanClass) throws java.lang.RuntimeException
Get theBeanMetadatafor the specifiedClass.- Parameters:
beanClass- the specifiedClass- Returns:
- non-null
- Throws:
java.lang.RuntimeException- if an exception occurs during introspection
-
findWriteMethod
public static java.lang.reflect.Method findWriteMethod(BeanMetadata beanMetadata, java.lang.String propertyName)
Finds the write method (setter) for a specified property of a given bean.This method retrieves the
PropertyDescriptorfor the specified property from the providedBeanMetadata. If the property descriptor exists and has a write method, that method is returned. Otherwise,nullis returned.Example Usage
// Example 1: Finding a write method for a simple property BeanMetadata metadata = BeanMetadata.of(Person.class); Method setNameMethod = BeanUtils.findWriteMethod(metadata, "name"); if (setNameMethod != null) { System.out.println("Found setter method: " + setNameMethod.getName()); } // Example 2: Handling a property with no setter Method readOnlyPropertyMethod = BeanUtils.findWriteMethod(metadata, "readOnlyProperty"); if (readOnlyPropertyMethod == null) { System.out.println("No setter method found for 'readOnlyProperty'"); }- Parameters:
beanMetadata- the metadata of the bean to introspect; must not benullpropertyName- the name of the property for which to find the write method; must not benullor empty- Returns:
- the write method (setter) for the specified property, or
nullif no such method exists or the property is not found - Throws:
java.lang.IllegalArgumentException- ifbeanMetadataorpropertyNameisnull
-
findPropertyDescriptor
public static java.beans.PropertyDescriptor findPropertyDescriptor(BeanMetadata beanMetadata, java.lang.String propertyName)
Finds thePropertyDescriptorfor a specified property of a given bean.This method retrieves the
PropertyDescriptorfor the specified property from the providedBeanMetadata. If the property descriptor exists, it is returned. Otherwise,nullis returned. If the property descriptor is not found, a trace log message will be recorded.Example Usage
// Example 1: Finding a property descriptor for a simple property BeanMetadata metadata = BeanMetadata.of(Person.class); PropertyDescriptor nameDescriptor = BeanUtils.findPropertyDescriptor(metadata, "name"); if (nameDescriptor != null) { System.out.println("Found property descriptor: " + nameDescriptor.getName()); } // Example 2: Handling a non-existent property PropertyDescriptor nonExistentDescriptor = BeanUtils.findPropertyDescriptor(metadata, "nonExistentProperty"); if (nonExistentDescriptor == null) { System.out.println("Property descriptor not found for 'nonExistentProperty'"); }- Parameters:
beanMetadata- the metadata of the bean to introspect; must not benullpropertyName- the name of the property for which to find the descriptor; must not benullor empty- Returns:
- the
PropertyDescriptorfor the specified property, ornullif no such descriptor exists or the property is not found - Throws:
java.lang.IllegalArgumentException- ifbeanMetadataorpropertyNameisnull
-
resolvePropertiesAsMap
@Nonnull @Immutable protected static java.util.Map<java.lang.String,java.lang.Object> resolvePropertiesAsMap(java.lang.Object bean, MutableInteger resolvedDepth, int maxResolvedDepth)
Resolves the properties of a given Java Bean recursively up to a specified maximum depth, tracking the current depth with aMutableInteger, and returns them as aMap.This method introspects the provided bean, extracts its properties using
PropertyDescriptors, and constructs a map where each key is the uncapitalized property name and each value is the resolved property value. The resolution process handles nested objects, arrays, lists, sets, and maps recursively, but will stop resolving further once the specifiedmaxResolvedDepthis reached. TheresolvedDepthparameter is used to track the current depth of recursion and is incremented upon each call.Example Usage
public class Person { private String name; private Address address; // Getters and setters... } public class Address { private String city; private Country country; // Getters and setters... } public class Country { private String name; // Getters and setters... } Person person = new Person(); person.setName("John Doe"); Address address = new Address(); address.setCity("New York"); Country country = new Country(); country.setName("USA"); address.setCountry(country); person.setAddress(address); MutableInteger depth = MutableInteger.of(0); Map<String, Object> properties = BeanUtils.resolvePropertiesAsMap(person, depth, 3); // Resulting map (depth limited to 3): // { // "name": "John Doe", // "address": { // "city": "New York", // "country": { // "name": "USA" // } // } // }- Parameters:
bean- the Java Bean whose properties are to be resolved; may benullresolvedDepth- the current depth of property resolution, incremented on each recursive call; must not benullmaxResolvedDepth- the maximum depth to which nested properties should be resolved; must be non-negative- Returns:
- an unmodifiable
Mapcontaining the resolved properties, or an empty map if the bean isnullor maximum resolution depth is reached - Throws:
java.lang.IllegalArgumentException- ifmaxResolvedDepthis negative- See Also:
resolvePropertiesAsMap(Object),resolvePropertiesAsMap(Object, int)
-
-