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> Tmake(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Boolean rethrowRuntimeExceptions, java.lang.Object... params)Instantiate an object of typenameand cast the new object to be of typeT.static <T> Tmake(java.lang.String name, java.lang.Class<T> parentClass, java.lang.Object... params)Callsmake(String, Class, Boolean, Object...)with a default value offalseforrethrowsRuntimeExceptionsstatic <T> TmakeOrQuit(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 offalseforrethrowRuntimeExceptionsstatic <T> TmakeOrQuit(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> TmakeOrQuit(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> TmakeOrQuit(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 typenameand 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 theClassobject ofTas a parameter to verify that the requested object is actually of typeT. (This check is not strictly necessary because if we don't have thisClassobject, 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
PluginInterfaceis a Java interface that is known at compile time, and thatMyPlugInis a class that implements thePluginInterfaceinterface.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
nullas 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 withprivateor 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
InvocationTargetExceptionis generated with the the exception generated by the constructor as its cause. WhenrethrowRuntimeExceptionsis set totrue, anyRuntimeExceptions 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 toRuntimeExceptionsonly because rethrowing anyExceptionin general would require this method to includethrows Exceptionin 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- TheClassobject forT(needed to verify that the cast fromObjecttoTis safe).rethrowRuntimeExceptions- whentrue, then this method re-throws anyRuntimeExceptions that are the cause of anyInvocationTargetException. Whenfalse, allInvocationTargetExceptionsgenerate 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- ifnameorparentClassarenull
-
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 offalseforrethrowsRuntimeExceptions- Type Parameters:
T- A super-type of the object to be created- Parameters:
name- The fully qualified name of the class to be instantiatedparentClass- TheClassobject forT(needed to verify that the cast fromObjecttoTis 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- ifnameorparentClassarenull
-
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 ifrethrowRuntimeExceptionistrueand 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- TheClassobject forT(needed to verify that the cast fromObjecttoTis safe).rethrowRuntimeExceptions- whentrue, then this method re-throws anyRuntimeExceptions that are the cause of anyInvocationTargetException. Whenfalse, allInvocationTargetExceptionsgenerate 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 offalseforrethrowRuntimeExceptions -
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.
-