Package edu.rit.util

Class Instance

java.lang.Object
edu.rit.util.Instance

public class Instance extends Object
Class Instance provides static methods for creating instances of classes.
Version:
09-Oct-2010
Author:
Alan Kaminsky
  • Method Details

    • newInstance

      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.
      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:
      IllegalArgumentException - (unchecked exception) Thrown if s does 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.