Back
proxy (clj)
(source)macro
(proxy class-and-interfaces args & fs)
class-and-interfaces - a vector of class names
args - a (possibly empty) vector of arguments to the superclass
constructor.
f => (name [params*] body) or
(name ([params*] body) ([params+] body) ...)
Expands to code which creates a instance of a proxy class that
implements the named class/interface(s) by calling the supplied
fns. A single class, if provided, must be first. If not provided it
defaults to Object.
The interfaces names must be valid interface types. If a method fn
is not provided for a class method, the superclass method will be
called. If a method fn is not provided for an interface method, an
UnsupportedOperationException will be thrown should it be
called. Method fns are closures and can capture the environment in
which proxy is called. Each method fn takes an additional implicit
first arg, which is bound to 'this. Note that while method fns can
be provided to override protected methods, they have no other access
to protected members, nor to super, as these capabilities cannot be
proxied.
Examples
clojure/core.typed
(ns ^:skip-wiki clojure.core.typed.ann.clojure
"Type annotations for the base Clojure distribution."
(:require [#?(:clj clojure.core.typed
:cljs cljs.core.typed)
:refer [defalias] :as t]))
(defalias
^{:doc "A Clojure proxy."
:forms '[Proxy]}
t/Proxy
clojure.lang.IProxy)
PrecursorApp/precursor
(ns pc.http.routes.api
(:require [cemerick.url :as url]
[cheshire.core :as json]
[clojure.core.memoize :as memo]
[clojure.string :as str]
[clojure.tools.reader.edn :as edn]
[crypto.equality :as crypto]
[defpage.core :as defpage :refer (defpage)]
[pc.auth :as auth]
[pc.crm :as crm]
[pc.datomic :as pcd]
[pc.early-access]
[pc.http.doc :as doc-http]
[pc.http.team :as team-http]
[pc.http.handlers.custom-domain :as custom-domain]
[pc.models.chat-bot :as chat-bot-model]
[pc.models.doc :as doc-model]
[pc.models.flag :as flag-model]
[pc.models.team :as team-model]
[pc.profile :as profile]
[ring.middleware.anti-forgery :as csrf]
[slingshot.slingshot :refer (try+ throw+)]))
;; proxy for the dribbble API, to be used by our dribbble cards on the blog
(defpage dribbble-profile "/api/v1/dribbble/users/:username" [req]
(let [username (get-in req [:params :username])]
(if (contains? dribbble-user-whitelist username)
{:body (json/encode (get-dribbble-profile username))
:status 200
:headers {"Content-Type" "application/json; charset=utf-8"}}
(throw+ {:status 400
:public-message "Sorry, this user isn't on the whitelist."}))))
hraberg/deuce
(ns deuce.emacs.alloc
(:use [deuce.emacs-lisp :only (defun defvar) :as el]
[taoensso.timbre :as timbre
:only (trace debug info warn error fatal spy)])
(:require [clojure.core :as c]
[clojure.walk :as w]
[deuce.emacs-lisp.cons :as cons])
(:refer-clojure :exclude [vector cons list])
(:import [java.util Arrays]
[java.lang.management ManagementFactory MemoryNotificationInfo MemoryType MemoryPoolMXBean]
[javax.management NotificationListener NotificationEmitter Notification]))
;; From http://www.javaspecialists.eu/archive/Issue092.html
(let [^MemoryPoolMXBean tenured-gen-pool (->> (ManagementFactory/getMemoryPoolMXBeans)
(filter (fn [^MemoryPoolMXBean mb]
(and (= (.getType mb) MemoryType/HEAP) (.isUsageThresholdSupported mb))))
first)
warning-level 0.8]
(.setUsageThreshold tenured-gen-pool
(long (* warning-level (.getMax (.getUsage tenured-gen-pool)))))
(.addNotificationListener ^NotificationEmitter (ManagementFactory/getMemoryMXBean)
(proxy [NotificationListener] []
(handleNotification [^Notification n hb]
(when (= (.getType n) MemoryNotificationInfo/MEMORY_THRESHOLD_EXCEEDED)
(el/setq memory-full true)))) nil nil))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
(t/defalias
^{:doc "A Clojure proxy."
:forms '[Proxy]}
t/Proxy
clojure.lang.IProxy)
cc/method-sig [java.lang.reflect.Method :-> '[t/Any t/AnyNilableNonEmptySeq t/Any]]
cc/proxy-name [Class (t/Seqable Class) :-> t/Str]
cc/get-proxy-class [Class :* :-> Class]
cc/construct-proxy [Class t/Any :* :-> t/Any]
cc/init-proxy [t/Proxy (t/Map t/Str t/Any) :-> t/Proxy]
cc/update-proxy [t/Proxy (t/Map t/Str t/Any) :-> t/Proxy]
cc/proxy-mappings [t/Proxy :-> (t/Map t/Str t/Any)]
cc/proxy-call-with-super (t/All [x] [[:-> x] t/Proxy t/Str :-> x])
cc/bean [Object :-> (t/Map t/Any t/Any)]
])
wizardpb/functional-vaadin
(ns functional-vaadin.rx.operators-test
(:require [clojure.test :refer :all]
[rx.lang.clojure.core :as rx]
[functional-vaadin.core :refer :all]
[functional-vaadin.rx.observers :refer :all]
[functional-vaadin.rx.operators :refer :all]
[functional-vaadin.utils :refer :all]
)
(:import (com.vaadin.ui Button$ClickEvent UI)
(java.util Map)
(rx Observable)))
(deftest rx-operators
(testing "Commit"
(let [form (form (button))
b (.getComponent form 0)
fired (atom nil)]
(rx/subscribe
(->> (button-clicks b)
(commit))
(fn [v] (swap! fired (fn [_] v))))
(.click b)
(is (instance? Map @fired))
(is (identical? (:source @fired) b))
(is (instance? Button$ClickEvent (:event @fired)))
(is (identical? (get-field-group form) (:field-group @fired)))
(is (identical? (.getItemDataSource (:field-group @fired)) (:item @fired)))
)
)
(testing "Consume-for"
(let [b (button) l (label) fired (atom nil)]
(->> (button-clicks b)
(consume-for l (fn [l v] (swap! fired (fn [_] {:component l :value v})))))
;(fn [v] (swap! fired (fn [_] v)))
(.click b)
(is (identical? (:component @fired) l))
(is (= (keys (:value @fired)) [:source :event :field-group]))
(is (identical? b (get-in @fired [:value :source])))
(is (nil? (get-in @fired [:value :field-group])))
)
)
(testing "with-ui-access"
(let [result (atom nil)
error (atom nil)
ui (proxy [UI] []
(access [rbl] (.run rbl))
(init [rqst] rqst))]
(UI/setCurrent ui)
(rx/subscribe (->>
(Observable/just "It!")
(with-ui-access))
(fn [v] (swap! result (fn [_] v)))
(fn [e] (swap! error (fn [_] e))))
(is (= @result "It!"))
(is (nil? @error))
)
(let [result (atom nil)
error (atom nil)
ui (proxy [UI] []
(access [rbl] (.run rbl))
(init [rqst] rqst))]
(UI/setCurrent ui)
(rx/subscribe (->>
(Observable/error (NullPointerException. "Test"))
(with-ui-access))
(fn [v] (swap! result (fn [_] v)))
(fn [e] (swap! error (fn [_] e))))
(is (nil? @result))
(is (instance? NullPointerException @error))
(is (= (.getMessage @error) "Test"))
)
)
)