Kaynağa Gözat

added write executor

develop
Robin Thoni 8 yıl önce
ebeveyn
işleme
0c8400ef88

+ 10
- 0
server/src/main/java/com/uqac/rthoni/java_rmi/server/TestDbo.java Dosyayı Görüntüle

@@ -6,6 +6,8 @@ package com.uqac.rthoni.java_rmi.server;
6 6
 public class TestDbo {
7 7
     private String privateString = "default_value";
8 8
 
9
+    private int privateInt = 24;
10
+
9 11
     public int publicInt = 42;
10 12
 
11 13
     public String getPrivateString() {
@@ -15,4 +17,12 @@ public class TestDbo {
15 17
     public void setPrivateString(String privateString) {
16 18
         this.privateString = privateString;
17 19
     }
20
+
21
+    public int getPrivateInt() {
22
+        return privateInt;
23
+    }
24
+
25
+    public void setPrivateInt(int privateInt) {
26
+        this.privateInt = privateInt;
27
+    }
18 28
 }

+ 61
- 0
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/WriteExecutor.java Dosyayı Görüntüle

@@ -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
+}

Loading…
İptal
Kaydet