source: trunk/j/src/org/armedbear/lisp/java.lisp @ 5238

Last change on this file since 5238 was 5238, checked in by asimon, 17 years ago

(SETF JARRAY-REF)

File size: 5.1 KB
Line 
1;;; java.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: java.lisp,v 1.10 2003-12-20 22:40:02 asimon Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package "JAVA")
21
22(defun jregister-handler (object event handler &key data count)
23  (%jregister-handler object event handler data count))
24
25(defun jclass-name (class)
26  "Returns the name of CLASS as a Lisp string"
27  (jcall (jmethod "java.lang.Class" "getName") class))
28
29(defun jobject-class (obj)
30  "Returns the Java class that OBJ belongs to"
31  (jcall (jmethod "java.lang.Object" "getClass") obj))
32
33(defun ensure-jclass (class-or-string)
34  "If handed a string, return a Class object."
35  (if (stringp class-or-string)
36      (jclass class-or-string)
37      class-or-string))
38
39(defun jclass-superclass (class)
40  "Returns the superclass of CLASS, or NIL if it hasn't got one"
41  (jcall (jmethod "java.lang.Class" "getSuperclass") (ensure-jclass class)))
42
43(defun jclass-interfaces (class)
44  "Returns the vector of interfaces of CLASS"
45  (jcall (jmethod "java.lang.Class" "getInterfaces") (ensure-jclass class)))
46
47(defun jclass-interface-p (class)
48  "Returns T if CLASS is an interface"
49  (jcall (jmethod "java.lang.Class" "isInterface") (ensure-jclass class)))
50
51(defun jclass-superclass-p (class-1 class-2)
52  "Returns T if CLASS-1 is a superclass or interface of CLASS-2"
53  (jcall (jmethod "java.lang.Class" "isAssignableFrom" "java.lang.Class") 
54   (ensure-jclass class-1)
55   (ensure-jclass class-2)))
56
57(defun jclass-array-p (class)
58  "Returns T if CLASS is an array class"
59  (jcall (jmethod "java.lang.Class" "isArray") (ensure-jclass class)))
60
61(defun jarray-component-type (atype)
62  "Returns the component type of the array type ATYPE"
63  (assert (jclass-array-p atype))
64  (jcall (jmethod "java.lang.Class" "getComponentType") atype))
65
66(defun (setf jarray-ref) (new-value java-array &rest indices)
67  (apply #'jarray-set java-array new-value indices))
68
69(defun jclass-constructors (class)
70  "Returns a vector of constructors for CLASS"
71  (jcall (jmethod "java.lang.Class" "getConstructors") (ensure-jclass class)))
72
73(defun jconstructor-params (constructor)
74  "Returns a vector of parameter types (Java classes) for CONSTRUCTOR"
75  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
76
77(defun jclass-fields (class &key declared public)
78  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
79  (let* ((getter (if declared "getDeclaredFields" "getFields"))
80         (fields (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
81    (if public (delete-if-not #'jmember-public-p fields) fields)))
82
83(defun jfield-type (field)
84  "Returns the type (Java class) of FIELD"
85  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
86
87(defun jfield-name (field)
88  "Returns the name of FIELD as a Lisp string"
89  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
90
91(defun jclass-methods (class &key declared public)
92  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
93  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
94         (methods (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
95    (if public (delete-if-not #'jmember-public-p methods) methods)))
96
97(defun jmethod-params (method)
98  "Returns a vector of parameter types (Java classes) for METHOD"
99  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
100
101(defun jmethod-return-type (method)
102  "Returns the result type (Java class) of the METHOD"
103  (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
104
105(defun jmethod-name (method)
106  "Returns the name of METHOD as a Lisp string"
107  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
108
109(defun jinstance-of-p (obj class)
110  "OBJ is an instance of CLASS (or one of its subclasses)"
111  (and (java-object-p obj)
112       (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") (ensure-jclass class) obj)))
113
114(defun jmember-static-p (member)
115  "MEMBER is a static member of its declaring class"
116  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
117    "java.lang.reflect.Modifier"
118    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
119
120(defun jmember-public-p (member)
121  "MEMBER is a public member of its declaring class"
122  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
123    "java.lang.reflect.Modifier"
124    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
Note: See TracBrowser for help on using the repository browser.