Class BeanUtil

  • All Implemented Interfaces:
    java.io.Serializable

    public final class BeanUtil
    extends java.lang.Object
    implements java.io.Serializable
    Utility class for Java Beans information access.
    Since:
    7.4
    Author:
    Vaadin Ltd
    See Also:
    Serialized Form
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean checkBeanValidationAvailable()
      Returns whether an implementation of JSR-303 version 1.0 or 1.1 is present on the classpath.
      static boolean checkSerialization​(java.lang.Object obj)
      Checks if the object is serializable or not.
      static java.util.List<java.beans.PropertyDescriptor> getBeanPropertyDescriptors​(java.lang.Class<?> beanType)
      Returns the property descriptors of a class or an interface.
      static java.beans.PropertyDescriptor getPropertyDescriptor​(java.lang.Class<?> beanType, java.lang.String propertyName)
      Returns the property descriptor for the property of the given name and declaring class.
      static java.lang.Class<?> getPropertyType​(java.lang.Class<?> beanType, java.lang.String propertyName)
      Returns the type of the property with the given name and declaring class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getBeanPropertyDescriptors

        public static java.util.List<java.beans.PropertyDescriptor> getBeanPropertyDescriptors​(java.lang.Class<?> beanType)
                                                                                        throws java.beans.IntrospectionException
        Returns the property descriptors of a class or an interface. For an interface, superinterfaces are also iterated as Introspector does not take them into account (Oracle Java bug 4275879), but in that case, both the setter and the getter for a property must be in the same interface and should not be overridden in subinterfaces for the discovery to work correctly.

        NOTE : This utility method relies on introspection (and returns PropertyDescriptor) which is a part of java.beans package. The latter package could require bigger JDK in the future (with Java 9+). So it may be changed in the future.

        For interfaces, the iteration is depth first and the properties of superinterfaces are returned before those of their subinterfaces.

        Parameters:
        beanType - the type whose properties to query
        Returns:
        a list of property descriptors of the given type
        Throws:
        java.beans.IntrospectionException - if the introspection fails
      • getPropertyType

        public static java.lang.Class<?> getPropertyType​(java.lang.Class<?> beanType,
                                                         java.lang.String propertyName)
                                                  throws java.beans.IntrospectionException
        Returns the type of the property with the given name and declaring class. The property name may refer to a nested property, e.g. "property.subProperty" or "property.subProperty1.subProperty2". The property must have a public read method (or a chain of read methods in case of a nested property).
        Parameters:
        beanType - the type declaring the property
        propertyName - the name of the property
        Returns:
        the property type
        Throws:
        java.beans.IntrospectionException - if the introspection fails
      • getPropertyDescriptor

        public static java.beans.PropertyDescriptor getPropertyDescriptor​(java.lang.Class<?> beanType,
                                                                          java.lang.String propertyName)
                                                                   throws java.beans.IntrospectionException
        Returns the property descriptor for the property of the given name and declaring class. The property name may refer to a nested property, e.g. "property.subProperty" or "property.subProperty1.subProperty2". The property must have a public read method (or a chain of read methods in case of a nested property).
        Parameters:
        beanType - the type declaring the property
        propertyName - the name of the property
        Returns:
        the corresponding descriptor
        Throws:
        java.beans.IntrospectionException - if the introspection fails
      • checkBeanValidationAvailable

        public static boolean checkBeanValidationAvailable()
        Returns whether an implementation of JSR-303 version 1.0 or 1.1 is present on the classpath. If this method returns false, trying to create a BeanValidator instance will throw an IllegalStateException. If an implementation is not found, logs a level FINE message the first time it is run.
        Returns:
        true if bean validation is available, false otherwise.
      • checkSerialization

        public static boolean checkSerialization​(java.lang.Object obj)
        Checks if the object is serializable or not. To be used in assertion checks only, since the check might be a bit heavyweight.
        Parameters:
        obj - to be checked
        Returns:
        true
        Throws:
        java.lang.AssertionError - if the object is not serializable