Changeset 4864


Ignore:
Timestamp:
11/21/03 22:17:36 (18 years ago)
Author:
asimon
Message:

More convenience functions for Java reflection.
JCLASS-FIELDS, JCLASS-METHODS accepts keyword arguments.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/j/src/org/armedbear/lisp/java.lisp

    r4859 r4864  
    2626  (jcall (jmethod "java.lang.Class" "getName") class))
    2727
     28(defun jclass-for-name (name)
     29  "Returns the class of name NAME"
     30  (jstatic "forName" "java.lang.Class" name))
     31
    2832(defun jobject-class (obj)
    2933  "Returns the Java class that OBJ belongs to"
     
    3842  (jcall (jmethod "java.lang.reflect.Constructor" "getParameterTypes") constructor))
    3943
    40 (defun jclass-fields (class)
    41   "Returns a vector of all accessible fields of CLASS (and its supeorclasses)"
    42   (jcall (jmethod "java.lang.Class" "getFields") class))
     44(defun jclass-fields (class &key declared public)
     45  "Returns a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) fields of CLASS"
     46  (let* ((getter (if declared "getDeclaredFields" "getFields"))
     47         (fields (jcall (jmethod "java.lang.Class" getter) class)))
     48    (if public (delete-if-not #'jmember-public-p fields) fields)))
     49
    4350
    4451(defun jfield-type (field)
     
    4653  (jcall (jmethod "java.lang.reflect.Field" "getType") field))
    4754
    48 (defun jclass-methods (class)
    49   "Return a vector of all accessible methods of CLASS (and its superclasses)"
    50   (jcall (jmethod "java.lang.Class" "getMethods") class))
     55(defun jfield-name (field)
     56  "Returns the name of FIELD as a Lisp string"
     57  (jcall (jmethod "java.lang.reflect.Field" "getName") field))
     58
     59(defun jclass-methods (class &key declared public)
     60  "Return a vector of all (or just the declared/public, if DECLARED/PUBLIC is true) methods of CLASS"
     61  (let* ((getter (if declared "getDeclaredMethods" "getMethods"))
     62         (methods (jcall (jmethod "java.lang.Class" getter) class)))
     63    (if public (delete-if-not #'jmember-public-p methods) methods)))
     64
     65
     66(defun jmethod-params (method)
     67  "Returns a vector of parameter types (Java classes) for METHOD"
     68  (jcall (jmethod "java.lang.reflect.Method" "getParameterTypes") method))
     69
     70(defun jmethod-return-type (method)
     71  "Returns the result type (Java class) of the METHOD"
     72  (jcall (jmethod "java.lang.reflect.Method" "getReturnType") method))
     73
     74(defun jmethod-name (method)
     75  "Returns the name of METHOD as a Lisp string"
     76  (jcall (jmethod "java.lang.reflect.Method" "getName") method))
    5177
    5278(defun jinstance-of-p (obj class)
    5379  "OBJ is an instance of CLASS (or one of its subclasses)"
    5480  (jcall (jmethod "java.lang.Class" "isInstance" "java.lang.Object") class obj))
     81
     82(defun jmember-static-p (member)
     83  "MEMBER is a static member of its declaring class"
     84  (jstatic (jmethod "java.lang.reflect.Modifier" "isStatic" "int")
     85    "java.lang.reflect.Modifier"
     86    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
     87
     88(defun jmember-public-p (member)
     89  "MEMBER is a public member of its declaring class"
     90  (jstatic (jmethod "java.lang.reflect.Modifier" "isPublic" "int")
     91    "java.lang.reflect.Modifier"
     92    (jcall (jmethod "java.lang.reflect.Member" "getModifiers") member)))
Note: See TracChangeset for help on using the changeset viewer.