ReflectionCommand extends AbstractCommand { @Override public void execute(String commandName, BufferedReader reader, BufferedWriter writer) throws Py4JException, IOException { char subCommand = safeReadLine(reader).charAt(0); String returnCommand = null; if (subCommand == GET_UNKNOWN_SUB_COMMAND_NAME) { returnCommand = getUnknownMember(reader); } else if (subCommand == GET_JAVA_LANG_CLASS_SUB_COMMAND_NAME) { returnCommand = getJavaLangClass(reader); } else { returnCommand = getMember(reader); } logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } ReflectionCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); @Override void init(Gateway gateway, Py4JServerConnection connection); final static char GET_UNKNOWN_SUB_COMMAND_NAME; final static char GET_MEMBER_SUB_COMMAND_NAME; final static char GET_JAVA_LANG_CLASS_SUB_COMMAND_NAME; static final String REFLECTION_COMMAND_NAME; }
@Test public void testUnknown() { String inputCommand1 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "java" + "\nrj\ne\n"; String inputCommand2 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "java.lang" + "\nrj\ne\n"; String inputCommand3 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "java.lang.String" + "\nrj\ne\n"; String inputCommand4 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "p1.Cat" + "\nrj\ne\n"; String inputCommand5 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "byte" + "\nrj\ne\n"; String inputCommand6 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "System" + "\nrj\ne\n"; String inputCommand7 = ReflectionCommand.GET_UNKNOWN_SUB_COMMAND_NAME + "\n" + "File" + "\nrj\ne\n"; try { this.gateway.getDefaultJVMView().addSingleImport("java.util.List"); this.gateway.getDefaultJVMView().addStarImport("java.io.*"); command.execute("r", new BufferedReader(new StringReader(inputCommand1)), writer); assertEquals("!yp\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand2)), writer); assertEquals("!yp\n!yp\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand3)), writer); assertEquals("!yp\n!yp\n!ycjava.lang.String\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand4)), writer); assertEquals("!yp\n!yp\n!ycjava.lang.String\n!ycp1.Cat\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand5)), writer); assertEquals("!yp\n!yp\n!ycjava.lang.String\n!ycp1.Cat\n!ycbyte\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand6)), writer); assertEquals("!yp\n!yp\n!ycjava.lang.String\n!ycp1.Cat\n!ycbyte\n!ycjava.lang.System\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand7)), writer); assertEquals( "!yp\n!yp\n!ycjava.lang.String\n!ycp1" + ".Cat\n!ycbyte\n!ycjava.lang.System\n!ycjava.io.File\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testJavaLangClass() { String inputCommand1 = ReflectionCommand.GET_JAVA_LANG_CLASS_SUB_COMMAND_NAME + "\n" + "java.lang.String\ne\n"; String inputCommand2 = ReflectionCommand.GET_JAVA_LANG_CLASS_SUB_COMMAND_NAME + "\n" + "java.lang" + ".FOOOOO\ne\n"; try { command.execute("r", new BufferedReader(new StringReader(inputCommand1)), writer); assertEquals("!yro0\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand2)), writer); assertEquals("!yro0\n!xsThe class java.lang.FOOOOO does not exist.\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }
@Test public void testMember() { String inputCommand1 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "java.lang.String\n" + "valueOf" + "\ne\n"; String inputCommand2 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "java.lang.String\n" + "length" + "\ne\n"; String inputCommand3 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "p1.Cat\n" + "meow" + "\ne\n"; String inputCommand4 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "p1.Cat\n" + "meow20" + "\ne\n"; String inputCommand5 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "p1.Cat\n" + "meow15" + "\ne\n"; String inputCommand6 = ReflectionCommand.GET_MEMBER_SUB_COMMAND_NAME + "\n" + "p1.Cat\n" + "CONSTANT" + "\ne\n"; try { command.execute("r", new BufferedReader(new StringReader(inputCommand1)), writer); assertEquals("!ym\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand2)), writer); assertEquals("!ym\n!xsTrying to access a non-static member from a " + "static context.\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand3)), writer); assertEquals( "!ym\n!xsTrying to access a non-static member from a static context.\n!xsTrying to access a non-static member from a static context.\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand4)), writer); assertEquals( "!ym\n!xsTrying to access a non-static member from a static context.\n!xsTrying to access a non-static member from a static context.\n!x\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand5)), writer); assertEquals( "!ym\n!xsTrying to access a non-static member from a static context.\n!xsTrying to access a non-static member from a static context.\n!x\n!ym\n", sWriter.toString()); command.execute("r", new BufferedReader(new StringReader(inputCommand6)), writer); assertEquals( "!ym\n!xsTrying to access a non-static member from a static context.\n!xsTrying to access a non-static member from a static context.\n!x\n!ym\n!ysSalut!\n", sWriter.toString()); } catch (Exception e) { e.printStackTrace(); fail(); } }