DirCommand extends AbstractCommand { @Override public void execute(String commandName, BufferedReader reader, BufferedWriter writer) throws Py4JException, IOException { String subCommand = safeReadLine(reader); boolean unknownSubCommand = false; String param = reader.readLine(); String returnCommand = null; try { final String[] names; if (subCommand.equals(DIR_FIELDS_SUBCOMMAND_NAME)) { Object targetObject = gateway.getObject(param); names = reflectionEngine.getPublicFieldNames(targetObject); } else if (subCommand.equals(DIR_METHODS_SUBCOMMAND_NAME)) { Object targetObject = gateway.getObject(param); names = reflectionEngine.getPublicMethodNames(targetObject); } else if (subCommand.equals(DIR_STATIC_SUBCOMMAND_NAME)) { Class<?> clazz = TypeUtil.forName(param); names = reflectionEngine.getPublicStaticNames(clazz); } else if (subCommand.equals(DIR_JVMVIEW_SUBCOMMAND_NAME)) { names = getJvmViewNames(param, reader); } else { names = null; unknownSubCommand = true; } reader.readLine(); if (unknownSubCommand) { returnCommand = Protocol.getOutputErrorCommand("Unknown Array SubCommand Name: " + subCommand); } else if (names == null) { ReturnObject returnObject = gateway.getReturnObject(null); returnCommand = Protocol.getOutputCommand(returnObject); } else { StringBuilder namesJoinedBuilder = new StringBuilder(); for (String name : names) { namesJoinedBuilder.append(name); namesJoinedBuilder.append("\n"); } final String namesJoined; if (namesJoinedBuilder.length() > 0) { namesJoined = namesJoinedBuilder.substring(0, namesJoinedBuilder.length() - 1); } else { namesJoined = ""; } ReturnObject returnObject = gateway.getReturnObject(namesJoined); returnCommand = Protocol.getOutputCommand(returnObject); } } catch (Exception e) { logger.log(Level.FINEST, "Error in a dir subcommand", e); returnCommand = Protocol.getOutputErrorCommand(); } logger.finest("Returning command: " + returnCommand); writer.write(returnCommand); writer.flush(); } DirCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); @Override void init(Gateway gateway, Py4JServerConnection connection); static final String DIR_COMMAND_NAME; static final String DIR_FIELDS_SUBCOMMAND_NAME; static final String DIR_METHODS_SUBCOMMAND_NAME; static final String DIR_STATIC_SUBCOMMAND_NAME; static final String DIR_JVMVIEW_SUBCOMMAND_NAME; }
@Test public void testDirFields() throws Exception { String inputCommand = "f\n" + target + "\ne\n"; assertTrue(gateway.getBindings().containsKey(target)); command.execute("d", new BufferedReader(new StringReader(inputCommand)), writer); Set<String> fields = convertResponse(sWriter.toString()); assertEquals(ExampleClassFields, fields); }
@Test public void testDirMethods() throws Exception { String inputCommand = "m\n" + target + "\ne\n"; assertTrue(gateway.getBindings().containsKey(target)); command.execute("d", new BufferedReader(new StringReader(inputCommand)), writer); Set<String> methods = convertResponse(sWriter.toString()); assertEquals(ExampleClassMethods, methods); }
@Test public void testDirStatics() throws Exception { String inputCommand = "s\n" + ExampleClass.class.getName() + "\ne\n"; assertTrue(gateway.getBindings().containsKey(target)); command.execute("d", new BufferedReader(new StringReader(inputCommand)), writer); Set<String> methods = convertResponse(sWriter.toString()); assertEquals(ExampleClassStatics, methods); }
@Test public void testDirJvmView() throws Exception { assertTrue(gateway.getBindings().containsKey(target)); command.execute("d", new BufferedReader(new StringReader(createDirJvmViewCommand(null))), writer); JvmViewRet result = convertResponseJvmView(sWriter.toString()); sWriter.getBuffer().setLength(0); assertEquals(new HashSet<String>(), result.names); gateway.getDefaultJVMView().addSingleImport("com.example.Class1"); gateway.getDefaultJVMView().addSingleImport("com.another.Class2"); command.execute("d", new BufferedReader(new StringReader(createDirJvmViewCommand(null))), writer); result = convertResponseJvmView(sWriter.toString()); sWriter.getBuffer().setLength(0); String sequenceID = result.sequenceId; assertEquals(new HashSet<String>(Arrays.asList("Class1", "Class2")), result.names); command.execute("d", new BufferedReader(new StringReader(createDirJvmViewCommand(sequenceID))), writer); result = convertResponseJvmView(sWriter.toString()); sWriter.getBuffer().setLength(0); assertNull(result); gateway.getDefaultJVMView().addSingleImport("com.third.Class3"); command.execute("d", new BufferedReader(new StringReader(createDirJvmViewCommand(sequenceID))), writer); result = convertResponseJvmView(sWriter.toString()); sWriter.getBuffer().setLength(0); assertEquals(new HashSet<String>(Arrays.asList("Class1", "Class2", "Class3")), result.names); }