Class BeanMeta<T>
- Type Parameters:
T- The class type that this metadata applies to.
BeanInfo).
Description
Uses introspection to find all the properties associated with this class. If the@BeanType annotation is present on the class (or the marshalling-side
@Marshalled sibling), that information is used to determine the properties on the class.
Otherwise, the BeanInfo functionality in Java is used to determine the properties on the class.
Bean property ordering
The order of the properties is as follows:-
If
@BeanType(properties)is specified on the class, the order matches the list of properties in the annotation. - Otherwise, by default, properties are sorted alphabetically.
-
When unsorted properties are enabled (
BeanConfigContext.isUnsortedProperties(),@BeanType(unsorted=true), or a bean filter), properties use declaration order:- Records: record component order.
- Other classes: field declaration order, superclass fields first. Getter/setter properties are ranked by their same-named field (even a private one), which is used as an ordering hint only.
- Method-only properties (no backing field) follow, alphabetically.
-
BeanConfigContext.isRecordComponentOrder()gives records component order even when other beans are sorted.
Field order comes from Class.getDeclaredFields(), which is declaration order on HotSpot and OpenJ9 but is
not guaranteed by the JLS.
Thread safety
Instances are effectively immutable after construction and are safe for concurrent read access. Construction is not thread-safe and should be completed before sharing an instance across threads.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents the result of creating a BeanMeta, including the bean metadata and any reason why it's not a bean. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBeanMeta(Class<T> beanClass, BeanConfigContext config) Bean-modeling constructor — builds aBeanMetawithout marshalling context state.protectedConstructor.protectedBeanMeta(BeanInfo<T> cm, BeanFilter bf, String[] pNames, ClassInfo implClass, PropertyNamer propertyNamerOverride) Same asBeanMeta(BeanInfo, BeanFilter, String[], ClassInfo)but allows an explicitPropertyNameroverride. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> BeanMeta.BeanMetaValue<T>Creates aBeanMetainstance for the specified bean type info.static <T> BeanMeta.BeanMetaValue<T>create(BeanInfo<T> cm, ClassInfo implClass, PropertyNamer propertyNamer) Same ascreate(BeanInfo, ClassInfo)but allows an explicitPropertyNamerto override the one that would otherwise be derived from the bean filter orBeanConfigContext.getPropertyNamer().booleanReturns the bean filter associated with this bean.Returns theBeanInfoof this bean.Returns the proxy invocation handler for this bean if it's an interface.Returns the bean registry for this bean.protected BeanTypeResolverReturns the bean-modelingBeanTypeResolverthat this metadata was constructed against.Returns theClassInfoof this bean.Returns the bean-modeling configuration snapshot used to build thisBeanMeta.Returns the constructor for this bean, if one was found.Returns the list of property names that correspond to the constructor parameters.Returns the dictionary name for this bean as defined through the@Marshalled(typeName)annotation.protected BeanPropertyMetaReturns the "extras" property for dynamic bean properties.Returns the map of getter methods to property names.protected Map<String,BeanPropertyMeta> Returns the map of hidden properties on this bean.Returns a map of all properties on this bean.Returns the per-property registry lookup associated with the givenBeanPropertyMeta.getPropertyMeta(String name) Returns metadata about the specified property.Returns the names of the properties marked@BeanProp(required=true).Returns the map of setter methods to property names.Returns a mock bean property that resolves to the name"_type" and whose value always resolves to the dictionary name of the bean.Returns the type property name for this bean.booleanReturns whether this bean has a constructor available for instantiation.inthashCode()booleanReturns whether any property of this bean is marked@BeanProp(required=true).protected booleanReturns whether this bean has opted out of alphabetical property sorting.Creates a new instance of this bean.static <T> BeanMeta<T>Same asof(Class, BeanConfigContext)usingBeanConfigContext.DEFAULT.static <T> BeanMeta<T>of(Class<T> beanClass, BeanConfigContext config) Creates aBeanMetafor the specified class using the suppliedBeanConfigContext.onReadProperty(Object bean, String name, Object value) Property read interceptor.onWriteProperty(Object bean, String name, Object value) Property write interceptor.toString()
-
Constructor Details
-
BeanMeta
Constructor.Creates a new
BeanMetainstance for the specified class metadata. This constructor performs introspection to discover all bean properties, methods, and fields in the class hierarchy.The bean metadata is built by:
- Finding all bean fields in the class hierarchy
- Finding all bean methods (getters, setters, extraKeys) in the class hierarchy
- Determining the appropriate constructor for bean instantiation
- Building the class hierarchy for property discovery
- Creating the bean registry for dictionary name resolution
- Validating and filtering properties based on bean filter settings
- Parameters:
cm- The class metadata for the bean class. Must not benull .bf- Optional bean filter to apply. Can benull (no bean filter customizations are applied; the default stop class ofObjectis used).pNames- Explicit list of property names and order. Ifnull , properties are determined automatically.implClass- Optional implementation class constructor to use if one cannot be found. Can benull (the bean class's own no-arg constructor is used instead).
-
BeanMeta
protected BeanMeta(BeanInfo<T> cm, BeanFilter bf, String[] pNames, ClassInfo implClass, PropertyNamer propertyNamerOverride) Same asBeanMeta(BeanInfo, BeanFilter, String[], ClassInfo)but allows an explicitPropertyNameroverride.- Parameters:
cm- The class metadata for the bean class. Must not benull .bf- Optional bean filter to apply. Can benull (no bean filter customizations are applied; the default stop class ofObjectis used).pNames- Explicit list of property names and order. Ifnull , properties are determined automatically.implClass- Optional implementation class constructor to use if one cannot be found. Can benull (the bean class's own no-arg constructor is used instead).propertyNamerOverride- Optional namer that, when non-null , takes precedence over the bean filter andBeanConfigContext.getPropertyNamer()for deriving property names.
-
BeanMeta
Bean-modeling constructor — builds aBeanMetawithout marshalling context state.See
of(Class, BeanConfigContext)for the public entry point. ThegetBeanInfo(),bean type resolver, and per-property registry lookups are leftnull ; the per-propertyrawTypeMeta/typeMetafields are alsonull (no type-resolution is performed in this path).- Parameters:
beanClass- The bean class. Must not benull .config- The bean-modeling configuration. Must not benull .
-
-
Method Details
-
create
Creates aBeanMetainstance for the specified bean type info.This is the marshalling-side factory entry point. It attempts to create bean metadata for a class; if the class is determined to be a bean, the returned
BeanMeta.BeanMetaValuecarries theBeanMetainstance and anull reason. Otherwise it carriesnull for the bean metadata and a non-empty string explaining why the class is not a bean.The bean filter is resolved internally from the
@Marshalled/@BeanTypeannotations on the class viaBeanMetaInitializer.buildBeanFilter(BeanInfo). Seeof(Class, BeanConfigContext)for the pure bean-modeling-side entry point that does not require aBeanInfo.Exception handling:
If a
RuntimeExceptionis thrown during bean metadata creation, it is caught and the exception message is returned as thenotABeanReasonwithnull for the bean metadata.Example:
// Create bean metadata for a class via the marshalling-side context. BeanInfo<Person>cm =marshallingContext .getClassMeta(Person.class ); BeanMetaHolder<Person>result = BeanMeta.create (cm ,null );// Check if it's a bean if (result .beanMeta() !=null ) { BeanMeta<Person>bm =result .beanMeta();// Use the bean metadata... }else { Stringreason =result .notABeanReason();// Handle the case where it's not a bean... }- Type Parameters:
T- The class type.- Parameters:
cm- The bean type info for the class to create bean metadata for. Must not benull .implClass- Optional implementation class info to use when looking for a no-arg constructor. Can benull (the bean class's own no-arg constructor is used instead).- Returns:
- A
BeanMeta.BeanMetaValuecontaining the bean metadata (if successful) or a reason why it's not a bean.
-
create
public static <T> BeanMeta.BeanMetaValue<T> create(BeanInfo<T> cm, ClassInfo implClass, PropertyNamer propertyNamer) Same ascreate(BeanInfo, ClassInfo)but allows an explicitPropertyNamerto override the one that would otherwise be derived from the bean filter orBeanConfigContext.getPropertyNamer().The returned
BeanMetais not cached anywhere — callers that need a namer-specific view (for example,MarshallingSession#toBeanMap(Object, PropertyNamer)) build one on demand so the shared,Class-keyed defaultBeanMetacache is never polluted with alternate-namer property keys.- Type Parameters:
T- The class type.- Parameters:
cm- The bean type info for the class to create bean metadata for. Must not benull .implClass- Optional implementation class info to use when looking for a no-arg constructor. Can benull (the bean class's own no-arg constructor is used instead).propertyNamer- Optional property namer to use for deriving property names. Ifnull , the namer is resolved the normal way (bean filter, thenBeanConfigContext.getPropertyNamer()).- Returns:
- A
BeanMeta.BeanMetaValuecontaining the bean metadata (if successful) or a reason why it's not a bean.
-
of
Creates aBeanMetafor the specified class using the suppliedBeanConfigContext.This is the bean-modeling entry point — it constructs a
BeanMetapurely from aClassand aBeanConfigContext, without touching any marshalling-side type infrastructure. The returnedBeanMetacarries enough information to do raw getter/setter invocation and property iteration; marshalling-aware reads (getBeanInfo(), type-resolution on eachBeanPropertyMeta.getBeanInfo(), the per-property registry lookup) remainnull .Example:
BeanMeta<Person>
bm = BeanMeta.of (Person.class , BeanConfigContext.DEFAULT ); BeanPropertyMetaname =bm .getProperties().get("name" ); -
of
Same asof(Class, BeanConfigContext)usingBeanConfigContext.DEFAULT. -
equals
-
getBeanFilter
Returns the bean filter associated with this bean.Bean filters are used to control aspects of how beans are handled during serialization and parsing, such as property inclusion/exclusion, property ordering, and type name mapping.
On the marshalling-side construction path, the bean filter is typically built from the
@Marshalledand@BeanTypeannotations on the class. If neither annotation is present (or thisBeanMetawas built via the commons-side path), this method returnsnull .- Returns:
- The bean filter for this bean, or
null if no bean filter is associated with this bean.
-
getBeanProxyInvocationHandler
Returns the proxy invocation handler for this bean if it's an interface.- Returns:
- The invocation handler, or
null if this is not an interface or interface proxies are disabled.
-
getBeanRegistry
Returns the bean registry for this bean.The bean registry is used to resolve dictionary names to class types. It's created when a bean class has a
@Marshalled(dictionary)annotation that specifies a list of possible subclasses.Returns the bean-modeling-side SPI type (
BeanRegistryLookup).- Returns:
- The bean registry for this bean, or
null if no bean registry is associated with it.
-
getPropertyBeanRegistry
Returns the per-property registry lookup associated with the givenBeanPropertyMeta.BeanPropertyMetano longer carries a concrete registry field — the per-property registry now lives in a side-map on thisBeanMetakeyed by the property meta itself. The serializer and parser sides still need to look up the property-level registry for polymorphic dispatch; they now route through this accessor (directly or via the deprecated-styleBeanPropertyMeta.getBeanRegistry()delegate).The returned registry chains the property's
@MarshalledProp(dictionary)entries on top of the bean-level registry, which in turn chains on top of the global bean dictionary.- Parameters:
p- The bean property meta to look up. Can be a normal property, the synthetic"_type" property, or any other property meta produced by this bean.- Returns:
- The bean registry for the specified property, or
null if none was registered (e.g. the property belongs to a different bean meta).
-
getBeanInfo
Returns theBeanInfoof this bean.Returns
null when thisBeanMetawas constructed via the commons-side path (of(Class, BeanConfigContext)) — in that casegetClassInfo()carries the pure-reflection view of the bean class.Returns the bean-modeling-side SPI type (
BeanInfo).- Returns:
- The
BeanInfoof this bean, ornull for bean-modeling-only construction.
-
getClassInfo
Returns theClassInfoof this bean.Pure-reflection view of the bean class. Always non-null, regardless of construction path. Use this in preference to
getBeanInfo()for any bean-modeling read that does not depend on marshalling-aware type metadata; the commons-side construction path leavesgetBeanInfo()null .- Returns:
- The class info for this bean. Never
null .
-
getConfig
Returns the bean-modeling configuration snapshot used to build thisBeanMeta.For marshalling-side construction, this is sourced from the owning marshalling context. For commons-side construction (via
of(Class, BeanConfigContext)), this is theBeanConfigContextpassed to the factory. Always non-null.- Returns:
- The bean-modeling configuration. Never
null .
-
getDictionaryName
Returns the dictionary name for this bean as defined through the@Marshalled(typeName)annotation.- Returns:
- The dictionary name for this bean, or
null if it has no dictionary name defined.
-
getProperties
Returns a map of all properties on this bean.The map is keyed by property name and contains
BeanPropertyMetaobjects that provide metadata about each property, including its type, getter/setter methods, field information, and serialization/parsing behavior.This map contains only the normal (non-hidden) properties of the bean. Hidden properties can be accessed via
getHiddenProperties().- Returns:
- A map of property names to their metadata. The map is unmodifiable.
- See Also:
-
getPropertyMeta
Returns metadata about the specified property.- Parameters:
name- The name of the property on this bean. Can benull to return the dynamic ("*" ) property.- Returns:
- The metadata about the property, or
null if no such property exists on this bean.
-
getTypeProperty
Returns a mock bean property that resolves to the name"_type" and whose value always resolves to the dictionary name of the bean.- Returns:
- The type name property.
-
getTypePropertyName
Returns the type property name for this bean.This is the name of the bean property used to store the dictionary name of a bean type so that the parser knows the data type to reconstruct.
If
null ,"_type" should be assumed.The value is determined from:
- The marshalling-side
@Marshalled(typePropertyName)on the class, if present (resolved viaBeanMetaInitializer.resolveTypePropertyName(BeanConfigContext, ClassInfo)). - Otherwise, the default from
BeanConfigContext.getBeanTypePropertyName().
- Returns:
- The type property name associated with this bean, or
null if the default"_type" should be used. - See Also:
- The marshalling-side
-
hashCode
-
onReadProperty
Property read interceptor.Called immediately after calling the getter to allow the value to be overridden.
- Parameters:
bean- The bean from which the property was read.name- The property name.value- The value just extracted from calling the bean getter.- Returns:
- The value to serialize. Default is just to return the existing value.
-
onWriteProperty
Property write interceptor.Called immediately before calling theh setter to allow value to be overwridden.
- Parameters:
bean- The bean from which the property was read.name- The property name.value- The value just parsed.- Returns:
- The value to serialize. Default is just to return the existing value.
-
properties
-
toString
-
getBeanTypeResolver
Returns the bean-modelingBeanTypeResolverthat this metadata was constructed against.On the marshalling-side construction path, the underlying object is the marshalling context (which implements
BeanTypeResolver); marshalling-side callers that need the concrete narrowing must cast (e.g.(MarshallingContext) bm.getBeanTypeResolver()).Returns
null when thisBeanMetawas constructed via the commons-side path (of(Class, BeanConfigContext)). UsegetConfig()for the always-non-null bean-modeling configuration facade.- Returns:
- The bean-modeling type resolver, or
null for bean-modeling-only construction.
-
getConstructor
Returns the constructor for this bean, if one was found.The constructor is determined by
findBeanConstructor()and may be:- A constructor annotated with
@BeanCtor - An implementation class constructor (if provided)
- A no-argument constructor
null if no suitable constructor was found
- Returns:
- The constructor for this bean, or
null if no constructor is available. - See Also:
- A constructor annotated with
-
getConstructorArgs
Returns the list of property names that correspond to the constructor parameters.The property names are in the same order as the constructor parameters. These names are used to map parsed property values to constructor arguments when creating bean instances.
The property names are determined from:
- The
properties()value in the@BeanCtorannotation (if present) - Otherwise, the parameter names from the constructor (if available in bytecode)
If the bean has no constructor or uses a no-argument constructor, this list will be empty.
- Returns:
- A list of property names corresponding to constructor parameters, in parameter order.
- See Also:
- The
-
getDynaProperty
Returns the "extras" property for dynamic bean properties.- Returns:
- The dynamic property, or
null if not present.
-
getGetterProps
Returns the map of getter methods to property names.- Returns:
- The getter properties map.
-
getHiddenProperties
Returns the map of hidden properties on this bean.Hidden properties are properties that exist on the bean but are not included in the normal property list. These properties are typically excluded from serialization but may still be accessible programmatically.
Hidden properties can be defined through:
Bean filter exclude properties- Properties that fail validation during bean metadata creation
- Returns:
- A map of hidden property names to their metadata. The map is unmodifiable.
- See Also:
-
getSetterProps
Returns the map of setter methods to property names.- Returns:
- The setter properties map.
-
hasConstructor
Returns whether this bean has a constructor available for instantiation.A bean has a constructor if
findBeanConstructor()was able to find a suitable constructor, which may be:- A constructor annotated with
@BeanCtor - An implementation class constructor (if provided)
- A no-argument constructor
If this method returns
false , the bean cannot be instantiated usingnewBean(Object), and may need to be created through other means (e.g., interface proxies).- Returns:
true if a constructor is available,false otherwise.- See Also:
- A constructor annotated with
-
hasRequiredProperties
Returns whether any property of this bean is marked@BeanProp(required=true).- Returns:
true if at least one property is required.
-
getRequiredPropertyNames
Returns the names of the properties marked@BeanProp(required=true).Hidden and excluded properties are not included, since input can never set them.
Example:
public record Person(@BeanProp (required=true ) Stringname , Integerage ) {}// [name] List<String>required = BeanMeta.of (Person.class , BeanConfigContext.DEFAULT ).getRequiredPropertyNames();- Returns:
- An unmodifiable list of property names, in property order. Never
null .
-
isUnsortedProperties
Returns whether this bean has opted out of alphabetical property sorting.- Returns:
true if properties use declaration order (see the class Javadoc).
-
newBean
Creates a new instance of this bean.- Parameters:
outer- The outer object if bean class is a non-static inner member class. Can benull if not applicable.- Returns:
- A new instance of this bean if possible, or
null if not. - Throws:
ExecutableException- Exception occurred on invoked constructor/method/field.
-