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);
64 public byte[] getBytes(String name) {
65 // Non-SCL classes are not handled here
66 if(!name.startsWith(SCL_PACKAGE_PREFIX))
69 // Determine the id of the class loader which is responsible of the class
70 String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
72 // Is class defined locally in this class loader?
73 if(requestedModuleName.equals(basePackageName)) {
74 String internalName = name.replace('.', '/');
75 byte[] bytes = localClasses.get(internalName);
78 return localClasses.get(internalName);
81 // Find suitable class loader that has this class locally
83 RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
84 if(parentModule == null)
87 // Find the class from the ancestor class loader
88 return parentModule.classLoader.getBytes(name);
92 private Class<?> getClass(String name) throws ClassNotFoundException {
93 //System.out.println("getClass " + name);
95 // If the class is not generated from SCL, use parent class loader
96 if(!name.startsWith(SCL_PACKAGE_PREFIX)) {
98 return getParent().loadClass(name);
99 } catch(ClassNotFoundException e) {
100 for(RuntimeModule module : runtimeModuleMap.values())
102 return module.classLoader.getParent().loadClass(name);
103 } catch(ClassNotFoundException e2) {
106 throw new ClassNotFoundException(name);
109 // Determine the id of the class loader which is responsible of the class
110 String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
112 // Is class defined locally in this class loader?
113 if(requestedModuleName.equals(basePackageName))
114 return getLocalClass(name);
116 // Find suitable class loader that has this class locally
118 RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
119 if(parentModule == null)
120 throw new ClassNotFoundException(name);
122 // Find the class from the ancestor class loader
123 return parentModule.classLoader.getLocalClass(name);
128 public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
129 Class<?> clazz = getClass(name);
135 public synchronized String getFreshPackageName() {
136 return basePackageName + "$" + (++transientPackageId);
140 public THashMap<Constant, Object> getConstantCache() {
145 public ClassLoader getClassLoader() {