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

Last change on this file since 4953 was 4953, checked in by asimon, 18 years ago

removed JCLASS-FOR-NAME
added three more convenience functions for Java reflection

File size: 5.0 KB
Line 
1;;; java.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: java.lisp,v 1.9 2003-12-03 11:13:20 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 jclass-constructors (class)
67  "Returns a vector of constructors for CLASS"
68  (jcall (jmethod "java.lang.Class" "getConstructors") (ensure-jclass class)))
69
70(defun jconstructor-params (constructor)
71  "Returns a vector of parameter types (Java classes) for CONSTRUCTOR"
72  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
73
74(defun jclass-fields (class &key declared public)
75  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
76  (let* ((getter (if declared "getDeclaredFields" "getFields"))
77         (fields (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
78    (if public (delete-if-not #'jmember-public-p fields) fields)))
79
80(defun jfield-type (field)
81  "Returns the type (Java class) of FIELD"
82  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
83
84(defun jfield-name (field)
85  "Returns the name of FIELD as a Lisp string"
86  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
87
88(defun jclass-methods (class &key declared public)
89  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
90  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
91         (methods (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
92    (if public (delete-if-not #'jmember-public-p methods) methods)))
93
94(defun jmethod-params (method)
95  "Returns a vector of parameter types (Java classes) for METHOD"
96  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
97
98(defun jmethod-return-type (method)
99  "Returns the result type (Java class) of the METHOD"
100  (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
101
102(defun jmethod-name (method)
103  "Returns the name of METHOD as a Lisp string"
104  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
105
106(defun jinstance-of-p (obj class)
107  "OBJ is an instance of CLASS (or one of its subclasses)"
108  (and (java-object-p obj)
109       (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") (ensure-jclass class) obj)))
110
111(defun jmember-static-p (member)
112  "MEMBER is a static member of its declaring class"
113  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
114    "java.lang.reflect.Modifier"
115    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
116
117(defun jmember-public-p (member)
118  "MEMBER is a public member of its declaring class"
119  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
120    "java.lang.reflect.Modifier"
121    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
Note: See TracBrowser for help on using the repository browser.