Class LightMetaBean<T extends Bean>

  • Type Parameters:
    T - the type of the bean
    All Implemented Interfaces:
    MetaBean, TypedMetaBean<T>

    public final class LightMetaBean<T extends Bean>
    extends java.lang.Object
    implements TypedMetaBean<T>
    A meta-bean implementation that operates using method handles.

    The properties are found using the PropertyDefinition annotation. There must be a constructor matching the property definitions (arguments of same order and types).

    This uses method handles to avoid problems with reflection setAccessible() in Java SE 9. The old reflection approach is still present, but deprecated.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      java.lang.Class<T> beanType()
      Get the type of the bean, represented as a Class.
      BeanBuilder<T> builder()
      Creates a bean builder that can be used to create an instance of this bean.
      boolean equals​(java.lang.Object obj)  
      int hashCode()  
      boolean isBuildable()
      Checks whether this bean is buildable or not.
      <R> MetaProperty<R> metaProperty​(java.lang.String propertyName)
      Gets a meta-property by name.
      java.util.Map<java.lang.String,​MetaProperty<?>> metaPropertyMap()
      Gets the map of meta-properties, keyed by property name.
      static <B extends Bean>
      LightMetaBean<B>
      of​(java.lang.Class<B> beanClass)
      Deprecated.
      Use method handles version of this method
      static <B extends Bean>
      LightMetaBean<B>
      of​(java.lang.Class<B> beanType, java.lang.invoke.MethodHandles.Lookup lookup)
      Obtains an instance of the meta-bean using standard default values.
      static <B extends Bean>
      LightMetaBean<B>
      of​(java.lang.Class<B> beanType, java.lang.invoke.MethodHandles.Lookup lookup, java.lang.Object... defaultValues)
      Deprecated.
      Use version with field names, because no way to determine order of fields by reflection
      static <B extends Bean>
      LightMetaBean<B>
      of​(java.lang.Class<B> beanType, java.lang.invoke.MethodHandles.Lookup lookup, java.lang.String[] fieldNames, java.lang.Object... defaultValues)
      Obtains an instance of the meta-bean specifying default values.
      java.lang.String toString()
      Returns a string that summarises the meta-bean.
      LightMetaBean<T> withAlias​(java.lang.String alias, java.lang.String realName)
      Adds an alias to the meta-bean.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • of

        @Deprecated
        public static <B extends BeanLightMetaBean<B> of​(java.lang.Class<B> beanClass)
        Deprecated.
        Use method handles version of this method
        Obtains an instance of the meta-bean.

        The properties will be determined using reflection to find the PropertyDefinition annotation.

        Type Parameters:
        B - the type of the bean
        Parameters:
        beanClass - the bean class, not null
        Returns:
        the meta-bean, not null
      • of

        public static <B extends BeanLightMetaBean<B> of​(java.lang.Class<B> beanType,
                                                           java.lang.invoke.MethodHandles.Lookup lookup)
        Obtains an instance of the meta-bean using standard default values.

        The properties will be determined using reflection to find the fields. Each field must have a PropertyDefinition annotation. The order of the properties is undefined as Java fields are not necessarily returned in source code order.

        The default values for primitives are determined automatically. If the bean has non-primitive values like lists and maps that need defaulting then of(Class, java.lang.invoke.MethodHandles.Lookup, String[], Object...) must be used.

        Type Parameters:
        B - the type of the bean
        Parameters:
        beanType - the bean type, not null
        lookup - the method handle lookup, not null
        Returns:
        the meta-bean, not null
      • of

        @Deprecated
        public static <B extends BeanLightMetaBean<B> of​(java.lang.Class<B> beanType,
                                                           java.lang.invoke.MethodHandles.Lookup lookup,
                                                           java.lang.Object... defaultValues)
        Deprecated.
        Use version with field names, because no way to determine order of fields by reflection
        Obtains an instance of the meta-bean specifying default values.

        The properties will be determined using reflection to find the PropertyDefinition annotation.

        The default values must be provided if they cannot be determined automatically. Default values for primitives are determined automatically, but empty lists and maps are not.

        Type Parameters:
        B - the type of the bean
        Parameters:
        beanType - the bean type, not null
        lookup - the method handle lookup, not null
        defaultValues - the default values, one for each property, not null
        Returns:
        the meta-bean, not null
      • of

        public static <B extends BeanLightMetaBean<B> of​(java.lang.Class<B> beanType,
                                                           java.lang.invoke.MethodHandles.Lookup lookup,
                                                           java.lang.String[] fieldNames,
                                                           java.lang.Object... defaultValues)
        Obtains an instance of the meta-bean specifying default values.

        The properties will be determined using reflection to find the PropertyDefinition annotation.

        The field names must be specified as reflection does not return fields in source code order. The default values must be provided if they cannot be determined automatically. Default values for primitives are determined automatically, but empty lists and maps are not.

        Type Parameters:
        B - the type of the bean
        Parameters:
        beanType - the bean type, not null
        lookup - the method handle lookup, not null
        fieldNames - the field names, one for each property, not null
        defaultValues - the default values, one for each property, not null
        Returns:
        the meta-bean, not null
      • withAlias

        public LightMetaBean<T> withAlias​(java.lang.String alias,
                                          java.lang.String realName)
        Adds an alias to the meta-bean.

        When using metaProperty(String), the alias will return the meta-property of the real name.

        Parameters:
        alias - the alias
        realName - the real name
        Returns:
        the new meta-bean instance
        Throws:
        java.lang.IllegalArgumentException - if the realName is invalid
      • isBuildable

        public boolean isBuildable()
        Description copied from interface: MetaBean
        Checks whether this bean is buildable or not.

        A buildable bean can be constructed using MetaBean.builder(). If this method returns true then builder() must return a valid builder. If this method returns false then builder() must throw UnsupportedOperationException.

        Specified by:
        isBuildable in interface MetaBean
        Returns:
        true if this bean is buildable
      • builder

        public BeanBuilder<T> builder()
        Description copied from interface: MetaBean
        Creates a bean builder that can be used to create an instance of this bean.

        The builder is used in two main ways. The first is to allow immutable beans to be constructed. The second is to enable automated tools like serialization/deserialization.

        The builder can be thought of as a Map of MetaProperty to value. Note that the implementation is not necessarily an actual map.

        Specified by:
        builder in interface MetaBean
        Specified by:
        builder in interface TypedMetaBean<T extends Bean>
        Returns:
        the bean builder, not null
      • beanType

        public java.lang.Class<T> beanType()
        Description copied from interface: MetaBean
        Get the type of the bean, represented as a Class.

        A MetaBean can be thought of as the equivalent of Class but for beans. This method allows the actual Class instance of the bean to be obtained.

        Specified by:
        beanType in interface MetaBean
        Specified by:
        beanType in interface TypedMetaBean<T extends Bean>
        Returns:
        the type of the bean, not null
      • metaProperty

        public <R> MetaProperty<R> metaProperty​(java.lang.String propertyName)
        Description copied from interface: MetaBean
        Gets a meta-property by name.

        Each meta-bean manages a single bean with a known set of properties. This method returns the property with the specified name.

        The base interface throws an exception if the name is not recognised. By contrast, the DynamicMetaBean subinterface creates the property on demand.

        Specified by:
        metaProperty in interface MetaBean
        Type Parameters:
        R - the property type, optional, enabling auto-casting
        Parameters:
        propertyName - the property name to retrieve, not null
        Returns:
        the meta property, not null
      • metaPropertyMap

        public java.util.Map<java.lang.String,​MetaProperty<?>> metaPropertyMap()
        Description copied from interface: MetaBean
        Gets the map of meta-properties, keyed by property name.

        Where possible, use MetaBean.metaPropertyIterable() instead as it typically has better performance.

        Specified by:
        metaPropertyMap in interface MetaBean
        Returns:
        the unmodifiable map of meta property objects, not null
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public java.lang.String toString()
        Returns a string that summarises the meta-bean.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a summary string, not null