Class Introspector


  • public class Introspector
    extends java.lang.Object
    A class to allow access to the methods "fooType getFoo()" and "void setFoo(fooType)" conventionally seen in objects. Useful when Foo arrives as a run-time String, such as is the case in serialization to/from XML representations.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.String field
      The field whose get/set methods we wish to invoke.
      private java.lang.Class<?> theClass
      The class whose field we are to operate on.
    • Constructor Summary

      Constructors 
      Constructor Description
      Introspector​(java.lang.Class<?> theClass, java.lang.String field)
      Build a new Introspector for the specified class and field name.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T construct​(java.lang.reflect.Constructor<T> constructor, java.lang.Object[] params)
      Construct a new instance.
      static java.lang.Class<?> getClassByName​(java.lang.String name)
      Get a class by name.
      static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> cl, java.lang.Class[] types)
      Get a constructor for a given class and arguments.
      private java.lang.reflect.Method getFromStringConverter​(java.lang.Class<?> argType)
      Get a function that converts from String to a given class.
      private java.lang.reflect.Method getGetMethod()
      Get a get-method for this Introspector.
      private java.lang.Class<?> getMethodReturnType​(java.lang.reflect.Method method)
      Get the return type from a Method.
      private java.lang.reflect.Method getSetMethod​(java.lang.Class<?> argType)
      Get a set-method for this Introspector.
      java.lang.String getter​(java.lang.Object obj)
      Invoke the get-method for this Introspector.
      private java.lang.reflect.Method getToStringConverter​(java.lang.Class<?> argType)
      Get a function that converts to String from a given class.
      static <T> T instantiate​(java.lang.Class<T> messageClass, java.lang.Class[] types, java.lang.Object[] params)
      Constructs a new instance of an object of a class specified by name, with supplied parameters.
      static java.lang.Object instantiate​(java.lang.String tag, java.lang.Class[] types, java.lang.Object[] params)
      Constructs a new instance of an object of a class specified by name, with supplied parameters.
      static <T> T invokeMethod​(java.lang.Object object, java.lang.String methodName, java.lang.Class<T> returnClass)
      Invoke an object method by name.
      static void invokeVoidMethod​(java.lang.Object object, java.lang.String methodName)
      Invoke an object void method by name.
      void setter​(java.lang.Object obj, java.lang.String value)
      Invoke the set-method provided by this Introspector.
      • Methods inherited from class java.lang.Object

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

      • theClass

        private final java.lang.Class<?> theClass
        The class whose field we are to operate on.
      • field

        private final java.lang.String field
        The field whose get/set methods we wish to invoke.
    • Constructor Detail

      • Introspector

        public Introspector​(java.lang.Class<?> theClass,
                            java.lang.String field)
        Build a new Introspector for the specified class and field name.
        Parameters:
        theClass - The Class of interest.
        field - The field name within the class of interest.
    • Method Detail

      • getSetMethod

        private java.lang.reflect.Method getSetMethod​(java.lang.Class<?> argType)
                                               throws Introspector.IntrospectorException
        Get a set-method for this Introspector.
        Parameters:
        argType - A Class that is the argument to the set-method
        Returns:
        A Method representing setField().
        Throws:
        Introspector.IntrospectorException - if the set-method is not available.
      • getMethodReturnType

        private java.lang.Class<?> getMethodReturnType​(java.lang.reflect.Method method)
                                                throws Introspector.IntrospectorException
        Get the return type from a Method.
        Parameters:
        method - The Method to examine.
        Returns:
        The method return type, or null on error.
        Throws:
        Introspector.IntrospectorException - if the return type is not available.
      • getToStringConverter

        private java.lang.reflect.Method getToStringConverter​(java.lang.Class<?> argType)
                                                       throws java.lang.NoSuchMethodException
        Get a function that converts to String from a given class. We use Enum.name() for enums, and String.valueOf(argType) for the rest.
        Parameters:
        argType - A Class to find a converter for.
        Returns:
        A conversion function, or null on error.
        Throws:
        java.lang.NoSuchMethodException - if no converter is found.
      • getFromStringConverter

        private java.lang.reflect.Method getFromStringConverter​(java.lang.Class<?> argType)
        Get a function that converts from String to a given class. We use Enum.valueOf(Class, String) for enums, and argType.valueOf(String) for the rest, having first dodged the primitive types.
        Parameters:
        argType - A Class to find a converter for.
        Returns:
        A conversion function, or null on error.
      • getter

        public java.lang.String getter​(java.lang.Object obj)
                                throws Introspector.IntrospectorException
        Invoke the get-method for this Introspector.
        Parameters:
        obj - An Object (really of type theClass) whose get-method is to be invoked.
        Returns:
        A String containing the result of invoking the get-method.
        Throws:
        Introspector.IntrospectorException - encompasses many failures.
      • setter

        public void setter​(java.lang.Object obj,
                           java.lang.String value)
                    throws Introspector.IntrospectorException
        Invoke the set-method provided by this Introspector.
        Parameters:
        obj - An Object (really of type theClass) whose set-method is to be invoked.
        value - A String containing the value to be set.
        Throws:
        Introspector.IntrospectorException - encompasses many failures.
      • getClassByName

        public static java.lang.Class<?> getClassByName​(java.lang.String name)
        Get a class by name.
        Parameters:
        name - The class name to look for.
        Returns:
        The class found, or null if none available.
      • getConstructor

        public static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> cl,
                                                                          java.lang.Class[] types)
        Get a constructor for a given class and arguments.
        Type Parameters:
        T - The type to construct.
        Parameters:
        cl - The base class.
        types - The types of the constructor arguments.
        Returns:
        The constructor found, or null on error.
      • construct

        public static <T> T construct​(java.lang.reflect.Constructor<T> constructor,
                                      java.lang.Object[] params)
                               throws Introspector.IntrospectorException
        Construct a new instance.
        Type Parameters:
        T - The type to construct.
        Parameters:
        constructor - The Constructor to use.
        params - The constructor parameters.
        Returns:
        The instance created, or null on error.
        Throws:
        Introspector.IntrospectorException - if there is a FreeCol failure.
      • instantiate

        public static java.lang.Object instantiate​(java.lang.String tag,
                                                   java.lang.Class[] types,
                                                   java.lang.Object[] params)
                                            throws Introspector.IntrospectorException
        Constructs a new instance of an object of a class specified by name, with supplied parameters.
        Parameters:
        tag - The name of the class to instantiate.
        types - The argument types of the constructor to call.
        params - The parameters to call the constructor with.
        Returns:
        The new object instance.
        Throws:
        Introspector.IntrospectorException - wraps all exceptional conditions.
      • instantiate

        public static <T> T instantiate​(java.lang.Class<T> messageClass,
                                        java.lang.Class[] types,
                                        java.lang.Object[] params)
                                 throws Introspector.IntrospectorException
        Constructs a new instance of an object of a class specified by name, with supplied parameters.
        Type Parameters:
        T - The actual return type.
        Parameters:
        messageClass - The class to instantiate.
        types - The argument types of the constructor to call.
        params - The parameters to call the constructor with.
        Returns:
        The new instance.
        Throws:
        Introspector.IntrospectorException - wraps all exceptional conditions.
      • invokeMethod

        public static <T> T invokeMethod​(java.lang.Object object,
                                         java.lang.String methodName,
                                         java.lang.Class<T> returnClass)
                                  throws java.lang.IllegalAccessException,
                                         java.lang.reflect.InvocationTargetException,
                                         java.lang.NoSuchMethodException
        Invoke an object method by name.
        Type Parameters:
        T - The actual return type.
        Parameters:
        object - The base object.
        methodName - The name of the method to invoke.
        returnClass - The expected class to return.
        Returns:
        The result of invoking the method.
        Throws:
        java.lang.IllegalAccessException - if the method exists but is hidden.
        java.lang.reflect.InvocationTargetException - if the target can not be invoked.
        java.lang.NoSuchMethodException - if the invocation fails.
      • invokeVoidMethod

        public static void invokeVoidMethod​(java.lang.Object object,
                                            java.lang.String methodName)
                                     throws java.lang.IllegalAccessException,
                                            java.lang.reflect.InvocationTargetException,
                                            java.lang.NoSuchMethodException
        Invoke an object void method by name.
        Parameters:
        object - The base object.
        methodName - The name of the method to invoke.
        Throws:
        java.lang.IllegalAccessException - if the method exists but is hidden.
        java.lang.reflect.InvocationTargetException - if the target can not be invoked.
        java.lang.NoSuchMethodException - if the invocation fails.