<commit_before>(ns ccw.core.factories
  (:import [clojure.lang Reflector]))

(defn handler-factory 
  "Creates an org.eclipse.core.commands.IHandler instance by proxying 
   org.eclipse.core.commands.AbstractHandler and overriding its 
   (.execute this event) method with a call to (handler this event)."
  [{handler "handler"}]
  (let [handler (symbol handler)]
    (require (-> handler namespace symbol))
    (let [handler (find-var handler)]
      (proxy [org.eclipse.core.commands.AbstractHandler]
             []
        (execute [event] (when handler (handler this event)))))))

(defn java-factory 
  "Factory for an Eclipse plugin executable extension which calls the no-arg
   constructor of the java class whose fully qualified name is passed in javaClass"
  [{java-class "javaClass"}]
  (println "I was called, hey!")
  (try
    (let [class (-> (Thread/currentThread) 
                  .getContextClassLoader
                  (.loadClass java-class))]
      (let [i (Reflector/invokeConstructor class (make-array Object 0))]
        (println "instance created: " i)
        i))
    (catch Exception e
      (println "sent exception: " (.getMessage e) "," e)
      (.printStackTrace e))))
<commit_msg>make-factory works and also supports instantiation from given bundle.<commit_after>(ns ccw.core.factories
  (:import [clojure.lang Reflector]))

(defn handler-factory 
  "Creates an org.eclipse.core.commands.IHandler instance by proxying 
   org.eclipse.core.commands.AbstractHandler and overriding its 
   (.execute this event) method with a call to (handler this event)."
  [{handler "handler"}]
  (let [handler (symbol handler)]
    (require (-> handler namespace symbol))
    (let [handler (find-var handler)]
      (proxy [org.eclipse.core.commands.AbstractHandler]
             []
        (execute [event] (when handler (handler this event)))))))

(defn make-factory
  "Creates a factory function for creating instances of the given java
   class name using the constructor with given argument types.
   The types of the arguments must be specified in a symbol sequence,
   which must be emtpy when the no arg constructor is to be used.
   The fn produced uses type hints to enable compiler/jit
   optimizations, at the expense of making this factory method slower
   so it's best to store its result in a var.
   A Bundle instance can optionally be passed for loading the class.
   Example:
     (def int-factory (make-factory \"Integer\" ['String]))
     (int-factory \"42\")
   See http://stackoverflow.com/a/3752276 for original inspiration."
  ([classname types]
    (let [args (map #(with-meta (symbol (str "x" %2)) {:tag %1}) types (range)) ]
      (eval `(fn [~@args] (new ~(symbol classname) ~@args)))))
  ([bundle classname types]
    (let [zclass (.. bundle (loadClass classname))
          constructor (.getConstructor zclass (into-array Class (map resolve types)))]
      (fn [& args] (.newInstance constructor (into-array Object args))))))
{'fraction_non_alphanumeric': 0.11339165545087483, 'fraction_numerical': 0.004037685060565276, 'mean_word_length': 3.3720588235294118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1871542', 'n_tokens_mistral': 847, 'n_tokens_neox': 809, 'n_words': 317}