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