Class BeanMeta<T>

java.lang.Object
org.apache.juneau.BeanMeta<T>
Type Parameters:
T - The class type that this metadata applies to.
Direct Known Subclasses:
BeanMetaFiltered

public class BeanMeta<T> extends Object
Encapsulates all access to the properties of a bean class (like a souped-up BeanInfo).
Description
Uses introspection to find all the properties associated with this class. If the @Bean annotation is present on the class, then 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 are as follows:
  • If @Bean annotation is specified on class, then the order is the same as the list of properties in the annotation.
  • If @Bean annotation is not specified on the class, then the order is based on the following.
    • Public fields (same order as Class.getFields()).
    • Properties returned by BeanInfo.getPropertyDescriptors().
    • Non-standard getters/setters with @Beanp annotation defined on them.
  • Constructor Details

    • BeanMeta

      protected BeanMeta(ClassMeta<T> cm, BeanFilter bf, String[] pNames, ClassInfo implClass)
      Constructor.

      Creates a new BeanMeta instance 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.
      bf - Optional bean filter to apply. Can be null.
      pNames - Explicit list of property names and order. If null, properties are determined automatically.
      implClass - Optional implementation class constructor to use if one cannot be found. Can be null.
  • Method Details

    • create

      public static <T> org.apache.juneau.BeanMeta.BeanMetaValue<T> create(ClassMeta<T> cm, ClassInfo implClass)
      Creates a BeanMeta instance for the specified class metadata.

      This is a factory method that attempts to create bean metadata for a class. If the class is determined to be a bean, the returned BeanMeta.BeanMetaValue will contain the BeanMeta instance and a null reason. If the class is not a bean, the returned value will contain null for the bean metadata and a non-null string explaining why it's not a bean.

      Parameters:
      • cm - The class metadata for the class to create bean metadata for.
      • bf - Optional bean filter to apply. Can be null.
      • pNames - Explicit list of property names and order. If null, properties are determined automatically.
      • implClassConstructor - Optional constructor to use if one cannot be found. Can be null.
      Return Value:

      Returns a BeanMeta.BeanMetaValue containing:

      • beanMeta - The bean metadata if the class is a bean, or null if it's not.
      • notABeanReason - A string explaining why the class is not a bean, or null if it is a bean.
      Exception Handling:

      If a RuntimeException is thrown during bean metadata creation, it is caught and the exception message is returned as the notABeanReason with null for the bean metadata.

      Example:

      // Create bean metadata for a class ClassMeta<Person> cm = beanContext.getClassMeta(Person.class); BeanMetaValue<Person> result = BeanMeta.create(cm, null, null, null); // Check if it's a bean if (result.beanMeta() != null) { BeanMeta<Person> bm = result.beanMeta(); // Use the bean metadata... } else { String reason = result.notABeanReason(); // Handle the case where it's not a bean... }

      Type Parameters:
      T - The class type.
      Parameters:
      cm - The class metadata for the class to create bean metadata for.
      implClass - The implementation class info.
      Returns:
      A BeanMeta.BeanMetaValue containing the bean metadata (if successful) or a reason why it's not a bean.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • 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.

      The bean filter is typically created from the @Bean annotation on the class. If no @Bean annotation is present, this method returns null.

      Returns:
      The bean filter for this bean, or null if no bean filter is associated with this bean.
      See Also:
    • 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 @Bean(dictionary) annotation that specifies a list of possible subclasses.

      Returns:
      The bean registry for this bean, or null if no bean registry is associated with it.
    • getClassMeta

      Returns the ClassMeta of this bean.
      Returns:
      The ClassMeta of this bean.
    • getDictionaryName

      Returns the dictionary name for this bean as defined through the @Bean(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 BeanPropertyMeta objects 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.
      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:

      Returns:
      The type property name associated with this bean, or null if the default "_type" should be used.
      See Also:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • onReadProperty

      public Object onReadProperty(Object bean, String name, Object value)
      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

      public Object onWriteProperty(Object bean, String name, Object value)
      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

      public String toString()
      Overrides:
      toString in class Object
    • getBeanContext

      Returns the bean context that created this metadata object.
      Returns:
      The bean context.
    • getConstructor

      Returns the constructor for this bean, if one was found.

      The constructor is determined by findBeanConstructor() and may be:

      • A constructor annotated with @Beanc
      • 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:
    • 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 @Beanc annotation (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:
    • 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:

      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

      protected boolean 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 @Beanc
      • An implementation class constructor (if provided)
      • A no-argument constructor

      If this method returns false, the bean cannot be instantiated using newBean(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:
    • isSortProperties

      protected boolean isSortProperties()
      Returns whether properties should be sorted for this bean.
      Returns:
      true if properties should be sorted.
    • newBean

      protected T newBean(Object outer) throws ExecutableException
      Creates a new instance of this bean.
      Parameters:
      outer - The outer object if bean class is a non-static inner member class.
      Returns:
      A new instance of this bean if possible, or null if not.
      Throws:
      ExecutableException - Exception occurred on invoked constructor/method/field.