This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Make ClojureReflector non-static

  • Loading branch information...
1 parent f24ffc8 commit 6eecfdbae6868a990891e4ac9109c48f102e16c7 @stuartsierra committed Mar 25, 2011
@@ -18,29 +18,30 @@ public ClojureCompileTask(Log log, String outputDirectory, String[] namespaces)
}
public void run() {
+ ClojureReflector clojure = new ClojureReflector();
boolean bindingsPushed = false;
try {
Object bindings =
- ClojureReflector.
- map(ClojureReflector.compilePathVar,
+ clojure.
+ map(clojure.compilePathVar,
System.getProperty(PATH_PROP),
- ClojureReflector.warnOnReflectionVar,
+ clojure.warnOnReflectionVar,
"true".equals(System.getProperty(REFLECTION_WARNING_PROP)),
- ClojureReflector.uncheckedMathVar,
+ clojure.uncheckedMathVar,
"true".equals(System.getProperty(UNCHECKED_MATH_PROP)));
- ClojureReflector.pushThreadBindings(bindings);
+ clojure.pushThreadBindings(bindings);
bindingsPushed = true;
for (int i = 0; i < namespaces.length; i++) {
log.info("Compiling " + namespaces[i]);
- ClojureReflector.compile(ClojureReflector.symbol(namespaces[i]));
+ clojure.compile(clojure.symbol(namespaces[i]));
}
} catch (Exception e) {
Thread.currentThread().getThreadGroup().
uncaughtException(Thread.currentThread(), e);
} finally {
if (bindingsPushed) {
try {
- ClojureReflector.popThreadBindings();
+ clojure.popThreadBindings();
} catch (Exception e) {
Thread.currentThread().getThreadGroup().
uncaughtException(Thread.currentThread(), e);
@@ -4,34 +4,37 @@
/** Utility class for accessing the Clojure runtime via the Java
* reflection API, to avoid any compile-time dependencies on a
- * particular Clojure version. */
+ * particular Clojure version.
+ *
+ * This class is not static because it may be loaded multiple times
+ * with different classloaders. */
public class ClojureReflector {
private static final String COMPILE_PATH_PROP = "clojure.compile.path";
private static final String WARN_ON_REFLECTION_PROP = "clojure.compile.warn-on-reflection";
private static final String UNCHECKED_MATH_PROP = "clojure.compile.unchecked-math";
- private static Class RTClass;
- private static Class VarClass;
+ private Class RTClass;
+ private Class VarClass;
- private static Method mapMethod;
- private static Method varMethod;
+ private Method mapMethod;
+ private Method varMethod;
- private static Method invoke0;
- private static Method invoke1;
- private static Method invoke2;
- private static Method invoke3;
- private static Method invoke4;
- private static Method invoke5;
+ private Method invoke0;
+ private Method invoke1;
+ private Method invoke2;
+ private Method invoke3;
+ private Method invoke4;
+ private Method invoke5;
- public static Object compileVar;
- public static Object compilePathVar;
- public static Object pushThreadBindingsVar;
- public static Object popThreadBindingsVar;
- public static Object symbolVar;
- public static Object uncheckedMathVar;
- public static Object warnOnReflectionVar;
+ public Object compileVar;
+ public Object compilePathVar;
+ public Object pushThreadBindingsVar;
+ public Object popThreadBindingsVar;
+ public Object symbolVar;
+ public Object uncheckedMathVar;
+ public Object warnOnReflectionVar;
- static {
+ public ClojureReflector() {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
try {
RTClass = classloader.loadClass("clojure.lang.RT");
@@ -61,70 +64,70 @@
}
/** Returns the Clojure Var with the given namespace and name. */
- public static Object var(String namespace, String name) throws Exception {
+ public Object var(String namespace, String name) throws Exception {
if (varMethod == null)
throw new IllegalStateException("Clojure failed to load");
return varMethod.invoke(null, namespace, name);
}
/** Returns a Clojure IPersistentMap given a series of key-value
* pairs. */
- public static Object map(Object... args) throws Exception {
+ public Object map(Object... args) throws Exception {
if (mapMethod == null)
throw new IllegalStateException("Clojure failed to load");
return mapMethod.invoke(null, (Object)args);
}
/** Invokes a Var as a function with no arguments */
- public static Object invoke(Object theVar) throws Exception{
+ public Object invoke(Object theVar) throws Exception{
if (invoke0 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke0.invoke(theVar);
}
- public static Object invoke(Object theVar, Object arg1) throws Exception{
+ public Object invoke(Object theVar, Object arg1) throws Exception{
if (invoke1 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke1.invoke(theVar, arg1);
}
- public static Object invoke(Object theVar, Object arg1, Object arg2) throws Exception{
+ public Object invoke(Object theVar, Object arg1, Object arg2) throws Exception{
if (invoke2 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke2.invoke(theVar, arg1, arg2);
}
- public static Object invoke(Object theVar, Object arg1, Object arg2, Object arg3) throws Exception{
+ public Object invoke(Object theVar, Object arg1, Object arg2, Object arg3) throws Exception{
if (invoke3 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke3.invoke(theVar, arg1, arg2, arg3);
}
- public static Object invoke(Object theVar, Object arg1, Object arg2, Object arg3, Object arg4) throws Exception{
+ public Object invoke(Object theVar, Object arg1, Object arg2, Object arg3, Object arg4) throws Exception{
if (invoke4 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke4.invoke(theVar, arg1, arg2, arg3, arg4);
}
- public static Object invoke(Object theVar, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws Exception{
+ public Object invoke(Object theVar, Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws Exception{
if (invoke5 == null)
throw new IllegalStateException("Clojure failed to load");
return invoke5.invoke(theVar, arg1, arg2, arg3, arg4, arg5);
}
- public static void pushThreadBindings(Object bindings) throws Exception {
+ public void pushThreadBindings(Object bindings) throws Exception {
invoke(pushThreadBindingsVar, bindings);
}
- public static void popThreadBindings() throws Exception {
+ public void popThreadBindings() throws Exception {
invoke(popThreadBindingsVar);
}
- public static Object symbol(String name) throws Exception {
+ public Object symbol(String name) throws Exception {
return invoke(symbolVar, name);
}
- public static Object compile(Object symbol) throws Exception {
+ public Object compile(Object symbol) throws Exception {
return invoke(compileVar, symbol);
}
}

0 comments on commit 6eecfdb

Please sign in to comment.