messif.utility.reflection
Class Instantiators

java.lang.Object
  extended by messif.utility.reflection.Instantiators

public abstract class Instantiators
extends java.lang.Object

Collection of utility methods for Instantiators.


Constructor Summary
Instantiators()
           
 
Method Summary
static java.lang.Object createInstanceStaticField(java.lang.Class<?> clazz, java.lang.String name)
          Get instance from a static field of a class.
static
<E> E
createInstanceUsingFactoryMethod(java.lang.Class<?> declaringClass, java.lang.String methodName, java.lang.Class<E> instanceClass, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.lang.String... arguments)
          Creates a new instance of a class.
static
<E> E
createInstanceUsingFactoryMethod(java.lang.Class<E> instanceClass, java.lang.String methodName, java.lang.Object... arguments)
          Creates a new instance of a class.
static
<E> E
createInstanceWithInheritableArgs(java.lang.Class<E> instanceClass, java.lang.Object... arguments)
          Creates a new instance of a class.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String... arguments)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String[] arguments, int argStartIndex)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String[] arguments, int argStartIndex, int argEndIndex)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String[] arguments, int argStartIndex, int argEndIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String[] arguments, int argStartIndex, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors, java.lang.String[] arguments, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Creates a new instance of a class using string arguments for its constructor.
static
<E> E
createInstanceWithStringArgs(java.lang.String signature, java.lang.Class<E> checkClass, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Creates a new instance of a class with a string constructor/factory-method/static-field signature.
static
<E> java.lang.reflect.Constructor<E>
getConstructor(java.lang.Class<E> clazz, boolean convertStringArguments, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.lang.Object[] arguments)
          Returns a constructor for the specified class that accepts the specified arguments.
static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz, java.lang.String methodName, boolean convertStringArguments, java.util.Map<java.lang.String,java.lang.Object> namedInstances, java.lang.Object[] arguments)
          Returns a method for the specified class that accepts the specified arguments.
static boolean isPrototypeMatching(java.lang.Class<?>[] methodTypes, java.lang.Class<?>[] methodPrototype)
          Test argument array, if it is compatible with the provided prototype.
static boolean isPrototypeMatching(java.lang.Class<?>[] methodTypes, java.lang.Class<?>[] methodPrototype, int skipIndex)
          Test argument array, if it is compatible with the provided prototype.
static boolean isPrototypeMatching(java.lang.Class<?>[] prototype, java.lang.Object[] arguments, boolean convertStringArguments, java.util.Map<java.lang.String,java.lang.Object> namedInstances)
          Test argument array, if it is compatible with the provided prototype.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Instantiators

public Instantiators()
Method Detail

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String[] arguments,
                                                 int argStartIndex,
                                                 int argEndIndex,
                                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the number of arguments equal to argEndIndex - argStartIndex + 1 are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
argStartIndex - index in the string arguments array from which to expect arguments (all the previous items are ignored)
argEndIndex - index in the string arguments array to which to expect arguments (all the following items are ignored)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String[] arguments,
                                                 int argStartIndex,
                                                 int argEndIndex)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the number of arguments equal to argEndIndex - argStartIndex + 1 are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Note also that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full createInstanceWithStringArgs method instead.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
argStartIndex - index in the string arguments array from which to expect arguments (all the previous items are ignored)
argEndIndex - index in the string arguments array to which to expect arguments (all the following items are ignored)
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String[] arguments,
                                                 int argStartIndex)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the number of arguments equal to arguments.length - argStartIndex are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Note also that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full createInstanceWithStringArgs method instead.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
argStartIndex - index in the string arguments array from which to expect arguments (all the previous items are ignored)
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String[] arguments,
                                                 int argStartIndex,
                                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the number of arguments equal to arguments.length - argStartIndex are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
argStartIndex - index in the string arguments array from which to expect arguments (all the previous items are ignored)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String... arguments)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the same number of arguments as the length of arguments are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Note also that LocalAbstractObject parameters will not be converted. For this fuctionality, use the full createInstanceWithStringArgs method instead.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.util.List<java.lang.reflect.Constructor<E>> constructors,
                                                 java.lang.String[] arguments,
                                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                      throws java.lang.reflect.InvocationTargetException
Creates a new instance of a class using string arguments for its constructor. The list of constructors of the desired class must be provided as constructors argument. The constructors are tried one by one from this list and if the arguments are convertible to the arguments of that constructor, a new instance is created.

Note that only constructors with the same number of arguments as the length of arguments are tried. If there are several constructors with the same number of arguments, the first (in the order of the list) constructor that succeeds in converting string arguments will be used.

Note also that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in constructors.

Type Parameters:
E - the type of the instantiated object
Parameters:
constructors - the list of constructors of the desired class to try
arguments - the string arguments for the constructor that will be converted to correct types
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
a new instance of the class the constructors were specified for
Throws:
java.lang.reflect.InvocationTargetException - if the constructor can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation

createInstanceWithStringArgs

public static <E> E createInstanceWithStringArgs(java.lang.String signature,
                                                 java.lang.Class<E> checkClass,
                                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances)
                                      throws java.lang.reflect.InvocationTargetException,
                                             java.lang.ClassNotFoundException
Creates a new instance of a class with a string constructor/factory-method/static-field signature. The string must contain a fully specified name of constructor optionally with arguments enclosed by parenthesis, e.g.:
      messif.pivotselection.StreamSequencePivotChooser(messif.objects.impl.MetaObjectMap, file)
 
Or the string can be a fully specified name of a factory method (package.class.methodName) with arguments enclosed by parenthesis, e.g.:
      messif.utility.ExtendedProperties.getProperties(file)
 
Or the string can be a fully specified name of a public static field (package.class.fieldName), e.g.:
     messif.buckets.index.LocalAbstractObjectOrder.locatorToLocalObjectComparator
 

Note that only types convertible by Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map) method can be used in arguments.

Type Parameters:
E - the type of the instantiated object
Parameters:
signature - constructor or factory method call with string arguments or a static field
checkClass - the superclass of (or the same class as) the instantiated object
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
a new instance of the specified object
Throws:
java.lang.reflect.InvocationTargetException - if the constructor/factory-method/static-field can't be found for the specified arguments, the argument string-to-type convertion has failed or there was an error during instantiation
java.lang.ClassNotFoundException - if the class in the constructor signature was not found or is not a descendant of checkClass

createInstanceWithInheritableArgs

public static <E> E createInstanceWithInheritableArgs(java.lang.Class<E> instanceClass,
                                                      java.lang.Object... arguments)
                                           throws java.lang.NoSuchMethodException,
                                                  java.lang.reflect.InvocationTargetException
Creates a new instance of a class. First, a constructor for the specified arguments is searched in the provided class. Then, an instance is created and returned.

Type Parameters:
E - the type of the instantiated object
Parameters:
instanceClass - the class for which to create an instance
arguments - the arguments for the constructor
Returns:
a new instance of the class
Throws:
java.lang.NoSuchMethodException - if there was no constructor for the specified list of arguments
java.lang.reflect.InvocationTargetException - if there was an exception during instantiation

createInstanceUsingFactoryMethod

public static <E> E createInstanceUsingFactoryMethod(java.lang.Class<E> instanceClass,
                                                     java.lang.String methodName,
                                                     java.lang.Object... arguments)
                                          throws java.lang.NoSuchMethodException,
                                                 java.lang.reflect.InvocationTargetException
Creates a new instance of a class. First, a factory method for the specified arguments is searched in the provided class and its ancestors. Then, an instance is created and returned.

Type Parameters:
E - the type of the instantiated object
Parameters:
instanceClass - the class for which to create an instance
methodName - the name of the factory method
arguments - the arguments for the factory method
Returns:
a new instance of the class
Throws:
java.lang.NoSuchMethodException - if there was no factory method for the specified list of arguments
java.lang.reflect.InvocationTargetException - if there was an exception during instantiation

createInstanceUsingFactoryMethod

public static <E> E createInstanceUsingFactoryMethod(java.lang.Class<?> declaringClass,
                                                     java.lang.String methodName,
                                                     java.lang.Class<E> instanceClass,
                                                     java.util.Map<java.lang.String,java.lang.Object> namedInstances,
                                                     java.lang.String... arguments)
                                          throws java.lang.IllegalArgumentException,
                                                 java.lang.reflect.InvocationTargetException
Creates a new instance of a class. First, a factory method for the specified arguments is searched in the provided class and its ancestors. Then, string arguments are converted to proper types for the method that was found. Finally, an instance is created and returned.

Type Parameters:
E - the type of the instantiated object
Parameters:
declaringClass - the class where the factory method is declared
methodName - the name of the factory method
instanceClass - the class for which to create an instance
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
arguments - the arguments for the factory method
Returns:
a new instance of the class
Throws:
java.lang.IllegalArgumentException - if there was no factory method for the specified list of arguments or has wrong parameters
java.lang.reflect.InvocationTargetException - if there was an exception during instantiation

createInstanceStaticField

public static java.lang.Object createInstanceStaticField(java.lang.Class<?> clazz,
                                                         java.lang.String name)
                                                  throws java.lang.IllegalArgumentException
Get instance from a static field of a class.

Parameters:
clazz - the class the field of which to access
name - the name of the clazz's static field
Returns:
a new instance of the class
Throws:
java.lang.IllegalArgumentException - if there was no public static field for the specified clazz and name

getConstructor

public static <E> java.lang.reflect.Constructor<E> getConstructor(java.lang.Class<E> clazz,
                                                                  boolean convertStringArguments,
                                                                  java.util.Map<java.lang.String,java.lang.Object> namedInstances,
                                                                  java.lang.Object[] arguments)
                                                       throws java.lang.NoSuchMethodException
Returns a constructor for the specified class that accepts the specified arguments. The clazz's declared constructors are searched for the one that accepts the arguments. If the convertStringArguments is specified, the arguments elements are replaced with the converted types if and only if a proper constructor is found. Their types then will be compatible with the constructor.

Type Parameters:
E - the class the constructor will create
Parameters:
clazz - the class for which to get the constructor
convertStringArguments - if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
arguments - the arguments for the constructor
Returns:
a constructor for the specified class
Throws:
java.lang.NoSuchMethodException - if there was no constructor for the specified list of arguments

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class<?> clazz,
                                                 java.lang.String methodName,
                                                 boolean convertStringArguments,
                                                 java.util.Map<java.lang.String,java.lang.Object> namedInstances,
                                                 java.lang.Object[] arguments)
                                          throws java.lang.NoSuchMethodException
Returns a method for the specified class that accepts the specified arguments. The clazz's declared methods are searched for the one that accepts the arguments. If the convertStringArguments is specified, the arguments elements are replaced with the converted types if and only if a proper method is found. Their types then will be compatible with the method.

Parameters:
clazz - the class for which to get the method
methodName - the name of the method to get
convertStringArguments - if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
arguments - the arguments for the method
Returns:
a method of the specified class
Throws:
java.lang.NoSuchMethodException - if there was no method with the specified name and arguments

isPrototypeMatching

public static boolean isPrototypeMatching(java.lang.Class<?>[] methodTypes,
                                          java.lang.Class<?>[] methodPrototype,
                                          int skipIndex)
Test argument array, if it is compatible with the provided prototype. That is, the number of arguments must be equal and each argument (an item from the methodTypes) must be assignable from the respective methodPrototype item.

Parameters:
methodTypes - the tested arguments array
methodPrototype - the prototype arguments array
skipIndex - the index of an argument that is not checked for the compatibility condition
Returns:
true if the method types are compatible with the prototype

isPrototypeMatching

public static boolean isPrototypeMatching(java.lang.Class<?>[] methodTypes,
                                          java.lang.Class<?>[] methodPrototype)
Test argument array, if it is compatible with the provided prototype. That is, each item from the methodTypes must be assignable from the respective methodPrototype item.

Parameters:
methodTypes - the tested arguments array
methodPrototype - the prototype arguments array
Returns:
true if the method types are compatible with the prototype

isPrototypeMatching

public static boolean isPrototypeMatching(java.lang.Class<?>[] prototype,
                                          java.lang.Object[] arguments,
                                          boolean convertStringArguments,
                                          java.util.Map<java.lang.String,java.lang.Object> namedInstances)
Test argument array, if it is compatible with the provided prototype. That is, the number of arguments must be equal and each argument must be assignable to the respective prototype item. If the convertStringArguments is specified, the arguments elements are replaced with the converted types if and only if the method returns true.

Parameters:
prototype - the tested prototype
arguments - the tested arguments
convertStringArguments - if true the string values from the arguments are converted using Convert.stringToType(java.lang.String, java.lang.Class, java.util.Map)
namedInstances - map of named instances - an instance from this map is returned if the string matches a key in the map
Returns:
true if the arguments are compatible with the prototype