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.

ReadExecutor.java 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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.server.ServerApplication;
  4. import java.lang.reflect.Field;
  5. import java.lang.reflect.Method;
  6. /**
  7. * Created by robin on 9/16/16.
  8. */
  9. public class ReadExecutor extends AbstractCommandExecutor {
  10. @Override
  11. public String getCommandName() {
  12. return "lecture";
  13. }
  14. @Override
  15. public String run(Command command, ServerApplication server) throws Exception {
  16. String id = command.getArgument(0, false);
  17. String fieldName = command.getArgument(1, false);
  18. String methodName = String.format("get%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
  19. Object obj = server.getObject(id);
  20. Class objClass = obj.getClass();
  21. Field field = objClass.getDeclaredField(fieldName);
  22. Object result;
  23. String stringResult;
  24. try {
  25. result = field.get(obj);
  26. } catch (IllegalAccessException e) {
  27. Method method = objClass.getDeclaredMethod(methodName);
  28. result = method.invoke(obj);
  29. }
  30. if (result != null) {
  31. stringResult = result.toString();
  32. }
  33. else {
  34. stringResult = "NULL";
  35. }
  36. return stringResult;
  37. }
  38. }