Class Reflection



  • public class Reflection
    extends Object
    A set of static methods relating to reflection.
    • Field Detail

      • INVOCATION_ERROR

        public static final Object INVOCATION_ERROR
        This object is returned by invokeMethod(..) when an error occurs.
    • Constructor Detail

      • Reflection

        public Reflection()
    • Method Detail

      • getFieldValue

        public static Object getFieldValue(String className,
                                           String fieldName)
        Uses reflection to retrieve a static field from a class.
        Returns:
        null if an error occurred retrieving this value
      • parseCommaSeparatedList

        protected static Object[] parseCommaSeparatedList(String arguments)
        Return an array of objects based on a comma-separated description of its contents.
      • parse

        public static Object parse(String input)
        Parse an object as null, an int, a long, a float, a String, a static field, or "new xyz(..)" (where ".." looks recursively for a comma-separated list of arguments)).

        For example, you can call:
        parse( "new Color( 255, 0, 128 )" );
        parse( "new com.bric.swing.resources.ArrowIcon( javax.swing.SwingConstants.EAST, 24, 24 )" );
        parse( "new com.bric.swing.resources.TriangleIcon( javax.swing.SwingConstants.EAST, 24, 24, new Color(0)" );

        The class name/constants must be fully qualified.

      • construct

        protected static <T> T construct(Class<T> t,
                                         String arguments)
        This constructs an instance of t if a constructor is found that matches the arguments provided. This will not identify a varargs constructor. If multiple constructors match the arguments provided, this method will choose one to use
        Parameters:
        t - the type of class to construct.
        arguments - a comma separated list of simple arguments, such as primitives, strings, or static fields.
        Returns:
        a new instance of t created using the arguments provided, or an exception will be thrown.
      • invokeMethod

        public static Object invokeMethod(Class<?> c,
                                          Object obj,
                                          String methodName,
                                          Object[] arguments)
        This uses reflection to call a method that may not exist in the compiling JVM.
        Returns:
        INVOCATION_ERROR if an error occurs (details are printed to the console), or the return value of the invocation.
      • nameStaticField

        public static String nameStaticField(Class<?> c,
                                             Object value)
        This debugging tool combs through a class and tells you what public static field has the value you've provided.

        For example, you can call:
        nameStaticField(BufferedImage.class,new Integer(BufferedImage.TYPE_INT_ARGB))
        And this method will return "TYPE_INT_ARGB".

        Parameters:
        c - the class of interest
        value - the value. Primitives must be wrapped.
        Returns:
        the string of the field with that value, or null if not hits were found. (Or if multiple hits were found, this returns a list of possible matches.)