This is the mail archive of the
kawa@sources.redhat.com
mailing list for the Kawa project.
isProperty patch
- From: Chris Dean <ctdean at sokitomi dot com>
- To: Kawa List <kawa at sources dot redhat dot com>
- Date: Thu, 05 Aug 2004 23:21:20 -0700
- Subject: isProperty patch
Here's a patch that adds "isName" property detection, a commonly used
pattern in Java Beans for boolean properties. This logic in this code
is similar to the logic in java.beans.PropertyDescriptor which plays
fast and loose with with whole "boolean" part of boolean properties.
Regards,
Chris Dean
Index: doc/kawa.texi
===================================================================
RCS file: /cvs/kawa/kawa/doc/kawa.texi,v
retrieving revision 1.150
diff -u -w -r1.150 kawa.texi
--- doc/kawa.texi 28 Jul 2004 22:36:49 -0000 1.150
+++ doc/kawa.texi 6 Aug 2004 06:14:27 -0000
@@ -4101,7 +4101,9 @@
The field name is "mangled" (@pxref{Mangling}) into a valid Java name.
If there is no accessible field whose name is @code{"@var{fieldname}"},
-we look for a no-argument method whose name is @code{"get@var{Fieldname}"}.
+we look for a no-argument method whose name is
+@code{"get@var{Fieldname}"} (or @code{"get@var{Fieldname}"}" for a
+boolean property).
If @var{object} is a primitive Java array, then @var{fieldname} can only
be @code{'length}, and the result is the number of elements of the array.
Index: doc/ChangeLog
===================================================================
RCS file: /cvs/kawa/kawa/doc/ChangeLog,v
retrieving revision 1.122
diff -u -w -r1.122 ChangeLog
--- doc/ChangeLog 28 Jul 2004 22:36:50 -0000 1.122
+++ doc/ChangeLog 6 Aug 2004 06:16:36 -0000
@@ -1,3 +1,7 @@
+2004-08-05 Chris Dean <ctdean@sokitomi.com>
+
+ * kawa.texi (Field operations): Document "isName" fields.
+
2004-07-27 Chris Dean <ctdean@sokitomi.com>
* kawa.texi (Compilation options): Add --warn-as-error.
Index: gnu/kawa/reflect/ChangeLog
===================================================================
RCS file: /cvs/kawa/kawa/gnu/kawa/reflect/ChangeLog,v
retrieving revision 1.73
diff -u -w -r1.73 ChangeLog
--- gnu/kawa/reflect/ChangeLog 4 Aug 2004 01:36:47 -0000 1.73
+++ gnu/kawa/reflect/ChangeLog 6 Aug 2004 06:05:52 -0000
@@ -1,3 +1,8 @@
+2004-08-05 Chris Dean <ctdean@sokitomi.com>
+
+ * SlotGet.java (Inlineable): Check for "isFname" methods.
+ * SlotSet.java (Inlineable): Likewise.
+
2004-08-03 Per Bothner <per@bothner.com>
* TypeSwitch.java: Extend MethodProc instead of CpsProcedure.
Index: gnu/kawa/reflect/SlotGet.java
===================================================================
RCS file: /cvs/kawa/kawa/gnu/kawa/reflect/SlotGet.java,v
retrieving revision 1.22
diff -u -w -r1.22 SlotGet.java
--- gnu/kawa/reflect/SlotGet.java 11 Feb 2004 20:26:40 -0000 1.22
+++ gnu/kawa/reflect/SlotGet.java 6 Aug 2004 06:05:52 -0000
@@ -89,12 +89,20 @@
}
}
- // Try looking for a method "getFname" instead:
- String mname = ClassExp.slotToMethodName("get", fname);
+ // Try looking for a method "getFname" or "isFname" instead:
try
{
- java.lang.reflect.Method getmethod
- = clas.getMethod(mname, noClasses);
+ String mname = null;
+ java.lang.reflect.Method getmethod = null;
+
+ try {
+ mname = ClassExp.slotToMethodName("get", fname);
+ getmethod = clas.getMethod(mname, noClasses);
+ } catch (Exception getEx) {
+ mname = ClassExp.slotToMethodName("is", fname);
+ getmethod = clas.getMethod(mname, noClasses);
+ }
+
if (isStatic
&& (getmethod.getModifiers() & java.lang.reflect.Modifier.STATIC) == 0)
throw new RuntimeException("cannot call non-static getter method `"
Index: gnu/kawa/reflect/SlotSet.java
===================================================================
RCS file: /cvs/kawa/kawa/gnu/kawa/reflect/SlotSet.java,v
retrieving revision 1.15
diff -u -w -r1.15 SlotSet.java
--- gnu/kawa/reflect/SlotSet.java 11 Feb 2004 20:26:40 -0000 1.15
+++ gnu/kawa/reflect/SlotSet.java 6 Aug 2004 06:05:52 -0000
@@ -61,12 +61,19 @@
}
// Try looking for a method "setFname" instead.
- // First look for "getName", to get the "field type".
- String getName = ClassExp.slotToMethodName("get", name);
+ // First look for "getName" or "isName", to get the "field type".
try
{
- java.lang.reflect.Method getmethod
- = clas.getMethod(getName, SlotGet.noClasses);
+ java.lang.reflect.Method getmethod = null;
+
+ try {
+ String getName = ClassExp.slotToMethodName("get", name);
+ getmethod = clas.getMethod(getName, SlotGet.noClasses);
+ } catch (Exception getEx) {
+ String getName = ClassExp.slotToMethodName("is", name);
+ getmethod = clas.getMethod(getName, SlotGet.noClasses);
+ }
+
String setName = ClassExp.slotToMethodName("set", name);
Class[] setArgTypes = new Class[1];
setArgTypes[0] = getmethod.getReturnType();
@@ -117,10 +124,15 @@
if (field != null)
return field;
- // Try looking for a method "getFname" instead:
+ // Try looking for a method "getName" or "isName" instead:
String getName = ClassExp.slotToMethodName("get", name);
gnu.bytecode.Method method = clas.getMethod(getName, Type.typeArray0);
if (method == null)
+ {
+ getName = ClassExp.slotToMethodName("is", name);
+ method = clas.getMethod(getName, Type.typeArray0);
+ }
+ if (method == null)
return null;
Type ftype = method.getReturnType();
String setName = ClassExp.slotToMethodName("set", name);
Index: testsuite/ChangeLog
===================================================================
RCS file: /cvs/kawa/kawa/testsuite/ChangeLog,v
retrieving revision 1.145
diff -u -w -r1.145 ChangeLog
--- testsuite/ChangeLog 3 Aug 2004 18:16:05 -0000 1.145
+++ testsuite/ChangeLog 6 Aug 2004 06:05:52 -0000
@@ -1,3 +1,7 @@
+2004-08-05 Chris Dean <ctdean@sokitomi.com>
+
+ * obj-test.scm: Check for "isName" methods.
+
2004-08-03 Per Bothner <per@bothner.com>
* module2.scm: Make module extend <pair>, for extra testing.
Index: testsuite/classes1.scm
===================================================================
RCS file: /cvs/kawa/kawa/testsuite/classes1.scm,v
retrieving revision 1.9
diff -u -w -r1.9 classes1.scm
--- testsuite/classes1.scm 23 May 2004 20:03:46 -0000 1.9
+++ testsuite/classes1.scm 6 Aug 2004 06:05:52 -0000
@@ -8,6 +8,13 @@
(b :: <int> init-form: 6 allocation: class:)
(n22 :: <int> init-value: 22 allocation: 'static access: 'protected)
(hyphenated-field? init-value: "yes")
+
+ (mHappy init: #t)
+ ((isHappy) :: <boolean>
+ mHappy)
+ ((setHappy val :: <boolean>) :: <void>
+ (set! mHappy val))
+
((lambda-method1)
(lambda (x) (make-vector 1 x))) ; This lambda doesn't "capture" anything.
((lambda-method2 n)
@@ -59,3 +66,6 @@
(define-simple-class <IdClass2> (<IdClass1>)
(allocation: 'class init: (get-new-count))
(var2 init-form: (get-new-count)))
+
+
+
Index: testsuite/obj-test.scm
===================================================================
RCS file: /cvs/kawa/kawa/testsuite/obj-test.scm,v
retrieving revision 1.45
diff -u -w -r1.45 obj-test.scm
--- testsuite/obj-test.scm 23 May 2004 20:03:46 -0000 1.45
+++ testsuite/obj-test.scm 6 Aug 2004 06:05:52 -0000
@@ -1,4 +1,4 @@
-(test-init "Objects" 91)
+(test-init "Objects" 95)
;; Force procedure to be applied without being inlined:
(define-syntax force-eval
@@ -209,6 +209,14 @@
(slot-set! obj1 'a (+ 10 (static-field <SimpleA> 'b)))
(test "yes" slot-ref obj1 'hyphenated-field?)
(test 16 field obj1 'a)
+(slot-set! obj1 'happy #t)
+(test #t slot-ref obj1 'happy)
+(slot-set! obj1 'happy #f)
+(test #f slot-ref obj1 'happy)
+(force-compile slot-set! (as <SimpleA> obj1) 'happy #t)
+(test #t slot-ref (as <SimpleA> obj1) 'happy)
+(set! (field obj1 'happy) #f)
+(test #f field obj1 'happy)
(define obj2 (make <SimpleB>))
(test 4 field obj2 'a)