source: trunk/abcl/src/org/armedbear/lisp/classloader.lisp

Last change on this file was 15636, checked in by Mark Evenson, 15 months ago

java: provide api for getting and setting classloader

The WITH-CLASSLOADER macro will set the context classloader to that of
the provided CLASSLOADER, or use the default ABCL classloader if
specfied with no argument.

The CLASSLOADER function returns the default classloader or that
associated with a specified JAVA-OBJECT, or the default ABCL
classloader if no JAVA-OBJECT is given

The CONTEXT-CLASSLOADER function returns the context classloader
associated with a JAVA-THREAD, or the currently executing thread if no
JAVA-THREAD is specified. The CONTEXT-CLASSLOADER is a SETF'able
place.

File size: 1.5 KB
Line 
1;;; Copyright (C) 2023 Mark Evenson
2(in-package java)
3(export '(classloader
4          context-classloader
5          with-classloader))
6
7(defun classloader (&optional java-object)
8  "Without a specified JAVA-OBJECT, return the classloader of the current one
9
10Otherwise return the classloader of the specified JAVA-object."
11  (if java-object
12      (jcall "getClassLoader" java-object)
13      (get-default-classloader)))
14
15(defun context-classloader (&optional java-thread)
16  "Without a specified JAVA-THREAD, return the context classloader of the current thread
17
18Otherwise return the context classloader of specified JAVA-THREAD."
19  (jcall "getContextClassLoader"
20         (if java-thread
21             java-thread
22             (threads::get-java-thread))))
23
24(defsetf context-classloader (&optional java-thread) (classloader)
25  `(progn
26     (jcall "setContextClassLoader"
27            (if ,java-thread
28                ,java-thread
29                (threads::get-java-thread))
30            ,classloader)
31     ,classloader))
32
33(defmacro with-classloader (&optional (classloader) &body body)
34  "Call BODY with optional CLASSLOADER argument set as the context classloader
35
36If the CLASSLOADER is not specified, the default classloader is set as
37the context classloader."
38  `(let ((original-context (context-classloader)))
39     (prog2 
40         (setf (context-classloader)
41               ,(if classloader
42                    classloader
43                    (classloader)))
44         ,@body
45       (when original-context
46         (setf (context-classloader) original-context)))))
47           
Note: See TracBrowser for help on using the repository browser.