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 public byte[] getBytes(String name) {
64 // Non-SCL classes are not handled here
65 if(!name.startsWith(SCL_PACKAGE_PREFIX))
68 // Determine the id of the class loader which is responsible of the class
69 String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
71 // Is class defined locally in this class loader?
72 if(requestedModuleName.equals(basePackageName)) {
73 String internalName = name.replace('.', '/');
74 byte[] bytes = localClasses.get(internalName);
77 return localClasses.get(internalName);
80 // Find suitable class loader that has this class locally
82 RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
83 if(parentModule == null)
86 // Find the class from the ancestor class loader
87 return parentModule.classLoader.getBytes(name);
91 private Class<?> getClass(String name) throws ClassNotFoundException {
92 //System.out.println("getClass " + name);
94 // If the class is not generated from SCL, use parent class loader
95 if(!name.startsWith(SCL_PACKAGE_PREFIX)) {
97 return getParent().loadClass(name);
98 } catch(ClassNotFoundException e) {
99 for(RuntimeModule module : runtimeModuleMap.values())
101 return module.classLoader.getParent().loadClass(name);
102 } catch(ClassNotFoundException e2) {
105 throw new ClassNotFoundException(name);
108 // Determine the id of the class loader which is responsible of the class
109 String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
111 // Is class defined locally in this class loader?
112 if(requestedModuleName.equals(basePackageName))
113 return getLocalClass(name);
115 // Find suitable class loader that has this class locally
117 RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
118 if(parentModule == null)
119 throw new ClassNotFoundException(name);
121 // Find the class from the ancestor class loader
122 return parentModule.classLoader.getLocalClass(name);
127 public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
128 Class<?> clazz = getClass(name);
134 public synchronized String getFreshPackageName() {
135 return basePackageName + "$" + (++transientPackageId);
139 public THashMap<Constant, Object> getConstantCache() {
144 public ClassLoader getClassLoader() {