Class ReflectedUniverse


  • public class ReflectedUniverse
    extends Object
    A general-purpose reflection wrapper class.

    Note: use of this class is discouraged in favor of compile-time (i.e., type-safe) dependency linkages. However, there are times when it proves very useful for writing blocks of reflected code in a more succinct way than you can do with the vanilla java.lang.reflect API. Of course, debugging such reflected code becomes much more difficult—caveat emptor!

    Author:
    Curtis Rueden
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectedUniverse()
      Constructs a new reflected universe.
      ReflectedUniverse​(ClassLoader loader)
      Constructs a new reflected universe that uses the given class loader.
      ReflectedUniverse​(URL[] urls)
      Constructs a new reflected universe, with the given URLs representing additional search paths for imported classes (in addition to the CLASSPATH).
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Object exec​(String command)
      Executes a command in the universe.
      Object getVar​(String varName)
      Returns the value of a variable or field in the universe.
      boolean isAccessibilityIgnored()
      Gets whether access modifiers (protected, private, etc.) are ignored.
      static boolean isInstance​(Class<?> c, Object o)
      Returns whether the given object is compatible with the specified class for the purposes of reflection.
      static void main​(String[] args)
      Allows exploration of a reflected universe in an interactive environment.
      void setAccessibilityIgnored​(boolean ignore)
      Sets whether access modifiers (protected, private, etc.) are ignored.
      void setVar​(String varName, boolean b)
      Registers a variable of primitive type boolean in the universe.
      void setVar​(String varName, byte b)
      Registers a variable of primitive type byte in the universe.
      void setVar​(String varName, char c)
      Registers a variable of primitive type char in the universe.
      void setVar​(String varName, double d)
      Registers a variable of primitive type double in the universe.
      void setVar​(String varName, float f)
      Registers a variable of primitive type float in the universe.
      void setVar​(String varName, int i)
      Registers a variable of primitive type int in the universe.
      void setVar​(String varName, long l)
      Registers a variable of primitive type long in the universe.
      void setVar​(String varName, short s)
      Registers a variable of primitive type short in the universe.
      void setVar​(String varName, Object obj)
      Registers a variable in the universe.
    • Constructor Detail

      • ReflectedUniverse

        public ReflectedUniverse()
        Constructs a new reflected universe.
      • ReflectedUniverse

        public ReflectedUniverse​(URL[] urls)
        Constructs a new reflected universe, with the given URLs representing additional search paths for imported classes (in addition to the CLASSPATH).
      • ReflectedUniverse

        public ReflectedUniverse​(ClassLoader loader)
        Constructs a new reflected universe that uses the given class loader.
    • Method Detail

      • isInstance

        public static boolean isInstance​(Class<?> c,
                                         Object o)
        Returns whether the given object is compatible with the specified class for the purposes of reflection.
      • exec

        public Object exec​(String command)
                    throws ReflectException
        Executes a command in the universe. The following syntaxes are valid:
        • import fully.qualified.package.ClassName
        • var = new ClassName(param1, ..., paramN)
        • var.method(param1, ..., paramN)
        • var2 = var.method(param1, ..., paramN)
        • ClassName.method(param1, ..., paramN)
        • var2 = ClassName.method(param1, ..., paramN)
        • var2 = var
        Important guidelines:
        • Any referenced class must be imported first using "import".
        • Variables can be exported from the universe with getVar().
        • Variables can be imported to the universe with setVar().
        • Each parameter must be either:
          1. a variable in the universe
          2. a static or instance field (i.e., no nested methods)
          3. a string literal (remember to escape the double quotes)
          4. an integer literal
          5. a long literal (ending in L)
          6. a double literal (containing a decimal point)
          7. a boolean literal (true or false)
          8. the null keyword
        Throws:
        ReflectException
      • setVar

        public void setVar​(String varName,
                           Object obj)
        Registers a variable in the universe.
      • setVar

        public void setVar​(String varName,
                           boolean b)
        Registers a variable of primitive type boolean in the universe.
      • setVar

        public void setVar​(String varName,
                           byte b)
        Registers a variable of primitive type byte in the universe.
      • setVar

        public void setVar​(String varName,
                           char c)
        Registers a variable of primitive type char in the universe.
      • setVar

        public void setVar​(String varName,
                           double d)
        Registers a variable of primitive type double in the universe.
      • setVar

        public void setVar​(String varName,
                           float f)
        Registers a variable of primitive type float in the universe.
      • setVar

        public void setVar​(String varName,
                           int i)
        Registers a variable of primitive type int in the universe.
      • setVar

        public void setVar​(String varName,
                           long l)
        Registers a variable of primitive type long in the universe.
      • setVar

        public void setVar​(String varName,
                           short s)
        Registers a variable of primitive type short in the universe.
      • getVar

        public Object getVar​(String varName)
                      throws ReflectException
        Returns the value of a variable or field in the universe. Primitive types will be wrapped in their Java Object wrapper classes.
        Throws:
        ReflectException
      • setAccessibilityIgnored

        public void setAccessibilityIgnored​(boolean ignore)
        Sets whether access modifiers (protected, private, etc.) are ignored.
      • isAccessibilityIgnored

        public boolean isAccessibilityIgnored()
        Gets whether access modifiers (protected, private, etc.) are ignored.
      • main

        public static void main​(String[] args)
                         throws IOException
        Allows exploration of a reflected universe in an interactive environment.
        Throws:
        IOException