Class TypeReflector

java.lang.Object
org.pipservices3.commons.reflect.TypeReflector

public class TypeReflector extends Object
Helper class to perform object type introspection and object instantiation.

This class has symmetric implementation across all languages supported by Pip.Services toolkit and used to support dynamic data processing.

Because all languages have different casing and case sensitivity rules, this TypeReflector treats all type names as case insensitive.

### Example ###

 
 TypeDescriptor descriptor = new TypeDescriptor("MyObject", "mylibrary");
 TypeReflector.getTypeByDescriptor(descriptor);
 MyObject myObj = TypeReflector.createInstanceByDescriptor(descriptor);

 TypeDescriptor.isPrimitive(myObj);        // Result: false
 TypeDescriptor.isPrimitive(123);                // Result: true
 
 
See Also:
  • Constructor Details

    • TypeReflector

      public TypeReflector()
  • Method Details

    • getType

      public static Class<?> getType(String name, String library)
      Gets object type by its name and library where it is defined.
      Parameters:
      name - an object type name.
      library - a library where the type is defined
      Returns:
      the object type or null is the type wasn't found.
    • getType

      public static Class<?> getType(String name)
      Gets object type by its name.
      Parameters:
      name - an object type name.
      Returns:
      the object type or null is the type wasn't found.
    • getTypeByDescriptor

      public static Class<?> getTypeByDescriptor(TypeDescriptor type)
      Gets object type by type descriptor.
      Parameters:
      type - a type descriptor that points to an object type
      Returns:
      the object type or null is the type wasn't found.
      See Also:
    • createInstanceByType

      public static Object createInstanceByType(Class<?> type, Object... args) throws Exception
      Creates an instance of an object type.
      Parameters:
      type - an object type (factory function) to create.
      args - arguments for the object constructor.
      Returns:
      the created object instance.
      Throws:
      Exception - when constructors with parameters are not supported
    • createInstance

      public static Object createInstance(String name, String library, Object... args) throws Exception
      Creates an instance of an object type specified by its name and library where it is defined.
      Parameters:
      name - an object type name.
      library - a library (module) where object type is defined.
      args - arguments for the object constructor.
      Returns:
      the created object instance.
      Throws:
      Exception - when type of instance not found
      See Also:
    • createInstance

      public static Object createInstance(String name, Object... args) throws Exception
      Creates an instance of an object type specified by its name.
      Parameters:
      name - an object type name.
      args - arguments for the object constructor.
      Returns:
      the created object instance.
      Throws:
      Exception - when type of instance not found
      See Also:
    • createInstanceByDescriptor

      public static Object createInstanceByDescriptor(TypeDescriptor type, Object... args) throws Exception
      Creates an instance of an object type specified by type descriptor.
      Parameters:
      type - a type descriptor that points to an object type
      args - arguments for the object constructor.
      Returns:
      the created object instance.
      Throws:
      Exception - when type of instance not found
      See Also:
    • isPrimitive

      public static boolean isPrimitive(Object value)
      Checks if value has primitive type.

      Primitive types are: numbers, strings, booleans, date and time. Complex (non-primitive types are): objects, maps and arrays

      Parameters:
      value - a value to check
      Returns:
      true if the value has primitive type and false if value type is complex.
      See Also: