|
@@ -0,0 +1,61 @@
|
|
1
|
+package com.uqac.rthoni.java_rmi.server.executors;
|
|
2
|
+
|
|
3
|
+import com.uqac.rthoni.java_rmi.common.Command;
|
|
4
|
+import com.uqac.rthoni.java_rmi.server.ServerApplication;
|
|
5
|
+
|
|
6
|
+import java.lang.reflect.Field;
|
|
7
|
+import java.lang.reflect.Method;
|
|
8
|
+
|
|
9
|
+/**
|
|
10
|
+ * Created by robin on 9/16/16.
|
|
11
|
+ */
|
|
12
|
+public class WriteExecutor extends AbstractCommandExecutor {
|
|
13
|
+ @Override
|
|
14
|
+ public String getCommandName() {
|
|
15
|
+ return "ecriture";
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ @Override
|
|
19
|
+ public String run(Command command, ServerApplication server) throws Exception {
|
|
20
|
+ String id = command.getArgument(0);
|
|
21
|
+ String fieldName = command.getArgument(1);
|
|
22
|
+ String methodName = String.format("set%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
|
|
23
|
+ String value = command.getArgument(2);
|
|
24
|
+
|
|
25
|
+ Object obj = server.getObject(id);
|
|
26
|
+ Class objClass = obj.getClass();
|
|
27
|
+ Field field = objClass.getDeclaredField(fieldName);
|
|
28
|
+
|
|
29
|
+ try {
|
|
30
|
+ field.set(obj, value);
|
|
31
|
+ } catch (IllegalAccessException e) {
|
|
32
|
+ try {
|
|
33
|
+ Method method = objClass.getDeclaredMethod(methodName, String.class);
|
|
34
|
+ method.invoke(obj, value);
|
|
35
|
+ }
|
|
36
|
+ catch (NoSuchMethodException e2) {
|
|
37
|
+ try {
|
|
38
|
+ Method method = objClass.getDeclaredMethod(methodName, int.class);
|
|
39
|
+ method.invoke(obj, Integer.parseInt(value));
|
|
40
|
+ }
|
|
41
|
+ catch (NoSuchMethodException e3) {
|
|
42
|
+ try {
|
|
43
|
+ Method method = objClass.getDeclaredMethod(methodName, float.class);
|
|
44
|
+ method.invoke(obj, Float.parseFloat(value));
|
|
45
|
+ }
|
|
46
|
+ catch (NoSuchMethodException e4) {
|
|
47
|
+ try {
|
|
48
|
+ Method method = objClass.getDeclaredMethod(methodName, boolean.class);
|
|
49
|
+ method.invoke(obj, Boolean.parseBoolean(value));
|
|
50
|
+ }
|
|
51
|
+ catch (NoSuchMethodException e5) {
|
|
52
|
+ throw new Exception("No such setter");
|
|
53
|
+ }
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+ }
|
|
57
|
+ }
|
|
58
|
+
|
|
59
|
+ return null;
|
|
60
|
+ }
|
|
61
|
+}
|