Package 

Class Reflect


  • 
    public class Reflect
    
                        

    A wrapper for an Object or Class upon which reflective calls can be made.

    An example of using Reflect is

    // Static import all reflection methods to decrease verbosity import static org.joor.Reflect.*; // Wrap an Object / Class / class name with the on() method: on("java.lang.String") // Invoke constructors using the create() method: .create("Hello World") // Invoke methods using the call() method: .call("toString") // Retrieve the wrapped object
    • Method Detail

      • on

         static Reflect on(String name)

        Wrap a class name.

        This is the same as calling on(Class.forName(name))

        Parameters:
        name - A fully qualified class name
      • on

         static Reflect on(String name, ClassLoader classLoader)

        Wrap a class name, loading it via a given class loader.

        This is the same as callingon(Class.forName(name, classLoader))

        Parameters:
        name - A fully qualified class name.
        classLoader - The class loader in whose context the class should beloaded.
      • on

         static Reflect on(Class<out Object> clazz)

        Wrap a class.

        Use this when you want to access static fields and methods on a Class object, or as a basis for constructing objects of thatclass using create

        Parameters:
        clazz - The class to be wrapped
      • on

         static Reflect on(Object object)

        Wrap an object.

        Use this when you want to access instance fields and methods on any Object

        Parameters:
        object - The object to be wrapped
      • get

         <T> T get()

        Get the wrapped object

      • set

         Reflect set(String name, Object value)

        Set a field value.

        This is roughly equivalent to set. If thewrapped object is a Class, then this will set a value to a staticmember field. If the wrapped object is any other Object, thenthis will set a value to an instance member field.

        Parameters:
        name - The field name
        value - The new field value
      • get

         <T> T get(String name)

        Get a field value.

        This is roughly equivalent to get. If the wrappedobject is a Class, then this will get a value from a staticmember field. If the wrapped object is any other Object, thenthis will get a value from an instance member field.

        If you want to "navigate" to a wrapped version of the field, use field instead.

        Parameters:
        name - The field name
      • field

         Reflect field(String name)

        Get a wrapped field.

        This is roughly equivalent to get. If the wrappedobject is a Class, then this will wrap a static member field. Ifthe wrapped object is any other Object, then this wrap aninstance member field.

        Parameters:
        name - The field name
      • fields

         Map<String, Reflect> fields()

        Get a Map containing field names and wrapped values for the fields'values.

        If the wrapped object is a Class, then this will return staticfields. If the wrapped object is any other Object, then this willreturn instance fields.

        These two calls are equivalent

        on(object).field("myField"); on(object).fields().get("myField"); 
      • call

         Reflect call(String name)

        Call a method by its name.

        This is a convenience method for callingcall(name, new Object[0])

        Parameters:
        name - The method name
      • call

         Reflect call(String name, Array<Object> args)

        Call a method by its name.

        This is roughly equivalent to invoke.If the wrapped object is a Class, then this will invoke a staticmethod. If the wrapped object is any other Object, then this willinvoke an instance method.

        Just like invoke, this will try to wrapprimitive types or unwrap primitive type wrappers if applicable. Ifseveral methods are applicable, by that rule, the first one encounteredis called. i.e. when calling

        on(...).call("method", 1, 1); 
        The first of the following methods will be called:
        public void method(int param1, Integer param2);
        public void method(Integer param1, int param2);
        public void method(Number param1, Number param2);
        public void method(Number param1, Object param2);
        public void method(int param1, Object param2);
        

        The best matching method is searched for with the following strategy:

        • public method with exact signature match in class hierarchy
        • non-public method with exact signature match on declaring class
        • public method with similar signature in class hierarchy
        • non-public method with similar signature on declaring class
        Parameters:
        name - The method name
        args - The method arguments
      • create

         Reflect create()

        Call a constructor.

        This is a convenience method for callingcreate(new Object[0])

      • create

         Reflect create(Array<Object> args)

        Call a constructor.

        This is roughly equivalent to newInstance.If the wrapped object is a Class, then this will create a newobject of that class. If the wrapped object is any other Object,then this will create a new object of the same type.

        Just like newInstance, this will try towrap primitive types or unwrap primitive type wrappers if applicable. Ifseveral constructors are applicable, by that rule, the first oneencountered is called. i.e. when calling

        on(C.class).create(1, 1); 
        The first of the following constructors will be applied:
        public C(int param1, Integer param2);
        public C(Integer param1, int param2);
        public C(Number param1, Number param2);
        public C(Number param1, Object param2);
        public C(int param1, Object param2);
        
        Parameters:
        args - The constructor arguments
      • as

         <P> P as(Class<P> proxyType)

        Create a proxy for the wrapped object allowing to typesafely invokemethods on it using a custom interface

        Parameters:
        proxyType - The interface type that is implemented by the proxy
      • wrapper

         static Class<out Object> wrapper(Class<out Object> type)

        Get a wrapper type for a primitive type, or the argument type itself, ifit is not a primitive type.