New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New macro with-class-lookup-disambiguated #57

Closed
wants to merge 1 commit into
base: master
from
Jump to file or symbol
Failed to load files and symbols.
+29 −2
Diff settings

Always

Just for now

New macro e.g. (with-class-lookup-disambiguated (lang.object) (find-j…

…ava-class 'object)) -> success (otherwise error: ambiguous)
  • Loading branch information...
alanruttenberg committed Jun 15, 2017
commit 5103d57822691d74c66b80c754f8438df6806bba
View
@@ -245,11 +245,29 @@ want to avoid the overhead of the dynamic dispatch."
(with-constant-signature ,(cdr fname-jname-pairs)
,@body)))))))
(defvar *class-lookup-overrides*)
(defmacro with-class-lookup-disambiguated (overrides &body body)
"Suppose you have code that references class using the symbol 'object, and this is ambiguous. E.g. in my system java.lang.Object, org.omg.CORBA.Object. Use (with-class-lookup-disambiguated (lang.object) ...). Within dynamic scope, find-java-class first sees if any of these match, and if so uses them to lookup the class."
`(let ((*class-lookup-overrides* ',overrides))
,@body))
(defun maybe-found-in-overridden (name)
(when (boundp '*class-lookup-overrides*)
(let ((found (find-if (lambda(el) (#"matches" (string el) (concatenate 'string "(?i).*" (string name) "$")))
*class-lookup-overrides*)))
(if found
(let ((*class-lookup-overrides* nil))
(lookup-class-name found))))))
(defun lookup-class-name (name
&key
(table *class-name-to-full-case-insensitive*)
(muffle-warning nil)
(return-ambiguous nil))
(let ((overridden (maybe-found-in-overridden name)))
(when overridden (return-from lookup-class-name overridden)))
(setq name (string name))
(let* (;; cant (last-name-pattern (#"compile" '|java.util.regex.Pattern| ".*?([^.]*)$"))
;; reason: bootstrap - the class name would have to be looked up...
@@ -1,6 +1,6 @@
(in-package :cl-user)
(prove:plan 6)
(prove:plan 8)
(prove:is
(read-from-string "#\"{bar}.{foo}\"")
@@ -22,7 +22,16 @@
(prove:is (#"toString"
(find "size" (#"getMethods" (find-java-class "java.util.Collections$UnmodifiableMap"))
:test 'string-equal :key #"getName"))
(#"toString" (java::jmethod "java.util.Collections$UnmodifiableMap" "size" )))
(#"toString" (java::jmethod "java.util.Collections$UnmodifiableMap" "size" )))
(prove:is
(jss::with-class-lookup-disambiguated (lang.object) (find-java-class 'object))
(find-java-class 'java.lang.object))
;; Object is ambiguous in default java
(prove:is-error
(find-java-class 'object)
'simple-error)
;; test that optimized jss is much faster than unoptimized
(let ()
ProTip! Use n and p to navigate between commits in a pull request.