JIMS: add the beans attitude 50/11950/2
Calixte DENIZET [Fri, 5 Jul 2013 10:46:50 +0000 (12:46 +0200)]
Change-Id: Ia30080bb591f52e61151147d2ca84bf42224a8f8

scilab/modules/external_objects_java/help/en_US/jgetfield.xml
scilab/modules/external_objects_java/help/en_US/jsetfield.xml
scilab/modules/external_objects_java/src/java/org/scilab/modules/external_objects_java/ScilabJavaObject.java

index 2a06923..b4f1a0f 100644 (file)
@@ -51,6 +51,9 @@
         <para>
             Get the value of the field named fieldName. If the optional third argument is true, then the value is automatically unwrapped.
         </para>
+        <para>
+            This function also works with beans properties which may not be mapped to a public java field.
+        </para>
     </refsection>
     <refsection>
         <title>Examples</title>
 
                jremove c t v;
       ]]></programlisting>
+        <programlisting role="example"><![CDATA[
+jimport java.util.Date;
+
+d = Date.new();
+d.minutes, // will call d.getMinutes()
+jgetfield(d, "minutes"), // will also call d.getMinutes()
+
+jremove Date d;
+      ]]></programlisting>
     </refsection>
     <refsection role="see also">
         <title>See Also</title>
index 46915f0..f2bdb78 100644 (file)
@@ -51,6 +51,9 @@
         <para>
             Set the field named fieldName to the given value.
         </para>
+        <para>
+            This function also works with beans properties which may not be mapped to a public java field.
+        </para>
     </refsection>
     <refsection>
         <title>Examples</title>
                junwrap(t.field)
                jremove c t;
       ]]></programlisting>
+        <programlisting role="example"><![CDATA[
+jimport java.util.Date;
+
+d = Date.new();
+d.minutes = 3; // will call d.setMinutes(3);
+jsetfield(d, "minutes", 3); // will call d.setMinutes(3);
+
+jremove Date d;
+      ]]></programlisting>
     </refsection>
     <refsection role="see also">
         <title>See Also</title>
index e40a66a..65befbf 100644 (file)
@@ -393,6 +393,14 @@ public class ScilabJavaObject {
                 f = arraySJO[id].clazz.getField(fieldName);
                 f.set(arraySJO[id].object, arraySJO[idarg].object);
             } catch (NoSuchFieldException e) {
+                if (!fieldName.isEmpty()) {
+                    String setter = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
+                    if (arraySJO[id].methods.containsKey(setter)) {
+                        arraySJO[id].methods.get(setter).invoke(arraySJO[id].object, returnType, new int[] {idarg});
+                        return;
+                    }
+                }
+
                 throw new ScilabJavaException("No field " + fieldName + " in object " + getClassName(id));
             } catch (IllegalArgumentException e) {
                 if (f != null && f.getType() == int.class && arraySJO[idarg].clazz == double.class
@@ -458,6 +466,13 @@ public class ScilabJavaObject {
 
                 return new ScilabJavaObject(f.get(arraySJO[id].object)).id;
             } catch (NoSuchFieldException e) {
+                if (!fieldName.isEmpty()) {
+                    String getter = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
+                    if (arraySJO[id].methods.containsKey(getter)) {
+                        return new ScilabJavaObject(arraySJO[id].methods.get(getter).invoke(arraySJO[id].object, returnType, new int[] {}), returnType[0]).id;
+                    }
+                }
+
                 throw new ScilabJavaException("No field " + fieldName + " in object " + getClassName(id));
             } catch (IllegalArgumentException e) {
                 throw new ScilabJavaException("Bad argument value for field " + fieldName + " in object " + getClassName(id));
@@ -494,9 +509,16 @@ public class ScilabJavaObject {
                         return -1;
                     }
                 }
+
                 arraySJO[id].clazz.getField(fieldName);
                 return 1;
             } catch (NoSuchFieldException e) {
+                if (!fieldName.isEmpty()) {
+                    String part = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
+                    if (arraySJO[id].methods.containsKey("set" + part) && arraySJO[id].methods.containsKey("get" + part)) {
+                        return 1;
+                    }
+                }
                 return -1;
             } catch (IllegalArgumentException e) {
                 return -1;