diff options
Diffstat (limited to 'src/main/java/com/beust/jcommander/internal/JDK6Console.java')
-rw-r--r-- | src/main/java/com/beust/jcommander/internal/JDK6Console.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/main/java/com/beust/jcommander/internal/JDK6Console.java b/src/main/java/com/beust/jcommander/internal/JDK6Console.java new file mode 100644 index 0000000..70cb186 --- /dev/null +++ b/src/main/java/com/beust/jcommander/internal/JDK6Console.java @@ -0,0 +1,45 @@ +package com.beust.jcommander.internal; + +import com.beust.jcommander.ParameterException; + +import java.io.PrintWriter; +import java.lang.reflect.Method; + +public class JDK6Console implements Console { + + private Object console; + + private PrintWriter writer; + + public JDK6Console(Object console) throws Exception { + this.console = console; + Method writerMethod = console.getClass().getDeclaredMethod("writer", new Class<?>[0]); + writer = (PrintWriter) writerMethod.invoke(console, new Object[0]); + } + + public void print(String msg) { + writer.print(msg); + } + + public void println(String msg) { + writer.println(msg); + } + + public char[] readPassword(boolean echoInput) { + try { + writer.flush(); + Method method; + if (echoInput) { + method = console.getClass().getDeclaredMethod("readLine", new Class<?>[0]); + return ((String) method.invoke(console, new Object[0])).toCharArray(); + } else { + method = console.getClass().getDeclaredMethod("readPassword", new Class<?>[0]); + return (char[]) method.invoke(console, new Object[0]); + } + } + catch (Exception e) { + throw new ParameterException(e); + } + } + +}
\ No newline at end of file |