Package org.pipservices3.commons.reflect
Class TypeReflector
java.lang.Object
org.pipservices3.commons.reflect.TypeReflector
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
createInstance
(String name, Object... args) Creates an instance of an object type specified by its name.static Object
createInstance
(String name, String library, Object... args) Creates an instance of an object type specified by its name and library where it is defined.static Object
createInstanceByDescriptor
(TypeDescriptor type, Object... args) Creates an instance of an object type specified by type descriptor.static Object
createInstanceByType
(Class<?> type, Object... args) Creates an instance of an object type.static Class<?>
Gets object type by its name.static Class<?>
Gets object type by its name and library where it is defined.static Class<?>
Gets object type by type descriptor.static boolean
isPrimitive
(Object value) Checks if value has primitive type.
-
Constructor Details
-
TypeReflector
public TypeReflector()
-
-
Method Details
-
getType
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
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
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
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
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
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 typeargs
- arguments for the object constructor.- Returns:
- the created object instance.
- Throws:
Exception
- when type of instance not found- See Also:
-
isPrimitive
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:
-