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