1 package org.simantics.scl.compiler.runtime;
5 import org.simantics.scl.compiler.constants.Constant;
7 import gnu.trove.map.hash.THashMap;
9 public class ExpressionClassLoader extends ClassLoader implements MutableClassLoader {
10 public static final boolean VALIDATE_CLASS_NAMES = true;
11 public static final boolean TRACE_CLASS_CREATION = false;
13 String basePackageName;
14 THashMap<String, byte[]> localClasses = new THashMap<String, byte[]>();
15 THashMap<String, RuntimeModule> runtimeModuleMap;
16 int transientPackageId = 0;
17 THashMap<Constant,Object> valueCache = new THashMap<Constant,Object>();
19 public ExpressionClassLoader(ClassLoader parent, THashMap<String, RuntimeModule> runtimeModuleMap, String basePackageName) {
21 this.basePackageName = basePackageName;
22 this.runtimeModuleMap = runtimeModuleMap;
25 public synchronized void addClass(String name, byte[] class_) {
26 if(TRACE_CLASS_CREATION)
27 System.out.println("addClass " + name + " (" + class_.length + " bytes)");
28 if(VALIDATE_CLASS_NAMES)
29 validateClassName(name);
30 localClasses.put(name, class_);
33 public synchronized void addClasses(Map<String, byte[]> classes) {
34 if(TRACE_CLASS_CREATION)
35 for(String name : classes.keySet())
36 System.out.println("addClass " + name + " (" + classes.get(name).length + " bytes)");
37 if(VALIDATE_CLASS_NAMES)
38 for(String name : classes.keySet())
39 validateClassName(name);
40 localClasses.putAll(classes);
43 private void validateClassName(String name) {
44 /*if(!name.startsWith(SCL_PACKAGE_PREFIX) || !extractClassLoaderId(name).equals(basePackageName))
45 throw new IllegalArgumentException("Class name " + name + " does not start with '" +
46 SCL_PACKAGE_PREFIX + basePackageName + "$'.");*/
49 private synchronized Class<?> getLocalClass(String name) throws ClassNotFoundException {
50 // Is class already loaded
51 Class<?> clazz = findLoadedClass(name);
55 // If we have bytecode for it, let's define the class
56 byte[] bytes = localClasses.get(name.replace('.', '/'));
58 throw new ClassNotFoundException(name);
60 return defineClass(name, bytes, 0, bytes.length);
63 private Class<?> getClass(String name) throws ClassNotFoundException {
64 //System.out.println("getClass " + name);
66 // If the class is not generated from SCL, use parent class loader
67 if(!name.startsWith(SCL_PACKAGE_PREFIX)) {
69 return getParent().loadClass(name);
70 } catch(ClassNotFoundException e) {
71 for(RuntimeModule module : runtimeModuleMap.values())
73 return module.classLoader.getParent().loadClass(name);
74 } catch(ClassNotFoundException e2) {
77 throw new ClassNotFoundException(name);
80 // Determine the id of the class loader which is responsible of the class
81 String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
83 // Is class defined locally in this class loader?
84 if(requestedModuleName.equals(basePackageName))
85 return getLocalClass(name);
87 // Find suitable class loader that has this class locally
89 RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
90 if(parentModule == null)
91 throw new ClassNotFoundException(name);
93 // Find the class from the ancestor class loader
94 return parentModule.classLoader.getLocalClass(name);
99 public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
100 Class<?> clazz = getClass(name);
106 public synchronized String getFreshPackageName() {
107 return basePackageName + "$" + (++transientPackageId);
111 public THashMap<Constant, Object> getConstantCache() {
116 public ClassLoader getClassLoader() {