]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/runtime/ExpressionClassLoader.java
Added info on backup location to documentation backup.
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / runtime / ExpressionClassLoader.java
1 package org.simantics.scl.compiler.runtime;
2
3 import java.util.Map;
4
5 import org.simantics.scl.compiler.constants.Constant;
6
7 import gnu.trove.map.hash.THashMap;
8
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;
12     
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>(); 
18     
19     public ExpressionClassLoader(ClassLoader parent, THashMap<String, RuntimeModule> runtimeModuleMap, String basePackageName) {
20         super(parent);
21         this.basePackageName = basePackageName;
22         this.runtimeModuleMap = runtimeModuleMap;
23     }
24     
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_);
31     }
32     
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);
41     }
42     
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 + "$'.");*/
47     }
48     
49     private synchronized Class<?> getLocalClass(String name) throws ClassNotFoundException {
50         // Is class already loaded
51         Class<?> clazz = findLoadedClass(name);
52         if(clazz != null)
53             return clazz;
54
55         // If we have bytecode for it, let's define the class
56         byte[] bytes = localClasses.get(name.replace('.', '/'));
57         if(bytes == null)
58             throw new ClassNotFoundException(name);
59
60         return defineClass(name, bytes, 0, bytes.length);
61     }
62     
63     private Class<?> getClass(String name) throws ClassNotFoundException {
64         //System.out.println("getClass " + name);
65         
66         // If the class is not generated from SCL, use parent class loader
67         if(!name.startsWith(SCL_PACKAGE_PREFIX)) {
68             try {
69                 return getParent().loadClass(name);
70             } catch(ClassNotFoundException e) {
71                 for(RuntimeModule module : runtimeModuleMap.values())
72                     try {
73                         return module.classLoader.getParent().loadClass(name);
74                     } catch(ClassNotFoundException e2) {
75                     }
76             }
77             throw new ClassNotFoundException(name);
78         }
79         
80         // Determine the id of the class loader which is responsible of the class
81         String requestedModuleName = RuntimeModule.extractClassLoaderId(name);
82         
83         // Is class defined locally in this class loader?
84         if(requestedModuleName.equals(basePackageName))
85             return getLocalClass(name);
86
87         // Find suitable class loader that has this class locally
88         else {
89             RuntimeModule parentModule = runtimeModuleMap.get(requestedModuleName);
90             if(parentModule == null)
91                 throw new ClassNotFoundException(name);
92
93             // Find the class from the ancestor class loader
94             return parentModule.classLoader.getLocalClass(name);
95         }
96     }
97     
98     @Override
99     public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
100         Class<?> clazz = getClass(name);
101         if (resolve)
102             resolveClass(clazz);
103         return clazz;
104     }
105     
106     public synchronized String getFreshPackageName() {
107         return basePackageName + "$" + (++transientPackageId);
108     }
109
110     @Override
111     public THashMap<Constant, Object> getConstantCache() {
112         return valueCache;
113     }
114
115     @Override
116     public ClassLoader getClassLoader() {
117         return this;
118     }
119 }