You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WriteExecutor.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package com.uqac.rthoni.java_rmi.server.executors;
  2. import com.uqac.rthoni.java_rmi.common.Command;
  3. import com.uqac.rthoni.java_rmi.common.ReflectionUtil;
  4. import com.uqac.rthoni.java_rmi.server.ServerApplication;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.Method;
  7. /**
  8. * Created by robin on 9/16/16.
  9. */
  10. public class WriteExecutor extends AbstractCommandExecutor {
  11. @Override
  12. public String getCommandName() {
  13. return "ecriture";
  14. }
  15. @Override
  16. public String run(Command command, ServerApplication server) throws Exception {
  17. String id = command.getArgument(0, false);
  18. String fieldName = command.getArgument(1, false);
  19. String methodName = String.format("set%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
  20. String value = command.getArgument(2, false);
  21. Object obj = server.getObject(id);
  22. Class objClass = obj.getClass();
  23. Field field = objClass.getDeclaredField(fieldName);
  24. Object typedValue = ReflectionUtil.toObject(field.getType(), value);
  25. try {
  26. field.set(obj, typedValue);
  27. } catch (IllegalAccessException e) {
  28. Method method = objClass.getDeclaredMethod(methodName, field.getType());
  29. method.invoke(obj, typedValue);
  30. }
  31. return null;
  32. }
  33. }