Class Accessor<T>

  • Type Parameters:
    T - parameter representing the class to be accessed.

    public final class Accessor<T>
    extends java.lang.Object
    Provides access even to private fields, methods, constructors and inner classes, static or not, via reflection. This is necessary for white-box-tests. Note that class objects for instances i are given by i.getClass() (except for instances of primitive types) but also if no instance is available, the class object is given by
    • <classname>.class for all types even the primitive ones and also for arrays (int.class and int[].class are valid) but not for hidden inner classes, e.g. private ones,
    • Boolean.TYPE, Character.TYPE and so on for the primitive types (which is superfluous because boolean.class works as well as Boolean.TYPE, except for java.lang.Void.TYPE which I never used so far),
    • methods getInnerClass(Class,String) and getInnerClass(Class,String[]) for hidden inner classes. Note that formally this even works for anonymous classes but since names of anonymous classes may change if another anonymous inner class is inserted, this feature should be used with caution. I personally feel it is best not to use it at all.
    Avoid using Class.forName(String className).

    The method getField(Class,String) returns the value of the specified static field. Note that if the field has primitive type, the wrapper of its content is returned. Correspondingly, getField(Object,String) returns the specified instant field. Note that since the class Class is final, and has no (non-public) fields, there should be no reason, to access a Class-object. In case of a very special application, casts like getField((Object)ls,name) should resolve the ambiguity. The two methods described above, should cover the typical situations. In some special cases however, including overwritten fields and fields of inner classes, getField(Class,Object,String) may provide the only way to access a field.

    What can be said about the methods getField applies correspondingly to methods setField. Note that there is no way to change fields which are declared final. To be more precise, the pointer in a final field

    Similarly for invoking methods: Except for very special cases, invokeStatic(Class,String,Object...) for static methods and invoke(Object,String,Object...) for instance methods will be sufficient, but for some special cases including overwriting and inner classes invoke(Class,Object,String,Object...) may be the only way to invoke a method. Note that these methods apply to parameters with primitive types as well by passing the appropriate wrapper object: For example Integer.toString(int i) may be invoked by invoke(Integer.class,"toString",new Integer(i)).

    Still there is a problem: if wrapping parameters makes signatures ambiguous, the parameter types must be specified explicitly using method invoke(Class,Object,String,Class[],Object[]). For this case, which seems to be quite rare, there are no convenience methods.

    Note that the return type is always Object. Of course, the return value may be casted to the return type of the specified method, provided the return type is an object and neither void nor a primitive type. In the latter case, the return value is wrapped in the corresponding wrapper class unlike the parameters.

    For the last case, creating objects using constructors, in most cases create(Class,Object[]) is sufficient; to avoid ambiguities, one has to specify the types of the parameters and use create(Class,Class[],Object[]) instead.

    Version:
    1.0
    Author:
    Ernst Reissner
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String INNER_SEPARATOR
      The separator between a class and its enclosing class for inner classes.
      private static java.lang.String STR_DNE  
      private static java.lang.String STR_IN_CLS  
      private static java.lang.String STR_SPEC_NULL_CLS  
      private static java.lang.String UNSPECIFIED_CLASS
      String denoting an unspecified class.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Accessor()
      Formally to create a new Accessor instance but intended in contrary to prevent an Accessor from being instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static <T> boolean constructorMatches​(java.lang.reflect.Constructor<T> cand, java.lang.Object... parameters)
      Returns whether the given constructor matches the parameter list.
      static <T> T create​(java.lang.Class<T> aClass, java.lang.Class<?>[] paramCls, java.lang.Object... parameters)
      Returns an object created by the specified constructor.
      static <T> T create​(java.lang.Class<T> aClass, java.lang.Object... parameters)
      Returns an object created by the specified constructor.
      private static <T> T create​(java.lang.reflect.Constructor<T> toBeInvoked, java.lang.Object... parameters)
      Invoke the specified constructor with the given arguments.
      private static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> aClass, java.lang.Object... parameters)
      Returns the specified constructor if aClas offers exactly one possibility with the given parameters.
      static java.lang.Object getField​(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName)
      Returns the value of the specified static field or member field resp. its wrapper.
      static java.lang.Object getField​(java.lang.Class<?> aClass, java.lang.String fieldName)
      Returns the value of the specified static field resp. its wrapper.
      static java.lang.Object getField​(java.lang.Object target, java.lang.String fieldName)
      Returns the value of the specified member field resp. its wrapper.
      private static java.lang.reflect.Field getFieldObj​(java.lang.Class<?> aClass, java.lang.String fieldName, boolean shouldBeStatic)
      Returns the specified Field object if possible.
      static java.lang.Class<?> getInnerClass​(java.lang.Class<?> enclosingCls, java.lang.String innerClsName)
      Returns the inner class of enclosingCls with the specified name innerClsName.
      static java.lang.Class<?> getInnerClass​(java.lang.Class<?> enclosingCls, java.lang.String[] pathToInner)
      Returns the inner class of enclosingCls with the specified name innerClsName.
      private static java.lang.reflect.Method getMethod​(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.reflect.Method[] cands, java.lang.Object... parameters)
      Returns the specified method if cands offers exactly one possibility.
      private static java.lang.Class<?>[] getParamCls​(java.lang.Object... parameters)
      Returns the classes of the given parameters.
      (package private) static java.lang.reflect.Method getToBeInvoked​(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Class<?>... paramCls)
      Returns the specified method if it exists; otherwise null.
      static java.lang.Object invoke​(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Class<?>[] paramCls, java.lang.Object[] parameters)
      If more than one method with the same parameter types is declared in a class, and one of these methods has a return type that is more specific than any of the others, that method is returned; otherwise one of the methods is chosen arbitrarily.
      static java.lang.Object invoke​(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
      Invokes the specified method with the given parameters and returns the value (which may be void of course. )
      static java.lang.Object invoke​(java.lang.Object target, java.lang.String methodName, java.lang.Object... parameters)
      For invoking member methods.
      private static java.lang.Object invoke​(java.lang.reflect.Method method, java.lang.Object target, java.lang.Object... parameters)
      Invokes the specified method with the given parameters and returns the value (which may be void of course. )
      static java.lang.Object invokeStatic​(java.lang.Class<?> aClass, java.lang.String methodName, java.lang.Object... parameters)
      For invoking static methods.
      private static boolean methodMatches​(java.lang.reflect.Method cand, java.lang.String methodName, java.lang.Object... parameters)
      Returns whether the given method matches the name and the parameter list.
      private static boolean paramsMatch​(java.lang.Class<?>[] paramTypes, java.lang.Object... parameters)
      Returns whether the given the parameter list matches the classes.
      private static java.lang.String paramsToString​(java.lang.Class<?>... paramCls)
      Converts a list of classes into their string-representation.
      static void setField​(java.lang.Class<?> aClass, java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
      If the type of the specified field extends Object, invoking this method acts like target.field = value; otherwise the argument value is unwrapped first.
      static void setField​(java.lang.Class<?> aClass, java.lang.String fieldName, java.lang.Object value)
      If the type of the specified field extends Object, invoking this method acts like aClass.fieldName = value; otherwise the argument value is unwrapped first.
      static void setField​(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
      If the type of the specified field extends Object, invoking this method acts like target.fieldName = value; otherwise the argument value is unwrapped first.
      • Methods inherited from class java.lang.Object

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

      • Accessor

        private Accessor()
        Formally to create a new Accessor instance but intended in contrary to prevent an Accessor from being instantiated. Note that the modifiers final and abstract are mutually exclusive and so this trick is the only remaining possibility.
    • Method Detail

      • paramsToString

        private static java.lang.String paramsToString​(java.lang.Class<?>... paramCls)
        Converts a list of classes into their string-representation.
        Parameters:
        paramCls - an array of Classes. The entries in the array may well be null.
        Returns:
        a comma separated sequence of the classes given.
      • invoke

        private static java.lang.Object invoke​(java.lang.reflect.Method method,
                                               java.lang.Object target,
                                               java.lang.Object... parameters)
                                        throws java.lang.reflect.InvocationTargetException
        Invokes the specified method with the given parameters and returns the value (which may be void of course. )
        Parameters:
        method - a Method.
        target - the target to which the specified method is to be applied. the target must be null if and only if the method specified is static.
        parameters - the list of parameters used when invoking method. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).
        Returns:
        The result of invoking the specified method with the given parameters. If the method has no return value, null is returned.
        Throws:
        java.lang.IllegalArgumentException -
        • if the specified method is static but target != null.
        • if the specified method is a member method but target == null.
        java.lang.IllegalStateException - with message "Method should be accessible; still is not. " if the method is not accessible.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the method invoked. Unwrap it using Throwable.getCause().
      • getParamCls

        private static java.lang.Class<?>[] getParamCls​(java.lang.Object... parameters)
        Returns the classes of the given parameters.
        Parameters:
        parameters - a parameter list represented as an array of Objects.
        Returns:
        the classes of the given parameters. For a null-parameter, the null-class is returned.
      • getField

        public static java.lang.Object getField​(java.lang.Object target,
                                                java.lang.String fieldName)
                                         throws java.lang.NoSuchFieldException
        Returns the value of the specified member field resp. its wrapper.
        Parameters:
        target - the instance for which a field is to be accessed.
        fieldName - the name of the field.
        Returns:
        If the type of the specified field is Object, this object is returned directly; otherwise the value of the field is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        Throws:
        java.lang.NoSuchFieldException - if the specified object does not contain a field with the given name, e.g. because fieldName == null.
        java.lang.IllegalArgumentException - if the target is null or if the specified field is static.
        See Also:
        setField(Object, String, Object)
      • getField

        public static java.lang.Object getField​(java.lang.Class<?> aClass,
                                                java.lang.String fieldName)
                                         throws java.lang.NoSuchFieldException
        Returns the value of the specified static field resp. its wrapper.
        Parameters:
        aClass - the class for which a static field is to be accessed. Typically one will use the expression <classname>.class to determine the class-object.
        fieldName - the name of the field.
        Returns:
        If the value of the specified field is an Object, this object is returned directly; otherwise the value of the field is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        Throws:
        java.lang.NoSuchFieldException - if the specified class does not contain a field with the given name, e.g. because fieldName == null.
        java.lang.IllegalArgumentException - if the class-parameter is null or if the specified field is not static.
        See Also:
        setField(Class, String, Object)
      • getFieldObj

        private static java.lang.reflect.Field getFieldObj​(java.lang.Class<?> aClass,
                                                           java.lang.String fieldName,
                                                           boolean shouldBeStatic)
                                                    throws java.lang.NoSuchFieldException
        Returns the specified Field object if possible. This method is commonly used by the methods named getField and setField.
        Parameters:
        aClass - a Class object. Typically one will use the expression <classname>.class to determine the class-object.
        fieldName - the name of a field to look for in the specified class and its superclasses.
        shouldBeStatic - whether the specified field should static.
        Returns:
        the specified Field object (Should never return null) made accessible if it exists.
        Throws:
        java.lang.IllegalArgumentException - if the "null-class" is specified.
        java.lang.NoSuchFieldException - if the specified class and none of its superclasses containd a field with the given name, e.g. because fieldName == null.
      • getField

        public static java.lang.Object getField​(java.lang.Class<?> aClass,
                                                java.lang.Object target,
                                                java.lang.String fieldName)
                                         throws java.lang.NoSuchFieldException
        Returns the value of the specified static field or member field resp. its wrapper. Use getField(Class, String) or getField(Object, String) if possible.
        Parameters:
        aClass - Some class object. Either target == null or target instanceof aClass.
        target - the object for which a field is to be accessed. For static fields, this must be null; whereas for memeber fields this has to be the corresponding instance.
        fieldName - the name of the field.
        Returns:
        If the value of the specified field is an Object, this object is returned directly; otherwise the value of the field is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        Throws:
        java.lang.NoSuchFieldException - if the specified class does not contain a field with the given name, e.g. because fieldName == null.
        java.lang.IllegalArgumentException -
        • if the null-class is specified
        • if the target is null whereas the specified field is a member field.
        • if the target is not null whereas the specified field is static.
        See Also:
        setField(Class, Object, String, Object)
      • setField

        public static void setField​(java.lang.Object target,
                                    java.lang.String fieldName,
                                    java.lang.Object value)
                             throws java.lang.NoSuchFieldException
        If the type of the specified field extends Object, invoking this method acts like target.fieldName = value; otherwise the argument value is unwrapped first. Note that there is no way to assign a value to a field which is declared final.
        Parameters:
        target - the object for which a field is to be accessed.
        fieldName - the name of the field.
        value - If the type of the specified field extends Object, the type of value must be a supertype; otherwise it must be the corresponding wrapper. E.g. the field declared by int intField; is set by setField(target, "intField", new Integer(5)) instead of setField(target, "intField", 5).
        Throws:
        java.lang.NoSuchFieldException - if the specified class does not contain a field with the given name.
        java.lang.IllegalArgumentException - if the target is null or if the specified field is static or if the specified field is declared final.
        See Also:
        getField(Object, String)
      • setField

        public static void setField​(java.lang.Class<?> aClass,
                                    java.lang.String fieldName,
                                    java.lang.Object value)
                             throws java.lang.NoSuchFieldException
        If the type of the specified field extends Object, invoking this method acts like aClass.fieldName = value; otherwise the argument value is unwrapped first. Note that there is no way to assign a value to a field which is declared final.
        Parameters:
        aClass - the class for which a static field is to be accessed. Typically one will use the expression <classname>.class to determine the class-object.
        fieldName - the name of the field.
        value - If the type of the specified field extends Object, the type of value must be a supertype; otherwise it must be the corresponding wrapper. E.g. the field declared by static int intField; is set by setField(aClass, "intField", new Integer(5)) instead of setField(aClass, "intField", 5).
        Throws:
        java.lang.NoSuchFieldException - if the specified class does not contain a field with the given name.
        java.lang.IllegalArgumentException - if aClass == null or if the specified field is not static or if the specified field is declared final.
        See Also:
        getField(Class, String)
      • setField

        public static void setField​(java.lang.Class<?> aClass,
                                    java.lang.Object target,
                                    java.lang.String fieldName,
                                    java.lang.Object value)
                             throws java.lang.NoSuchFieldException
        If the type of the specified field extends Object, invoking this method acts like target.field = value; otherwise the argument value is unwrapped first. Note that there is no way to assign a value to a field which is declared final.
        Parameters:
        aClass - Some class object. Either target == null or target instanceof aClass.
        target - the object for which a field is to be accessed.
        fieldName - the name of the field.
        value - If the type of the specified field extends Object, the type of value must be the same; otherwise it must be the corresponding wrapper.
        Throws:
        java.lang.NoSuchFieldException - if the specified class does not contain a field with the given name.
        java.lang.IllegalArgumentException -
        • if the null-class is specified
        • if the target is null whereas the specified field is a member field.
        • if the target is not null whereas the specified field is static.
        • if the specified field is declared final.
        • if the specified field is primitive but a null-value is tried to be assigned.
        See Also:
        getField(Class, Object, String)
      • invokeStatic

        public static java.lang.Object invokeStatic​(java.lang.Class<?> aClass,
                                                    java.lang.String methodName,
                                                    java.lang.Object... parameters)
                                             throws java.lang.reflect.InvocationTargetException
        For invoking static methods.
        Parameters:
        aClass - The class of the static method to be invoked. The method is looked up recursively in the superclasses of aClass. Typically one will use the expression <classname>.class to determine the class-object.
        methodName - the short name of a static method. Short means without package or class.
        parameters - the list of parameters used when invoking methodName. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).
        Returns:
        • If the return type of the specified method is Object or a subclass, the return value is returned directly;
        • if it is a primitive type, it is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        • If the return type is void, i.e. java.lang.Void.TYPE, null is returned.
        Throws:
        java.lang.IllegalArgumentException -
        • if aClass == null.
        • if the specified method does not exist or is not unique.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the method invoked. Unwrap it using Throwable.getCause().
        See Also:
        invoke(Class, Object, String, Object...)
      • invoke

        public static java.lang.Object invoke​(java.lang.Object target,
                                              java.lang.String methodName,
                                              java.lang.Object... parameters)
                                       throws java.lang.reflect.InvocationTargetException
        For invoking member methods.
        Parameters:
        target - the target on which the specified member method is to be applied.
        methodName - the short name of a static method. Short means without package or class.
        parameters - the list of parameters used when invoking methodName. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).
        Returns:
        • If the return type of the specified method is Object or a subclass, the return value is returned directly;
        • if it is a primitive type, it is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        • If the return type is void, i.e. java.lang.Void.TYPE, null is returned.
        Throws:
        java.lang.IllegalArgumentException -
        • if target == null.
        • if the specified method does not exist or is not unique.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the method invoked. Unwrap it using Throwable.getCause().
        See Also:
        invoke(Class, Object, String, Object...)
      • invoke

        public static java.lang.Object invoke​(java.lang.Class<?> aClass,
                                              java.lang.Object target,
                                              java.lang.String methodName,
                                              java.lang.Object... parameters)
                                       throws java.lang.reflect.InvocationTargetException
        Invokes the specified method with the given parameters and returns the value (which may be void of course. )

        CAUTION: This may cause an exception although the runtime system finds out which method is ment. This is the case if for example methods exa(int) and exa(Integer) are present: both are invoked with invoke(cls, target, "exa", new Integer(0))}. In this case use invoke(Class, Object, String, Class[], Object[]) instead.

        Parameters:
        aClass - The class of the method to be invoked. The method is looked up recursively in the superclasses of aClass. Typically one will use the expression <classname>.class to determine the class-object.
        target - the target to which the specified method is to be applied. the target must be null if and only if the method specified is static.
        methodName - the short name of a method. Short means without package or class.
        parameters - the list of parameters used when invoking methodName. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).
        Returns:
        • If the return type of the specified method is Object or a subclass, the return value is returned directly;
        • if it is a primitive type, it is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        • If the return type is void, i.e. java.lang.Void.TYPE, null is returned.
        Throws:
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the method invoked. Unwrap it using Throwable.getCause().
        java.lang.IllegalArgumentException -
        • if aClass == null.
        • if the specified method is static but target != null.
        • if the specified method is a member method but target == null.
        • if the specified method does not exist or is not unique.
      • invoke

        public static java.lang.Object invoke​(java.lang.Class<?> aClass,
                                              java.lang.Object target,
                                              java.lang.String methodName,
                                              java.lang.Class<?>[] paramCls,
                                              java.lang.Object[] parameters)
                                       throws java.lang.reflect.InvocationTargetException
        If more than one method with the same parameter types is declared in a class, and one of these methods has a return type that is more specific than any of the others, that method is returned; otherwise one of the methods is chosen arbitrarily.
        Parameters:
        aClass - The class of the method to be invoked. The method is looked up recursively in the superclasses of aClass. Typically one will use the expression <classname>.class to determine the class-object.
        target - the target to which the specified method is to be applied. If the method specified is static, the target is ignored. Convention: should be null in this case.
        methodName - the short name of a method. Short means without package or class.
        paramCls - the types specifying the parameter list of the desired method. Typically one will use the expression <classname>.class to denote a class, even a primitive one. For primitive types, the alternatives java.lang.Boolean.TYPE java.lang.Character.TYPE java.lang.Byte.TYPE java.lang.Short.TYPE java.lang.Integer.TYPE java.lang.Long.TYPE java.lang.Float.TYPE java.lang.Double.TYPE (java.lang.Void.TYPE) are available. For hidden inner classes, e.g. private ones, getInnerClass(Class, String[]) and getInnerClass(Class, String) may be used. If an object i of a desired class is present and if the class of is i not primitive, i.getClass() returns the desired class object as well.
        parameters - the list of parameters used when invoking methodName. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).
        Returns:
        • If the return type of the specified method is Object or a subclass, the return value is returned directly;
        • if it is a primitive type, it is wrapped in an object of the appropriate class. E.g. new Integer(5) is returned instead of the int-value 5.
        • If the return type is void, i.e. java.lang.Void.TYPE, null is returned.
        Throws:
        java.lang.IllegalArgumentException - if the specified method does not exist.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the method invoked. Unwrap it using Throwable.getCause().
        See Also:
        invoke(Class, Object, String, Class[], Object[])
      • getToBeInvoked

        static java.lang.reflect.Method getToBeInvoked​(java.lang.Class<?> aClass,
                                                       java.lang.String methodName,
                                                       java.lang.Class<?>... paramCls)
        Returns the specified method if it exists; otherwise null. Note that it is searched in the superclasses as well.
        Parameters:
        aClass - the Class to start searching the method. Typically one will use the expression <classname>.class to determine the class-object.
        methodName - the name of the method to be returned.
        paramCls - the types specifying the parameter list of the desired method.
        Returns:
        the method with the specified name and parameter list. The search is started in candClass and descends recursively until a method is found. If no method is found, null is returned.
      • create

        public static <T> T create​(java.lang.Class<T> aClass,
                                   java.lang.Object... parameters)
                            throws java.lang.InstantiationException,
                                   java.lang.reflect.InvocationTargetException
        Returns an object created by the specified constructor. Note that this method is not applicable to interfaces, primitive types, array classes, or void. In the context of this package, these restrictions are not severe.
        Parameters:
        aClass - the class of the instance to be created. Typically one will use the expression <classname>.class to determine the class-object.
        parameters - the list of parameters of the specified constructor. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).

        Note also that for static inner classes, formally the surrounding instance is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.

        Returns:
        an Object created by the specified constructor.
        Throws:
        java.lang.InstantiationException - if the instantiation with the specified constructor failed.
        java.lang.IllegalArgumentException - if the specified constructor does not exist or is not unique.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the constructor invoked. Unwrap it using Throwable.getCause().
      • create

        public static <T> T create​(java.lang.Class<T> aClass,
                                   java.lang.Class<?>[] paramCls,
                                   java.lang.Object... parameters)
                            throws java.lang.NoSuchMethodException,
                                   java.lang.InstantiationException,
                                   java.lang.reflect.InvocationTargetException
        Returns an object created by the specified constructor. Note that this method is not applicable to interfaces, primitive types, array classes, or void. In the context of this package, these restrictions are not severe.
        Parameters:
        aClass - the class of the instance to be created Typically one will use the expression <classname>.class to determine the class-object.
        paramCls - the types specifying the parameter list of the desired method. Typically one will use the expression <classname>.class to denote a class, even a primitive one. For primitive types, the alternatives java.lang.Boolean.TYPE java.lang.Character.TYPE java.lang.Byte.TYPE java.lang.Short.TYPE java.lang.Integer.TYPE java.lang.Long.TYPE java.lang.Float.TYPE java.lang.Double.TYPE (java.lang.Void.TYPE) are available. For hidden inner classes, e.g. private ones, getInnerClass(Class, String[]) and getInnerClass(Class, String) may be used. If an object i of a desired class is present and if the class of is i not primitive, i.getClass() returns the desired class object as well.

        Note also that for static inner classes, formally the surrounding class is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.

        parameters - the list of parameters of the specified constructor. Note that parameters of elementary types have to be wrapped in an object (e.g. write new Integer(8) instead of just 8).

        Note also that for static inner classes, formally the surrounding instance is prefixed as a parameter. ****** i think this does not apply to methods, but actually i did not try yet.

        Returns:
        an Object created by the specified constructor.
        Throws:
        java.lang.InstantiationException - if the instantiation with the specified constructor failed.
        java.lang.NoSuchMethodException - if the specified constructor does not exist.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the constructor invoked. Unwrap it using Throwable.getCause().
      • create

        private static <T> T create​(java.lang.reflect.Constructor<T> toBeInvoked,
                                    java.lang.Object... parameters)
                             throws java.lang.InstantiationException,
                                    java.lang.reflect.InvocationTargetException
        Invoke the specified constructor with the given arguments.
        Parameters:
        toBeInvoked - some Constructor.
        parameters - parameters fitting the given Constructor.
        Throws:
        java.lang.InstantiationException - if the instantiation with the specified constructor failed.
        java.lang.reflect.InvocationTargetException - to wrap an exception thrown by the constructor invoked. Unwrap it using Throwable.getCause().
      • methodMatches

        private static boolean methodMatches​(java.lang.reflect.Method cand,
                                             java.lang.String methodName,
                                             java.lang.Object... parameters)
        Returns whether the given method matches the name and the parameter list.
        Parameters:
        cand - either a Constructor or a Method.
        methodName - the required name of a Method. If cand is a constructor, this field is ignored; if it is a method the name of which does not match, null is returned.
        parameters - the list of parameters.
        Returns:
        true if and only if all of the following conditions are satisfied:
        Throws:
        java.lang.IllegalStateException - if cand is neither a method nor a constructor.
      • constructorMatches

        private static <T> boolean constructorMatches​(java.lang.reflect.Constructor<T> cand,
                                                      java.lang.Object... parameters)
        Returns whether the given constructor matches the parameter list.
        Parameters:
        cand - either a Constructor or a Method.
        parameters - the list of parameters.
        Returns:
        a true if and only if the parameter list of cand matches parameters as specified for paramsMatch(java.lang.Class<?>[], java.lang.Object...).
        Throws:
        java.lang.IllegalStateException - if cand is neither a method nor a constructor.
      • paramsMatch

        private static boolean paramsMatch​(java.lang.Class<?>[] paramTypes,
                                           java.lang.Object... parameters)
        Returns whether the given the parameter list matches the classes.
        Parameters:
        paramTypes - the list of classes of parameters.
        parameters - the list of parameters.
        Returns:
        a true if and only if the parameter list of cand matches parameters up to equivalence of primitive types and their wrappers. This includes that the two arrays have the same length.
        Throws:
        java.lang.IllegalStateException - if cand is neither a method nor a constructor.
      • getMethod

        private static java.lang.reflect.Method getMethod​(java.lang.Class<?> aClass,
                                                          java.lang.String methodName,
                                                          java.lang.reflect.Method[] cands,
                                                          java.lang.Object... parameters)
        Returns the specified method if cands offers exactly one possibility.
        Parameters:
        aClass - the Class in which to search a Method. For error/exception messages only.
        methodName - the name of the desired Method.
        cands - an array of Methods the return value of this method is choosen from.
        parameters - the list of parameters.
        Returns:
        a Method; possibly null if the specified Method does not exist in the specified class.
        Throws:
        java.lang.IllegalArgumentException - if the specified constructor or method is not unique.
      • getConstructor

        private static <T> java.lang.reflect.Constructor<T> getConstructor​(java.lang.Class<T> aClass,
                                                                           java.lang.Object... parameters)
        Returns the specified constructor if aClas offers exactly one possibility with the given parameters.
        Parameters:
        aClass - the Class in which to search a Constructor. For error/exception messages only.
        parameters - the list of parameters.
        Returns:
        a Constructor or null if the specified constructor does not exist in the specified class.
        Throws:
        java.lang.IllegalArgumentException - if the specified constructor or method is not unique.
      • getInnerClass

        public static java.lang.Class<?> getInnerClass​(java.lang.Class<?> enclosingCls,
                                                       java.lang.String[] pathToInner)
        Returns the inner class of enclosingCls with the specified name innerClsName. By inner classes we mean both static inner classes and member classes. Also inherited classes are included.
        Parameters:
        enclosingCls - a Class object which may also be an inner class, static or not.
        pathToInner - a short path of a class enclosed by enclosingCls. If the name of the enclosing class is enclosing then the name of the inner class has the form enclosing$shortName. Here, "$" is as specified in INNER_SEPARATOR. This remains also true for nested inner classes. In this case, shortName itself has the form cls1$...$clsN. The the corresponding short path is new String[] {cls1, ..., clsN}. For paths with length 1 one may use getInnerClass(Class, String) instead.
        Returns:
        the Class object represented by the parameters.
        Throws:
        java.lang.IllegalArgumentException - if either of the parameters is null.
        java.lang.IllegalArgumentException - if the specified class does not exist.
        See Also:
        getInnerClass(Class, String)
      • getInnerClass

        public static java.lang.Class<?> getInnerClass​(java.lang.Class<?> enclosingCls,
                                                       java.lang.String innerClsName)
        Returns the inner class of enclosingCls with the specified name innerClsName. By inner classes we mean both static inner classes and member classes. Also inherited classes are included.
        Parameters:
        enclosingCls - a Class object which may also be an inner class, static or not.
        innerClsName - a short name of a class enclosed by enclosingCls. If the name of the enclosing class is enclosing then the name of the inner class has the form enclosing$shortName. Here, "$" is as specified in INNER_SEPARATOR. This remains also true for nested inner classes. In this case, shortName itself has the form cls1$...$clsN. CAUTION In this case apply method getInnerClass(Class, String[]) instead.
        Returns:
        the Class object represented by the parameters.
        Throws:
        java.lang.IllegalArgumentException - if either of the parameters is null or if the specified class does not exist.
        See Also:
        getInnerClass(Class, String[])