Package edu.rit.util
Class Instance
java.lang.Object
edu.rit.util.Instance
Class Instance provides static methods for creating instances of classes.
- Version:
- 09-Oct-2010
- Author:
- Alan Kaminsky
-
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectCreate a new instance of a class as specified by the given string.
-
Method Details
-
newInstance
public static Object newInstance(String s) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException Create a new instance of a class as specified by the given string. The string must consist of a fully-qualified class name, a left parenthesis, zero or more comma-separated arguments, and a right parenthesis. No whitespace is allowed. This method attempts to find a constructor for the given class as follows, where N is the number of arguments:- If N = 0, use a no-argument constructor.
-
Else if all arguments are integers, use a constructor with N
arguments of type
int. -
Else if all arguments are integers and there is no such constructor, use
a constructor with one argument of type
int[]. -
Else if not all arguments are integers, use a constructor with N
arguments of type
String. -
Else if not all arguments are integers and there is no such constructor,
use a constructor with one argument of type
String[]. - Else throw a NoSuchMethodException.
- Parameters:
s- Constructor expression string.- Returns:
- New instance.
- Throws:
IllegalArgumentException- (unchecked exception) Thrown ifsdoes not obey the required syntax.ClassNotFoundException- Thrown if the given class cannot be found.NoSuchMethodException- Thrown if a suitable constructor cannot be found in the given class.InstantiationException- Thrown if an instance cannot be created because the given class is an interface or an abstract class.IllegalAccessException- Thrown if an instance cannot be created because the calling method does not have access to the given constructor.InvocationTargetException- Thrown if the given constructor throws an exception.ClassNotFoundException- if any.NoSuchMethodException- if any.InstantiationException- if any.IllegalAccessException- if any.InvocationTargetException- if any.
-