diff src/clojure/lang/DynamicClassLoader.java @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/lang/DynamicClassLoader.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,73 @@
     1.4 +/**
     1.5 + *   Copyright (c) Rich Hickey. All rights reserved.
     1.6 + *   The use and distribution terms for this software are covered by the
     1.7 + *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.8 + *   which can be found in the file epl-v10.html at the root of this distribution.
     1.9 + *   By using this software in any fashion, you are agreeing to be bound by
    1.10 + * 	 the terms of this license.
    1.11 + *   You must not remove this notice, or any other, from this software.
    1.12 + **/
    1.13 +
    1.14 +/* rich Aug 21, 2007 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +import java.util.HashMap;
    1.19 +import java.util.Map;
    1.20 +import java.util.concurrent.ConcurrentHashMap;
    1.21 +import java.net.URLClassLoader;
    1.22 +import java.net.URL;
    1.23 +import java.lang.ref.ReferenceQueue;
    1.24 +import java.lang.ref.SoftReference;
    1.25 +
    1.26 +public class DynamicClassLoader extends URLClassLoader{
    1.27 +HashMap<Integer, Object[]> constantVals = new HashMap<Integer, Object[]>();
    1.28 +static ConcurrentHashMap<String, SoftReference<Class>>classCache =
    1.29 +        new ConcurrentHashMap<String, SoftReference<Class> >();
    1.30 +
    1.31 +static final URL[] EMPTY_URLS = new URL[]{};
    1.32 +
    1.33 +static final ReferenceQueue rq = new ReferenceQueue();
    1.34 +
    1.35 +public DynamicClassLoader(){
    1.36 +    //pseudo test in lieu of hasContextClassLoader()
    1.37 +	super(EMPTY_URLS,(Thread.currentThread().getContextClassLoader() == null ||
    1.38 +                Thread.currentThread().getContextClassLoader() == ClassLoader.getSystemClassLoader())?
    1.39 +                Compiler.class.getClassLoader():Thread.currentThread().getContextClassLoader());
    1.40 +}
    1.41 +
    1.42 +public DynamicClassLoader(ClassLoader parent){
    1.43 +	super(EMPTY_URLS,parent);
    1.44 +}
    1.45 +
    1.46 +public Class defineClass(String name, byte[] bytes, Object srcForm){
    1.47 +	Util.clearCache(rq, classCache);
    1.48 +	Class c = defineClass(name, bytes, 0, bytes.length);
    1.49 +    classCache.put(name, new SoftReference(c,rq));
    1.50 +    return c;
    1.51 +}
    1.52 +
    1.53 +protected Class<?> findClass(String name) throws ClassNotFoundException{
    1.54 +    SoftReference<Class> cr = classCache.get(name);
    1.55 +	if(cr != null)
    1.56 +		{
    1.57 +		Class c = cr.get();
    1.58 +        if(c != null)
    1.59 +            return c;
    1.60 +		}
    1.61 +	return super.findClass(name);
    1.62 +}
    1.63 +
    1.64 +public void registerConstants(int id, Object[] val){
    1.65 +	constantVals.put(id, val);
    1.66 +}
    1.67 +
    1.68 +public Object[] getConstants(int id){
    1.69 +	return constantVals.get(id);
    1.70 +}
    1.71 +
    1.72 +public void addURL(URL url){
    1.73 +	super.addURL(url);
    1.74 +}
    1.75 +
    1.76 +}