Rguns

check et uut

Jan 14th, 2020
70
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void handleRequest(MethodCallMessage message) throws IllegalAccessException, InvocationTargetException {
  2. Optional<Method> first =
  3. Arrays.stream(implementation.getClass().getDeclaredMethods())
  4. .filter(m -> m.getName().equals(message.getMethodName())).findFirst();
  5.  
  6. if (message.getParameters().size() != message.getParametersStartingWith("arg").size()) {
  7. throw new RuntimeException("Parameter name format wrong");
  8. }
  9.  
  10. if (!first.isPresent()) throw new RuntimeException("Couldn't find method");
  11.  
  12.  
  13. Method m = first.get();
  14. message.getParameters().keySet().forEach(System.out::println);
  15. int sizeOfMethocCallParams = (int) message.getParameters().keySet().stream()
  16. .map(k -> k.split("\\.")[0]).distinct().count();
  17. int sizeOfMethodParams = m.getParameterCount();
  18.  
  19. if (sizeOfMethocCallParams != sizeOfMethodParams) throw new RuntimeException("Parameter count don't match");
  20.  
  21. Object returnedObject = null;
  22. if (sizeOfMethodParams > 0) {
  23. Object[] newParameters = new Object[sizeOfMethodParams];
  24. for (int i = 0; i < sizeOfMethocCallParams; i++) {
  25. Map<String, String> parametersStartingWith = message.getParametersStartingWith("arg" + i);
  26. Object object = null;
  27. if (parametersStartingWith.size() > 1) {
  28. Class<?> returnType = m.getParameterTypes()[i];
  29. Field[] fields = returnType.getDeclaredFields();
  30. Object instance = null;
  31. try {
  32. System.out.println(returnType);
  33. instance = returnType.getDeclaredConstructor().newInstance();
  34. } catch (InstantiationException | NoSuchMethodException e) {
  35. e.printStackTrace();
  36. }
  37. for (Map.Entry<String, String> stringStringEntry : parametersStartingWith.entrySet()) {
  38. String paramName = stringStringEntry.getKey().split("\\.")[1];
  39. System.out.println(paramName);
  40. Arrays.stream(fields).forEach(System.out::println);
  41. Optional<Field> field = Arrays.stream(fields).filter(f -> f.getName().equals(paramName)).findFirst();
  42. if (field.isPresent()) {
  43. System.out.println("present");
  44. Field field1 = field.get();
  45. field1.setAccessible(true);
  46. field1.set(instance, parseToCorretObject(field1.getType(), stringStringEntry.getValue()));
  47. }
  48.  
  49. }
  50. object = instance;
  51. } else {
  52. object = parseToCorretObject(m.getParameterTypes()[i], parametersStartingWith.get("arg" + i));
  53. }
  54. newParameters[i] = object;
  55. }
  56. returnedObject = m.invoke(implementation, newParameters);
  57. } else {
  58. returnedObject = m.invoke(implementation);
  59. System.out.println("Invoked method: " + m);
  60. }
  61.  
  62. System.out.println(returnedObject);
  63. if (returnedObject != null) {
  64. MethodCallMessage methodCallMessage = new MethodCallMessage(messageManager.getMyAddress(), "result");
  65. if (parameterTypes.contains(returnedObject.getClass())) {
  66. methodCallMessage.setParameter("result", returnedObject.toString());
  67. } else {
  68. Field[] declaredFields = returnedObject.getClass().getDeclaredFields();
  69. for (Field field : declaredFields) {
  70. field.setAccessible(true);
  71. methodCallMessage.setParameter("result." + field.getName(), field.get(returnedObject).toString());
  72. }
  73. }
  74. messageManager.send(methodCallMessage, message.getOriginator());
  75. } else {
  76. sendEmptyReply(message.getOriginator());
  77. }
  78. }
RAW Paste Data