Class Inspector

java.lang.Object
groovy.inspect.Inspector

public class Inspector extends Object
The Inspector provides a unified access to an object's information that can be determined by introspection.
  • Field Details

    • objectUnderInspection

      protected Object objectUnderInspection
      Object currently being inspected.
    • CLASS_PACKAGE_IDX

      public static final int CLASS_PACKAGE_IDX
      Index for package information.
      See Also:
    • CLASS_CLASS_IDX

      public static final int CLASS_CLASS_IDX
      Index for class information.
      See Also:
    • CLASS_INTERFACE_IDX

      public static final int CLASS_INTERFACE_IDX
      Index for implemented interface information.
      See Also:
    • CLASS_SUPERCLASS_IDX

      public static final int CLASS_SUPERCLASS_IDX
      Index for superclass information.
      See Also:
    • CLASS_OTHER_IDX

      public static final int CLASS_OTHER_IDX
      Index for other class information.
      See Also:
    • MEMBER_ORIGIN_IDX

      public static final int MEMBER_ORIGIN_IDX
      Index for member origin information.
      See Also:
    • MEMBER_MODIFIER_IDX

      public static final int MEMBER_MODIFIER_IDX
      Index for member modifier information.
      See Also:
    • MEMBER_DECLARER_IDX

      public static final int MEMBER_DECLARER_IDX
      Index for member declarer information.
      See Also:
    • MEMBER_TYPE_IDX

      public static final int MEMBER_TYPE_IDX
      Index for member type information.
      See Also:
    • MEMBER_NAME_IDX

      public static final int MEMBER_NAME_IDX
      Index for member name information.
      See Also:
    • MEMBER_PARAMS_IDX

      public static final int MEMBER_PARAMS_IDX
      Index for member parameter information.
      See Also:
    • MEMBER_VALUE_IDX

      public static final int MEMBER_VALUE_IDX
      Index for member value information.
      See Also:
    • MEMBER_EXCEPTIONS_IDX

      public static final int MEMBER_EXCEPTIONS_IDX
      Index for member exception information.
      See Also:
    • NOT_APPLICABLE

      public static final String NOT_APPLICABLE
      Placeholder for unavailable information.
      See Also:
    • GROOVY

      public static final String GROOVY
      Origin label for Groovy members.
      See Also:
    • JAVA

      public static final String JAVA
      Origin label for Java members.
      See Also:
  • Constructor Details

    • Inspector

      public Inspector(Object objectUnderInspection)
      Parameters:
      objectUnderInspection - must not be null
  • Method Details

    • getClassProps

      public String[] getClassProps()
      Get the Class Properties of the object under inspection.
      Returns:
      String array to be indexed by the CLASS_xxx_IDX constants
    • isGroovy

      public boolean isGroovy()
      Indicates whether the inspected type implements GroovyObject.
      Returns:
      true if the inspected type is Groovy-aware
    • getObject

      public Object getObject()
      Gets the object being inspected.
      Returns:
      the object
    • getMethods

      public Object[] getMethods()
      Get info about usual Java instance and class Methods as well as Constructors.
      Returns:
      Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
    • getMethodsWithInfo

      public Tuple2[] getMethodsWithInfo()
      Get info about usual Java instance and class Methods as well as Constructors.
    • getMetaMethods

      public Object[] getMetaMethods()
      Get info about instance and class Methods that are dynamically added through Groovy.
      Returns:
      Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
    • getMetaMethodsWithInfo

      public Tuple2[] getMetaMethodsWithInfo()
      Get info about instance and class Methods that are dynamically added through Groovy.
    • getPublicFields

      public Object[] getPublicFields()
      Get info about usual Java public fields incl. constants.
      Returns:
      Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
    • getPropertyInfo

      public Object[] getPropertyInfo()
      Get info about Properties (Java and Groovy alike).
      Returns:
      Array of StringArrays that can be indexed with the MEMBER_xxx_IDX constants
    • fieldInfo

      protected String[] fieldInfo(Field field)
      Builds descriptive information for a Java field.
      Parameters:
      field - the field to describe
      Returns:
      the field information array
    • fieldInfo

      protected String[] fieldInfo(PropertyValue pv)
      Builds descriptive information for a Groovy property value.
      Parameters:
      pv - the property value to describe
      Returns:
      the property information array
    • getPropertiesWithInfo

      public Object[] getPropertiesWithInfo()
      Gets properties together with their values and formatted metadata.
      Returns:
      the property/value tuples
    • fieldWithInfo

      protected Tuple2<Object,String[]> fieldWithInfo(PropertyValue pv)
      Builds the value and metadata tuple for a Groovy property.
      Parameters:
      pv - the property value to describe
      Returns:
      the property tuple
    • getClassUnderInspection

      protected Class getClassUnderInspection()
      Gets the class of the inspected object.
      Returns:
      the inspected class
    • shortName

      public static String shortName(Class clazz)
      Returns the class name without its package prefix.
      Parameters:
      clazz - the class to shorten
      Returns:
      the shortened class name, or NOT_APPLICABLE
    • methodInfo

      protected String[] methodInfo(Method method)
      Builds descriptive information for a Java method.
      Parameters:
      method - the method to describe
      Returns:
      the method information array
    • methodInfo

      protected String[] methodInfo(Constructor ctor)
      Builds descriptive information for a constructor.
      Parameters:
      ctor - the constructor to describe
      Returns:
      the constructor information array
    • methodInfo

      protected String[] methodInfo(MetaMethod method)
      Builds descriptive information for a Groovy meta-method.
      Parameters:
      method - the meta-method to describe
      Returns:
      the method information array
    • withoutNulls

      protected String[] withoutNulls(String[] toNormalize)
      Replaces null entries with NOT_APPLICABLE.
      Parameters:
      toNormalize - the array to normalize
      Returns:
      the normalized array
    • print

      public static void print(Object[] memberInfo)
      Prints member information to System.out.
      Parameters:
      memberInfo - the member information to print
    • sort

      public static Collection sort(List<Object> memberInfo)
      Sorts member information using the default comparator.
      Parameters:
      memberInfo - the member information to sort
      Returns:
      the sorted collection
    • sort

      public static Collection sort(List<Object> memberInfo, Comparator<Object> comparator)
      Sorts member information using the supplied comparator.
      Parameters:
      memberInfo - the member information to sort
      comparator - the comparator to apply
      Returns:
      the sorted collection