Class ReflectExtensions


  • @Beta
    public class ReflectExtensions
    extends java.lang.Object
    Extension methods to simplify reflective invocation of methods and fields.
    Since:
    2.3
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T get​(java.lang.Object receiver, java.lang.String fieldName)
      Retrieves the value of the given accessible field of the given receiver.
      java.lang.Object invoke​(java.lang.Object receiver, java.lang.String methodName, java.lang.Object... args)
      Invokes the first accessible method defined on the receiver'c class with the given name and a parameter list compatible to the given arguments.
      void set​(java.lang.Object receiver, java.lang.String fieldName, java.lang.Object value)
      Sets the given value on an the receivers's accessible field with the given name.
      • Methods inherited from class java.lang.Object

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

      • ReflectExtensions

        public ReflectExtensions()
    • Method Detail

      • set

        public void set​(java.lang.Object receiver,
                        java.lang.String fieldName,
                        java.lang.Object value)
                 throws java.lang.SecurityException,
                        java.lang.NoSuchFieldException,
                        java.lang.IllegalArgumentException,
                        java.lang.IllegalAccessException
        Sets the given value on an the receivers's accessible field with the given name.
        Parameters:
        receiver - the receiver, never null
        fieldName - the field's name, never null
        value - the value to set
        Throws:
        java.lang.NoSuchFieldException - see Class.getField(String)
        java.lang.SecurityException - see Class.getField(String)
        java.lang.IllegalAccessException - see Field.set(Object, Object)
        java.lang.IllegalArgumentException - see Field.set(Object, Object)
      • get

        public <T> T get​(java.lang.Object receiver,
                         java.lang.String fieldName)
                  throws java.lang.SecurityException,
                         java.lang.NoSuchFieldException,
                         java.lang.IllegalArgumentException,
                         java.lang.IllegalAccessException
        Retrieves the value of the given accessible field of the given receiver.
        Parameters:
        receiver - the container of the field, not null
        fieldName - the field's name, not null
        Returns:
        the value of the field
        Throws:
        java.lang.NoSuchFieldException - see Class.getField(String)
        java.lang.SecurityException - see Class.getField(String)
        java.lang.IllegalAccessException - see Field.get(Object)
        java.lang.IllegalArgumentException - see Field.get(Object)
      • invoke

        public java.lang.Object invoke​(java.lang.Object receiver,
                                       java.lang.String methodName,
                                       java.lang.Object... args)
                                throws java.lang.SecurityException,
                                       java.lang.IllegalArgumentException,
                                       java.lang.IllegalAccessException,
                                       java.lang.reflect.InvocationTargetException,
                                       java.lang.NoSuchMethodException
        Invokes the first accessible method defined on the receiver'c class with the given name and a parameter list compatible to the given arguments.
        Parameters:
        receiver - the method call receiver, not null
        methodName - the method name, not null
        args - the arguments for the method invocation
        Returns:
        the result of the method invocation. null if the method was of type void.
        Throws:
        java.lang.SecurityException - see Class.getMethod(String, Class...)
        java.lang.NoSuchMethodException - see Class.getMethod(String, Class...)
        java.lang.IllegalAccessException - see Method.invoke(Object, Object...)
        java.lang.IllegalArgumentException - see Method.invoke(Object, Object...)
        java.lang.reflect.InvocationTargetException - see Method.invoke(Object, Object...)