From 50a6258807114f757275ae64e96db58ef2b25eed Mon Sep 17 00:00:00 2001 From: zilti Date: Sat, 16 Dec 2017 05:40:19 +0000 Subject: [PATCH] Fixed: library can't be used from Boot projects --- src/clojurefx/FXClassLoader.java | 28 ---------------------------- src/clojurefx/controllergen.clj | 16 ++++++++++++---- 2 files changed, 12 insertions(+), 32 deletions(-) delete mode 100644 src/clojurefx/FXClassLoader.java diff --git a/src/clojurefx/FXClassLoader.java b/src/clojurefx/FXClassLoader.java deleted file mode 100644 index 8ec49a8..0000000 --- a/src/clojurefx/FXClassLoader.java +++ /dev/null @@ -1,28 +0,0 @@ -package clojurefx; - -public class FXClassLoader { - - public static Class loadClass(String className, byte[] b) { - //override classDefine (as it is protected) and define the class. - Class clazz = null; - try { - ClassLoader loader = ClassLoader.getSystemClassLoader(); - Class cls = Class.forName("java.lang.ClassLoader"); - java.lang.reflect.Method method = - cls.getDeclaredMethod("defineClass", new Class[] { String.class, byte[].class, int.class, int.class }); - - // protected method invocaton - method.setAccessible(true); - try { - Object[] args = new Object[] { className, b, new Integer(0), new Integer(b.length)}; - clazz = (Class) method.invoke(loader, args); - } finally { - method.setAccessible(false); - } - } catch (Exception e) { - e.printStackTrace(); - System.exit(1); - } - return clazz; - } -} diff --git a/src/clojurefx/controllergen.clj b/src/clojurefx/controllergen.clj index 73eff6a..73d3a97 100644 --- a/src/clojurefx/controllergen.clj +++ b/src/clojurefx/controllergen.clj @@ -2,8 +2,7 @@ ;; (:gen-class :name Controllergen ;; :implements [org.objectweb.asm.Opcodes]) ;; (:import (net.openhft.compiler CachedCompiler CompilerUtils)) - (:import (org.objectweb.asm ClassWriter Opcodes) - clojurefx.FXClassLoader) + (:import (org.objectweb.asm ClassWriter Opcodes)) (:use swiss.arrows) (:require [clojure.xml :as xml] [clojure.zip :as zip] @@ -175,6 +174,15 @@ ;; ;; Plumber +(defn define-class [name bytes] + (let [cl (.getClassLoader clojure.lang.RT) + method (.getDeclaredMethod java.lang.ClassLoader "defineClass" + (into-array [String (Class/forName "[B") (Integer/TYPE) (Integer/TYPE)]))] + (try + (.setAccessible method true) + (.invoke method cl (into-array Object [name bytes (int 0) (int (count bytes))])) + (finally (.setAccessible method false))))) + (defn gen-fx-controller-class [fxmlpath clj-fn] (let [clj-fn ^String (if (symbol? clj-fn) (str (namespace clj-fn) "/" (name clj-fn)) @@ -186,5 +194,5 @@ (try (Class/forName (str pkg "." classname)) (catch Exception e - (FXClassLoader/loadClass (str pkg "." classname) - (gen-fx-controller fxmlzip fxmlpath cljvec [pkg classname])))))) + (define-class (str pkg "." classname) + (gen-fx-controller fxmlzip fxmlpath cljvec [pkg classname]))))))