edu.rit.util
Class Instance
- java.lang.Object
-
- edu.rit.util.Instance
-
public class Instance extends java.lang.ObjectClass Instance provides static methods for creating instances of classes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static java.lang.ObjectnewInstance(java.lang.String s)Create a new instance of a class as specified by the given string.
-
-
-
Method Detail
-
newInstance
public static java.lang.Object newInstance(java.lang.String s) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetExceptionCreate 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.
This method invokes the chosen constructor, passing in the given argument values, and returns a reference to the newly-created instance.
Note: To find the given class, the calling thread's context class loader is used.
- Parameters:
s- Constructor expression string.- Returns:
- New instance.
- Throws:
java.lang.IllegalArgumentException- (unchecked exception) Thrown if s does not obey the required syntax.java.lang.ClassNotFoundException- Thrown if the given class cannot be found.java.lang.NoSuchMethodException- Thrown if a suitable constructor cannot be found in the given class.java.lang.InstantiationException- Thrown if an instance cannot be created because the given class is an interface or an abstract class.java.lang.IllegalAccessException- Thrown if an instance cannot be created because the calling method does not have access to the given constructor.java.lang.reflect.InvocationTargetException- Thrown if the given constructor throws an exception.
-
-
DMelt 3.0 © DataMelt by jWork.ORG