annotate 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
rev   line source
rlm@10 1 /**
rlm@10 2 * Copyright (c) Rich Hickey. All rights reserved.
rlm@10 3 * The use and distribution terms for this software are covered by the
rlm@10 4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 5 * which can be found in the file epl-v10.html at the root of this distribution.
rlm@10 6 * By using this software in any fashion, you are agreeing to be bound by
rlm@10 7 * the terms of this license.
rlm@10 8 * You must not remove this notice, or any other, from this software.
rlm@10 9 **/
rlm@10 10
rlm@10 11 /* rich Aug 21, 2007 */
rlm@10 12
rlm@10 13 package clojure.lang;
rlm@10 14
rlm@10 15 import java.util.HashMap;
rlm@10 16 import java.util.Map;
rlm@10 17 import java.util.concurrent.ConcurrentHashMap;
rlm@10 18 import java.net.URLClassLoader;
rlm@10 19 import java.net.URL;
rlm@10 20 import java.lang.ref.ReferenceQueue;
rlm@10 21 import java.lang.ref.SoftReference;
rlm@10 22
rlm@10 23 public class DynamicClassLoader extends URLClassLoader{
rlm@10 24 HashMap<Integer, Object[]> constantVals = new HashMap<Integer, Object[]>();
rlm@10 25 static ConcurrentHashMap<String, SoftReference<Class>>classCache =
rlm@10 26 new ConcurrentHashMap<String, SoftReference<Class> >();
rlm@10 27
rlm@10 28 static final URL[] EMPTY_URLS = new URL[]{};
rlm@10 29
rlm@10 30 static final ReferenceQueue rq = new ReferenceQueue();
rlm@10 31
rlm@10 32 public DynamicClassLoader(){
rlm@10 33 //pseudo test in lieu of hasContextClassLoader()
rlm@10 34 super(EMPTY_URLS,(Thread.currentThread().getContextClassLoader() == null ||
rlm@10 35 Thread.currentThread().getContextClassLoader() == ClassLoader.getSystemClassLoader())?
rlm@10 36 Compiler.class.getClassLoader():Thread.currentThread().getContextClassLoader());
rlm@10 37 }
rlm@10 38
rlm@10 39 public DynamicClassLoader(ClassLoader parent){
rlm@10 40 super(EMPTY_URLS,parent);
rlm@10 41 }
rlm@10 42
rlm@10 43 public Class defineClass(String name, byte[] bytes, Object srcForm){
rlm@10 44 Util.clearCache(rq, classCache);
rlm@10 45 Class c = defineClass(name, bytes, 0, bytes.length);
rlm@10 46 classCache.put(name, new SoftReference(c,rq));
rlm@10 47 return c;
rlm@10 48 }
rlm@10 49
rlm@10 50 protected Class<?> findClass(String name) throws ClassNotFoundException{
rlm@10 51 SoftReference<Class> cr = classCache.get(name);
rlm@10 52 if(cr != null)
rlm@10 53 {
rlm@10 54 Class c = cr.get();
rlm@10 55 if(c != null)
rlm@10 56 return c;
rlm@10 57 }
rlm@10 58 return super.findClass(name);
rlm@10 59 }
rlm@10 60
rlm@10 61 public void registerConstants(int id, Object[] val){
rlm@10 62 constantVals.put(id, val);
rlm@10 63 }
rlm@10 64
rlm@10 65 public Object[] getConstants(int id){
rlm@10 66 return constantVals.get(id);
rlm@10 67 }
rlm@10 68
rlm@10 69 public void addURL(URL url){
rlm@10 70 super.addURL(url);
rlm@10 71 }
rlm@10 72
rlm@10 73 }