Code example for Constructor

Methods: newInstance, setAccessible

0
  // HACK: Why is CommandLineReader a package private class? 
  public static Reader createCommandLineReader() {
    try { 
      Class<?> clazz = Class.forName("bsh.CommandLineReader");
      Constructor<?> constructor = clazz.getConstructor(Reader.class);
      constructor.setAccessible(true);
      return (Reader)constructor.newInstance(new InputStreamReader(System.in));
    } catch (Exception err) {
      throw new IllegalStateException("Exception", err);
    } 
  } 
}