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

Last change on this file since 4947 was 4947, checked in by dmcnaught, 18 years ago

Added ENSURE-JCLASS so the user can pass a string or a class object

and things will still work

File size: 4.5 KB
Line 
1;;; java.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: java.lisp,v 1.8 2003-12-02 22:29:59 dmcnaught 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 jclass-for-name (name)
30  "Returns the class of name NAME"
31  (jstatic "forName" "java.lang.Class" name))
32
33(defun jobject-class (obj)
34  "Returns the Java class that OBJ belongs to"
35  (jcall (jmethod "java.lang.Object" "getClass") obj))
36
37(defun ensure-jclass (class-or-string)
38  "If handed a string, return a Class object."
39  (if (stringp class-or-string)
40      (jclass class-or-string)
41      class-or-string))
42
43(defun jclass-superclass (class)
44  "Returns the superclass of CLASS, or NIL if it hasn't got one"
45  (jcall (jmethod "java.lang.Class" "getSuperclass") (ensure-jclass class)))
46
47(defun jclass-interfaces (class)
48  "Returns the vector of interfaces of CLASS"
49  (jcall (jmethod "java.lang.Class" "getInterfaces") (ensure-jclass class)))
50
51(defun jclass-interface-p (class)
52  "Returns T if CLASS is an interface"
53  (jcall (jmethod "java.lang.Class" "isInterface") (ensure-jclass class)))
54
55(defun jclass-constructors (class)
56  "Returns a vector of constructors for CLASS"
57  (jcall (jmethod "java.lang.Class" "getConstructors") (ensure-jclass class)))
58
59(defun jconstructor-params (constructor)
60  "Returns a vector of parameter types (Java classes) for CONSTRUCTOR"
61  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
62
63(defun jclass-fields (class &key declared public)
64  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
65  (let* ((getter (if declared "getDeclaredFields" "getFields"))
66         (fields (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
67    (if public (delete-if-not #'jmember-public-p fields) fields)))
68
69(defun jfield-type (field)
70  "Returns the type (Java class) of FIELD"
71  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
72
73(defun jfield-name (field)
74  "Returns the name of FIELD as a Lisp string"
75  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
76
77(defun jclass-methods (class &key declared public)
78  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
79  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
80         (methods (jcall (jmethod "java.lang.Class" getter) (ensure-jclass class))))
81    (if public (delete-if-not #'jmember-public-p methods) methods)))
82
83(defun jmethod-params (method)
84  "Returns a vector of parameter types (Java classes) for METHOD"
85  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
86
87(defun jmethod-return-type (method)
88  "Returns the result type (Java class) of the METHOD"
89  (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
90
91(defun jmethod-name (method)
92  "Returns the name of METHOD as a Lisp string"
93  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
94
95(defun jinstance-of-p (obj class)
96  "OBJ is an instance of CLASS (or one of its subclasses)"
97  (and (java-object-p obj)
98       (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") (ensure-jclass class) obj)))
99
100(defun jmember-static-p (member)
101  "MEMBER is a static member of its declaring class"
102  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
103    "java.lang.reflect.Modifier"
104    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
105
106(defun jmember-public-p (member)
107  "MEMBER is a public member of its declaring class"
108  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
109    "java.lang.reflect.Modifier"
110    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
Note: See TracBrowser for help on using the repository browser.