nl.jqno.equalsverifier.util
Class ConditionalInstantiator

java.lang.Object
  extended by nl.jqno.equalsverifier.util.ConditionalInstantiator

public class ConditionalInstantiator
extends Object

Allows instantiation of classes that may or may not be present on the classpath.

Author:
Jan Ouwens

Constructor Summary
ConditionalInstantiator(String fullyQualifiedClassName)
          Constructor.
 
Method Summary
 Object callFactory(String factoryMethod, Class<?>[] paramTypes, Object[] paramValues)
          Attempts to call a static factory method on the type.
 Object callFactory(String factoryTypeName, String factoryMethod, Class<?>[] paramTypes, Object[] paramValues)
          Attempts to call a static factory method on a type.
static Class<?>[] classes(Class<?>... classes)
          Helper method to create an array of Classes.
static Class<?> forName(String className)
          Helper method to resolve a Class of a given name.
 Object instantiate(Class<?>[] paramTypes, Object[] paramValues)
          Attempts to instantiate the type.
static Object[] objects(Object... objects)
          Helper method to create an array of Objects.
 Class<?> resolve()
          Attempts to resolve the type.
 Object returnConstant(String constantName)
          Attempts to resolve a static constant on the type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConditionalInstantiator

public ConditionalInstantiator(String fullyQualifiedClassName)
Constructor.

Parameters:
fullyQualifiedClassName - The fully-qualified name of the class that we intend to instantiate.
Method Detail

resolve

public Class<?> resolve()
Attempts to resolve the type.

Returns:
The corresponding class object if the type exists; null otherwise.

instantiate

public Object instantiate(Class<?>[] paramTypes,
                          Object[] paramValues)
Attempts to instantiate the type.

Parameters:
paramTypes - The types of the constructor parameters of the constructor that we want to call.
paramValues - The values that we want to pass into the constructor.
Returns:
An instance of the type given in the constructor with the given parameter values, or null if the type does not exist.
Throws:
ReflectionException - If instantiation fails.

callFactory

public Object callFactory(String factoryMethod,
                          Class<?>[] paramTypes,
                          Object[] paramValues)
Attempts to call a static factory method on the type.

Parameters:
factoryMethod - The name of the factory method.
paramTypes - The types of the parameters of the specific overload of the factory method we want to call.
paramValues - The values that we want to pass into the factory method.
Returns:
An instance of the type given by the factory method with the given parameter values, or null of the type does not exist.
Throws:
ReflectionException - If the call to the factory method fails.

callFactory

public Object callFactory(String factoryTypeName,
                          String factoryMethod,
                          Class<?>[] paramTypes,
                          Object[] paramValues)
Attempts to call a static factory method on a type.

Parameters:
factoryTypeName - The type that contains the factory method.
factoryMethod - The name of the factory method.
paramTypes - The types of the parameters of the specific overload of the factory method we want to call.
paramValues - The values that we want to pass into the factory method.
Returns:
An instance of the type given by the factory method with the given parameter values, or null of the type does not exist.
Throws:
ReflectionException - If the call to the factory method fails.

returnConstant

public Object returnConstant(String constantName)
Attempts to resolve a static constant on the type.

Parameters:
constantName - The name of the constant.
Returns:
The value of the constant, or null if the type does not exist.
Throws:
ReflectionException - If resolving the constant fails.

classes

public static Class<?>[] classes(Class<?>... classes)
Helper method to create an array of Classes.

Parameters:
classes - The classes to construct an array out of.
Returns:
An array with the given classes.

objects

public static Object[] objects(Object... objects)
Helper method to create an array of Objects.

Parameters:
objects - The objects to construct an array out of.
Returns:
An array with the given objects.

forName

public static Class<?> forName(String className)
Helper method to resolve a Class of a given name.

Parameters:
className - The fully qualified name of the class to resolve.
Returns:
The corresponding class if it exists, null otherwise.


Copyright © 2015. All Rights Reserved.