FieldCommand extends AbstractCommand { private String setField(BufferedReader reader) throws IOException { String targetObjectId = reader.readLine(); String fieldName = reader.readLine(); String value = reader.readLine(); reader.readLine(); Object valueObject = Protocol.getObject(value, this.gateway); Object object = gateway.getObject(targetObjectId); Field field = reflectionEngine.getField(object, fieldName); logger.finer("Setting field " + fieldName); String returnCommand = null; if (field == null) { returnCommand = Protocol.getNoSuchFieldOutputCommand(); } else { reflectionEngine.setFieldValue(object, field, valueObject); returnCommand = Protocol.getOutputVoidCommand(); } return returnCommand; } FieldCommand(); @Override void execute(String commandName, BufferedReader reader, BufferedWriter writer); @Override void init(Gateway gateway, Py4JServerConnection connection); final static String FIELD_COMMAND_NAME; final static String FIELD_GET_SUB_COMMAND_NAME; final static String FIELD_SET_SUB_COMMAND_NAME; }
@Test public void testSetField() { String inputCommand = "s\n" + target + "\nfield10\ni123\ne\n"; try { command.execute("f", new BufferedReader(new StringReader(inputCommand)), writer); assertEquals("!yv\n", sWriter.toString()); assertEquals(((ExampleClass) gateway.getObject(target)).field10, 123); } catch (Exception e) { e.printStackTrace(); fail(); } }