Class ClassReference


  • public class ClassReference
    extends java.lang.Object
    Allows access to classes by reflection.
    Author:
    Alexandre Iline (alexandre.iline@sun.com)
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassReference​(java.lang.Object o)
      Constructor.
      ClassReference​(java.lang.String className)
      Contructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Class[] getClasses()
      Returns all superclasses.
      java.lang.Object getField​(java.lang.String field_name)
      Returns the field value.
      java.lang.Object invokeMethod​(java.lang.String method_name, java.lang.Object[] params, java.lang.Class[] params_classes)
      Locates method by name and parameter types and executes it.
      java.lang.Object newInstance​(java.lang.Object[] params, java.lang.Class[] params_classes)
      Locates constructor by parameter types and creates an instance.
      void setField​(java.lang.String field_name, java.lang.Object newValue)
      Change a field's value.
      void startApplication()
      Executes class's main(java.lang.String[]) method with a zero-length java.lang.String array as a parameter.
      void startApplication​(java.lang.String[] params)
      Executes class's main(java.lang.String[]) method.
      • Methods inherited from class java.lang.Object

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

      • ClassReference

        public ClassReference​(java.lang.Object o)
        Constructor.
        Parameters:
        o - Object to work with.
      • ClassReference

        public ClassReference​(java.lang.String className)
                       throws java.lang.ClassNotFoundException
        Contructor. The object created by this constructor can be used to access static methods and fields only.
        Parameters:
        className - name of class
        Throws:
        java.lang.ClassNotFoundException
    • Method Detail

      • startApplication

        public void startApplication()
                              throws java.lang.reflect.InvocationTargetException,
                                     java.lang.NoSuchMethodException
        Executes class's main(java.lang.String[]) method with a zero-length java.lang.String array as a parameter.
        Throws:
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
      • startApplication

        public void startApplication​(java.lang.String[] params)
                              throws java.lang.reflect.InvocationTargetException,
                                     java.lang.NoSuchMethodException
        Executes class's main(java.lang.String[]) method.
        Parameters:
        params - The java.lang.String array to pass to main(java.lang.String[]).
        Throws:
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
      • invokeMethod

        public java.lang.Object invokeMethod​(java.lang.String method_name,
                                             java.lang.Object[] params,
                                             java.lang.Class[] params_classes)
                                      throws java.lang.reflect.InvocationTargetException,
                                             java.lang.NoSuchMethodException,
                                             java.lang.IllegalAccessException
        Locates method by name and parameter types and executes it.
        Parameters:
        method_name - Name of method.
        params - Method parameters.
        params_classes - Method parameters types.
        Returns:
        the return value from an invocation of the Method.
        If method_name method is void, null is returned.
        If method_name method returns a primitive type, then return wrapper class instance.
        Throws:
        java.lang.reflect.InvocationTargetException - when the invoked method throws an exception.
        java.lang.NoSuchMethodException - when the method cannot be found.
        java.lang.IllegalAccessException - when access to the class or method is lacking.
        java.lang.SecurityException - if access to the package or method is denied.
        java.lang.IllegalAccessException
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
      • newInstance

        public java.lang.Object newInstance​(java.lang.Object[] params,
                                            java.lang.Class[] params_classes)
                                     throws java.lang.reflect.InvocationTargetException,
                                            java.lang.NoSuchMethodException,
                                            java.lang.IllegalAccessException,
                                            java.lang.InstantiationException
        Locates constructor by parameter types and creates an instance.
        Parameters:
        params - An array of Method parameters.
        params_classes - An array of Method parameter types.
        Returns:
        a new class instance.
        Throws:
        java.lang.reflect.InvocationTargetException - when the invoked constructor throws an exception.
        java.lang.NoSuchMethodException - when the constructor cannot be found.
        java.lang.IllegalAccessException - when access to the class or constructor is lacking.
        java.lang.InstantiationException - when the constructor is for an abstract class.
        java.lang.SecurityException - if access to the package or constructor is denied.
        java.lang.IllegalAccessException
        java.lang.NoSuchMethodException
        java.lang.InstantiationException
        java.lang.reflect.InvocationTargetException
      • getField

        public java.lang.Object getField​(java.lang.String field_name)
                                  throws java.lang.NoSuchFieldException,
                                         java.lang.IllegalAccessException
        Returns the field value.
        Parameters:
        field_name - The name of the field.
        Returns:
        the field value
        Throws:
        java.lang.NoSuchFieldException - when the field cannot be found.
        java.lang.IllegalAccessException - when access to the class or constructor is lacking.
        java.lang.SecurityException - if access to the package or field is denied.
        java.lang.IllegalAccessException
        java.lang.NoSuchFieldException
        See Also:
        setField(java.lang.String, java.lang.Object)
      • setField

        public void setField​(java.lang.String field_name,
                             java.lang.Object newValue)
                      throws java.lang.NoSuchFieldException,
                             java.lang.IllegalAccessException
        Change a field's value.
        Parameters:
        field_name - The name of the field.
        newValue - The fields new value.
        Throws:
        java.lang.NoSuchFieldException - when the field cannot be found.
        java.lang.IllegalAccessException - when access to the class or constructor is lacking.
        java.lang.SecurityException - if access to the package or field is denied.
        java.lang.IllegalAccessException
        java.lang.NoSuchFieldException
        See Also:
        getField(java.lang.String)
      • getClasses

        public java.lang.Class[] getClasses()
        Returns all superclasses.
        Returns:
        an array of superclasses, starting with the reference class and ending with java.lang.Object.