com.github.jlangch.venice.java.venice Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of venice Show documentation
Show all versions of venice Show documentation
Venice, a sandboxed Lisp implemented in Java.
;;;; __ __ _
;;;; \ \ / /__ _ __ (_) ___ ___
;;;; \ \/ / _ \ '_ \| |/ __/ _ \
;;;; \ / __/ | | | | (_| __/
;;;; \/ \___|_| |_|_|\___\___|
;;;;
;;;;
;;;; Copyright 2017-2024 Venice
;;;;
;;;; Licensed under the Apache License, Version 2.0 (the "License");
;;;; you may not use this file except in compliance with the License.
;;;; You may obtain a copy of the License at
;;;;
;;;; http://www.apache.org/licenses/LICENSE-2.0
;;;;
;;;; Unless required by applicable law or agreed to in writing, software
;;;; distributed under the License is distributed on an "AS IS" BASIS,
;;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;;; See the License for the specific language governing permissions and
;;;; limitations under the License.
;;;; Venice Java functions
(ns java)
(defn
^{ :arglists '("(javadoc-url class)")
:doc "Returns the javadoc URL for a Java class."
:examples '("(java/javadoc-url (class :java.lang.String))") }
javadoc-url [class]
(let [module (fn [] (module-name class))
cname (str (str/replace-all (class-name class) "." "/") ".html")]
(case (java-major-version)
8 "http://docs.oracle.com/javase/8/docs/api/~{cname}"
9 "http://docs.oracle.com/javase/9/docs/api/~{cname}"
10 "http://docs.oracle.com/javase/10/docs/api/~{cname}"
11 "https://docs.oracle.com/en/java/javase/11/docs/api/~(module)/~{cname}"
12 "https://docs.oracle.com/en/java/javase/12/docs/api/~(module)/~{cname}"
13 "https://docs.oracle.com/en/java/javase/13/docs/api/~(module)/~{cname}"
14 "https://docs.oracle.com/en/java/javase/14/docs/api/~(module)/~{cname}"
15 "https://docs.oracle.com/en/java/javase/15/docs/api/~(module)/~{cname}"
16 "https://docs.oracle.com/en/java/javase/16/docs/api/~(module)/~{cname}"
17 "https://docs.oracle.com/en/java/javase/17/docs/api/~(module)/~{cname}"
18 "https://docs.oracle.com/en/java/javase/18/docs/api/~(module)/~{cname}"
19 "https://docs.oracle.com/en/java/javase/19/docs/api/~(module)/~{cname}"
20 "https://docs.oracle.com/en/java/javase/20/docs/api/~(module)/~{cname}"
21 "https://docs.oracle.com/en/java/javase/21/docs/api/~(module)/~{cname}"
22 "https://docs.oracle.com/en/java/javase/22/docs/api/~(module)/~{cname}"
23 "https://docs.oracle.com/en/java/javase/23/docs/api/~(module)/~{cname}"
24 "https://docs.oracle.com/en/java/javase/24/docs/api/~(module)/~{cname}"
"http://docs.oracle.com/javase/8/docs/api/~{cname}")))
(defn
^{ :arglists '("(javadoc class-or-object)")
:doc "Opens a browser window displaying the javadoc for argument."
:examples '("(java/javadoc :java.lang.String)") }
javadoc [class-or-object]
(let [class (if (instance-of? :java.lang.Class class-or-object)
class-or-object
(class class-or-object))]
(if-let [url (javadoc-url class)]
(sh/open url)
(println "Could not find Javadoc for" c))))
(defn
^{ :arglists '("(describe obj)")
:doc "Describes a Java class."
:examples '("(java/describe :java.util.ArrayList)") }
describe [obj]
(let [info (describe-class obj)
ctors (:constructors info)
methods (:methods info)
methods-instance (filter #(false? (:static %)) (:methods info))
methods-static (filter #(true? (:static %)) (:methods info))
fields (:fields info)
fields-instance (filter #(false? (:static %)) (:fields info))
fields-static (filter #(true? (:static %)) (:fields info))]
(when (pos? (count ctors))
(println "[Constructors]")
(docoll (fn [c]
(println " constructor")
(when (:default c)
(println " default"))
(docoll (fn [[n t]]
(println " " (name n) "->" t))
(seq (:params c))))
ctors))
(when (pos? (count methods-instance))
(println)
(println "[Methods instance]")
(docoll (fn [m]
(println " " (name (:name m)))
(docoll (fn [[n t]]
(println " " (name n) "->" t))
(seq (:params m)))
(println " return ->" (:return m)))
methods-instance))
(when (pos? (count methods-static))
(println)
(println "[Methods static]")
(docoll (fn [m]
(println " " (name (:name m)))
(docoll (fn [[n t]]
(println " " (name n) "->" t))
(seq (:params m)))
(println " return ->" (:return m)))
methods-static))
(when (pos? (count fields-instance))
(println)
(println "[Fields instance]")
(docoll #(println " " (name (:name %)) "->" (:type %)) fields-instance))
(when (pos? (count fields-static))
(println)
(println "[Fields static]")
(docoll #(println " " (:name %) "->" (:type %)) fields-static))))
(defn
^{ :arglists '("(describe-bean obj)")
:doc "Describes a Java bean."
:examples '("(java/describe-bean :java.util.String)") }
describe-bean [obj]
(let [info (describe-class obj)
ctors (:constructors info)
bean (:bean info)
getters (filter #(true? (:getter %)) bean)
setters (filter #(true? (:setter %)) bean)]
(when (pos? (count ctors))
(println "[Constructors]")
(docoll (fn [c]
(println " constructor")
(when (:default c)
(println " default"))
(docoll (fn [[n t]]
(println " " (name n) "->" t))
(seq (:params c))))
ctors))
(when (pos? (count getters))
(println)
(println "[Getters]")
(docoll #(println " " (name (:property %)) "->" (:type %)) getters))
(when (pos? (count setters))
(println)
(println "[Setters]")
(docoll #(println " " (name (:property %)) "->" (:type %)) setters))))
(defn
^{ :arglists '("(as-runnable f)")
:doc
"Wraps the function f in a [java.lang.Runnable](https://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static void testRunnable(final Runnable r) {
;; r.run();
;; }
(defn op [] (println "running"))
(. :FunctionalInterfaces :testRunnable (j/as-runnable op)))
""")
:see-also '(
"java/as-callable" "java/as-predicate" "java/as-function"
"java/as-consumer" "java/as-supplier") }
as-runnable [f] (proxify :java.lang.Runnable { :run f }))
(defn
^{ :arglists '("(as-callable f)")
:doc
"Wraps the function f in a [java.util.concurrent.Callable](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Callable.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testCallable(Callable c) throws Exception {
;; return c.call();
;; }
(defn op [] 4)
(. :FunctionalInterfaces :testCallable (j/as-callable op)))
""")
:see-also '(
"java/as-runnable" "java/as-predicate" "java/as-function"
"java/as-consumer" "java/as-supplier") }
as-callable [f] (proxify :java.util.concurrent.Callable { :call f }))
(defn
^{ :arglists '("(as-predicate f)")
:doc
"Wraps the function f in a [java.util.function.Predicate](https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static boolean testPredicate(Predicate p, Long t) {
;; return p.test(t);
;; }
(defn op [t] (pos? t))
(. :FunctionalInterfaces :testPredicate (j/as-predicate op) 4))
""")
:see-also '(
"java/as-runnable" "java/as-callable" "java/as-function"
"java/as-consumer" "java/as-supplier") }
as-predicate [f] (proxify :java.util.function.Predicate { :test f }))
(defn
^{ :arglists '("(as-function f)")
:doc
"Wraps the function f in a [java.util.function.Function](https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testFunction(Function f, Long t) {
;; return f.apply(t);
;; }
(defn op [t] (+ t 1))
(. :FunctionalInterfaces :testFunction (j/as-function op) 4))
""")
:see-also '(
"java/as-runnable" "java/as-callable" "java/as-predicate"
"java/as-consumer" "java/as-supplier") }
as-function [f] (proxify :java.util.function.Function { :apply f }))
(defn
^{ :arglists '("(as-consumer f)")
:doc
"Wraps the function f in a [java.util.function.Consumer](https://docs.oracle.com/javase/8/docs/api/java/util/function/Consumer.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static void testConsumer(Consumer f, Long t) {
;; f.accept(t);
;; }
(defn op [t] (println "consumed" t))
(. :FunctionalInterfaces :testConsumer (j/as-consumer op) 4))
""")
:see-also '(
"java/as-runnable" "java/as-callable" "java/as-predicate"
"java/as-function" "java/as-supplier") }
as-consumer [f] (proxify :java.util.function.Consumer { :accept f }))
(defn
^{ :arglists '("(as-supplier f)")
:doc
"Wraps the function f in a [java.util.function.Supplier](https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testSupplier(Supplier f) {
;; return f.get();
;; }
(defn op [] 5)
(. :FunctionalInterfaces :testSupplier (j/as-supplier op)))
""")
:see-also '(
"java/as-runnable" "java/as-callable" "java/as-predicate"
"java/as-function" "java/as-consumer") }
as-supplier [f] (proxify :java.util.function.Supplier { :get f }))
(defn
^{ :arglists '("(as-bipredicate f)")
:doc
"Wraps the function f in a [java.util.function.BiPredicate](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiPredicate.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static boolean testBiPredicate(BiPredicate f, Long t, Long u) {
;; return f.test(t,u);
;; }
(defn op [t u] (> t u))
(. :FunctionalInterfaces :testBiPredicate (j/as-bipredicate op) 1 2))
""")
:see-also '(
"java/as-bifunction" "java/as-biconsumer"
"java/as-unaryoperator" "java/as-binaryoperator") }
as-bipredicate [f] (proxify :java.util.function.BiPredicate { :test f }))
(defn
^{ :arglists '("(as-bifunction f)")
:doc
"Wraps the function f in a [java.util.function.BiFunction](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testBiFunction(BiFunction f, Long t, Long u) {
;; return f.apply(t,u);
;; }
(defn op [t u] (+ t u))
(. :FunctionalInterfaces :testBiFunction (j/as-bifunction op) 1 2))
""")
:see-also '(
"java/as-bipredicate" "java/as-biconsumer"
"java/as-unaryoperator" "java/as-binaryoperator") }
as-bifunction [f] (proxify :java.util.function.BiFunction { :apply f }))
(defn
^{ :arglists '("(as-biconsumer f)")
:doc
"Wraps the function f in a [java.util.function.BiConsumer](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static void testBiConsumer(BiConsumer f, Long t, Long u) {
;; f.accept(t,u);
;; }
(defn op [t u] (println "consumed" t u))
(. :FunctionalInterfaces :testBiConsumer (j/as-biconsumer op) 1 2))
""")
:see-also '(
"java/as-bipredicate" "java/as-bifunction"
"java/as-unaryoperator" "java/as-binaryoperator") }
as-biconsumer [f] (proxify :java.util.function.BiConsumer { :accept f }))
(defn
^{ :arglists '("(as-unaryoperator f)")
:doc
"Wraps the function f in a [java.util.function.UnnaryOperator](https://docs.oracle.com/javase/8/docs/api/java/util/function/UnaryOperator.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testUnaryOperator(UnaryOperator f, Long t) {
;; return f.apply(t);
;; }
(defn op [t] (+ t 1))
(. :FunctionalInterfaces :testUnaryOperator (j/as-unaryoperator op) 1))
""")
:see-also '(
"java/as-bipredicate" "java/as-bifunction" "java/as-biconsumer"
"java/as-binaryoperator") }
as-unaryoperator [f] (proxify :java.util.function.UnaryOperator { :apply f }))
(defn
^{ :arglists '("(as-binaryoperator f)")
:doc
"Wraps the function f in a [java.util.function.BinaryOperator](https://docs.oracle.com/javase/8/docs/api/java/util/function/BinaryOperator.html)"
:examples '(
"""
(do
(load-module :java ['java :as 'j])
(import :com.github.jlangch.venice.demo.FunctionalInterfaces)
;; public static Long testBinaryOperator(BinaryOperator f, Long t, Long u) {
;; return f.apply(t,u);
;; }
(defn op [t u] (+ t u))
(. :FunctionalInterfaces :testBinaryOperator (j/as-binaryoperator op) 1 2))
""")
:see-also '(
"java/as-bipredicate" "java/as-bifunction" "java/as-biconsumer"
"java/as-unaryoperator") }
as-binaryoperator [f] (proxify :java.util.function.BinaryOperator { :apply f }))