Class SimpleFactory
public class SimpleFactory
extends java.lang.Object
- Author:
- Zachary Kurmas
-
Constructor Summary
Constructors Constructor Description SimpleFactory()
-
Method Summary
Modifier and Type Method Description static <T> T
make(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.lang.Object... params)
Instantiate an object of typename
and cast the new object to be of typeT
.static <T> T
make(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Object... params)
Callsmake(String, Class, Boolean, Object...)
with a default value offalse
forrethrowsRuntimeExceptions
static <T> T
makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.io.PrintStream error, java.lang.Integer exitValue, java.lang.Object... params)
CallsmakeOrQuit(String, Class, Boolean, java.io.PrintStream, Integer, Object...)
with default value offalse
forrethrowRuntimeExceptions
static <T> T
makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.io.PrintStream error, java.lang.Integer exitValue, java.lang.Object... params)
Try tomake(String, Class, Boolean, Object...)
a new object, or exit the program if the call tomake(String, Class, Boolean, Object...)
throws aDLException
.static <T> T
makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.lang.Object... params)
callsmakeOrQuit(String, Class, Boolean, java.io.PrintStream, Integer, Object...)
with the default error stream an exit value.static <T> T
makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Object... params)
callsmakeOrQuit(String, Class, java.io.PrintStream, Integer, Object...)
with the default error stream an exit value.
-
Constructor Details
-
SimpleFactory
public SimpleFactory()
-
-
Method Details
-
make
public static <T> T make(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.lang.Object... params) throws DLExceptionInstantiate an object of typename
and cast the new object to be of typeT
. This method's main purpose is to instantiate objects that are not known at compile time (e.g., plug-ins). The new object will be cast to some super-type,T
. (It can't be cast to its own type, because its own type isn't known at compile time.) Because of Java's type erasure, we need theClass
object ofT
as a parameter to verify that the requested object is actually of typeT
. (This check is not strictly necessary because if we don't have thisClass
object, and there is a type mismatch, Java will eventually generate aClassCastException
; however, we prefer to find any such problems as early as possible.)ExampleUsage:
PluginInterface plugin = SimpleFactory.make("MyPlugIn", PluginInterface.class);
The above code assumes that
PluginInterface
is a Java interface that is known at compile time, and thatMyPlugIn
is a class that implements thePluginInterface
interface.Two important limitations:
- This code does not search for the most specific match among several overload constructors. It will simply throw an exception if more than one constructor could be called given the types of the actual parameters, but no one constructor matches exactly.
- You cannot directly pass primitive data to the constructor. Any primitive data will be autoboxed. You
cannot pass
null
as a parameter to the constructor.
Note: It is assumed that the class being dynamically loaded is not in the package
edu.gvsu.kurmasz .warszawa.dl
. Therefore, it will automatically throw an exception if the user attempts to instantiate class withprivate
or package-protected protection, even though that would normally be allowed for classes in the same package asSimpleFactory
. Other "overly aggressive" exceptions may be thrown for similar reasons when attempting to load classes inedu.gvsu.kurmasz.warszawa.dl
.Normally, when a dynamically invoked constructor throws an exception, a
InvocationTargetException
is generated with the the exception generated by the constructor as its cause. WhenrethrowRuntimeExceptions
is set totrue
, anyRuntimeException
s generated by the constructor are simply re-thrown. This allows the calling method to simply check for exceptions as if the object were being created directly (i.e., usingnew
). This feature is limited toRuntimeExceptions
only because rethrowing anyException
in general would require this method to includethrows Exception
in its signature, and we don't want the user to have to explicitly check for a generalException
.- Type Parameters:
T
- A super-type of the object to be created- Parameters:
name
- The fully qualified name of the class to be instantiatedparentClass
- TheClass
object forT
(needed to verify that the cast fromObject
toT
is safe).rethrowRuntimeExceptions
- whentrue
, then this method re-throws anyRuntimeException
s that are the cause of anyInvocationTargetException
. Whenfalse
, allInvocationTargetExceptions
generate aDLException
(the same as any other exception). (See note above).params
- a list of parameters to the constructor- Returns:
- an instance of the desired object
- Throws:
DLException
- if anything goes wrong.java.lang.IllegalArgumentException
- ifname
orparentClass
arenull
-
make
public static <T> T make(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Object... params) throws DLExceptionCallsmake(String, Class, Boolean, Object...)
with a default value offalse
forrethrowsRuntimeExceptions
- Type Parameters:
T
- A super-type of the object to be created- Parameters:
name
- The fully qualified name of the class to be instantiatedparentClass
- TheClass
object forT
(needed to verify that the cast fromObject
toT
is safe).params
- a list of parameters to the constructor- Returns:
- an instance of the desired object
- Throws:
DLException
- if anything goes wrong.java.lang.IllegalArgumentException
- ifname
orparentClass
arenull
-
makeOrQuit
public static <T> T makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.io.PrintStream error, java.lang.Integer exitValue, java.lang.Object... params)Try tomake(String, Class, Boolean, Object...)
a new object, or exit the program if the call tomake(String, Class, Boolean, Object...)
throws aDLException
. (Note: This method only quits when it encounters aDLException
. It does not quit ifrethrowRuntimeException
istrue
and the constructor throws aRuntimeException
.- Type Parameters:
T
- A super-type of the object to be created- Parameters:
name
- The fully qualified name of the class to be instantiatedparentClass
- TheClass
object forT
(needed to verify that the cast fromObject
toT
is safe).rethrowRuntimeExceptions
- whentrue
, then this method re-throws anyRuntimeException
s that are the cause of anyInvocationTargetException
. Whenfalse
, allInvocationTargetExceptions
generate aDLException
(the same as any other exception). (See note above).error
- the stream to which to print any error messagesexitValue
- the value the process will return on exitparams
- a list of parameters to the constructor- Returns:
- the newly created object
-
makeOrQuit
public static <T> T makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.lang.Object... params)callsmakeOrQuit(String, Class, Boolean, java.io.PrintStream, Integer, Object...)
with the default error stream an exit value. -
makeOrQuit
public static <T> T makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.io.PrintStream error, java.lang.Integer exitValue, java.lang.Object... params)CallsmakeOrQuit(String, Class, Boolean, java.io.PrintStream, Integer, Object...)
with default value offalse
forrethrowRuntimeExceptions
-
makeOrQuit
public static <T> T makeOrQuit(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Object... params)callsmakeOrQuit(String, Class, java.io.PrintStream, Integer, Object...)
with the default error stream an exit value.
-