1package com.beust.jcommander.internal;
2
3import com.beust.jcommander.ParameterException;
4
5import java.io.PrintWriter;
6import java.lang.reflect.Method;
7
8public class JDK6Console implements Console {
9
10  private Object console;
11
12  private PrintWriter writer;
13
14  public JDK6Console(Object console) throws Exception {
15    this.console = console;
16    Method writerMethod = console.getClass().getDeclaredMethod("writer", new Class<?>[0]);
17    writer = (PrintWriter) writerMethod.invoke(console, new Object[0]);
18  }
19
20  public void print(String msg) {
21    writer.print(msg);
22  }
23
24  public void println(String msg) {
25    writer.println(msg);
26  }
27
28  public char[] readPassword(boolean echoInput) {
29    try {
30      writer.flush();
31      Method method;
32      if (echoInput) {
33          method = console.getClass().getDeclaredMethod("readLine", new Class<?>[0]);
34          return ((String) method.invoke(console, new Object[0])).toCharArray();
35      } else {
36          method = console.getClass().getDeclaredMethod("readPassword", new Class<?>[0]);
37          return (char[]) method.invoke(console, new Object[0]);
38      }
39    }
40    catch (Exception e) {
41      throw new ParameterException(e);
42    }
43  }
44
45}