Permalink
Browse files

model loader: adapted to latest model loader API for ceylon/ceylon-co…

…mpiler#1052

Required to support multiple modules of the same name different version at runtime, but
not tested yet.
  • Loading branch information...
1 parent 4ebf3b5 commit ba5776f8229854935a9dd11a0145a5d25ca4ddac @FroMage FroMage committed May 28, 2013
@@ -5,10 +5,15 @@
import java.util.List;
import java.util.Map;
+import org.jboss.modules.ModuleClassLoader;
+
import com.redhat.ceylon.cmr.api.ArtifactResult;
import com.redhat.ceylon.cmr.impl.JULLogger;
import com.redhat.ceylon.compiler.loader.impl.reflect.CachedTOCJars;
import com.redhat.ceylon.compiler.loader.impl.reflect.ReflectionModelLoader;
+import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionClass;
+import com.redhat.ceylon.compiler.loader.impl.reflect.mirror.ReflectionUtils;
+import com.redhat.ceylon.compiler.loader.mirror.ClassMirror;
import com.redhat.ceylon.compiler.loader.model.LazyModule;
import com.redhat.ceylon.compiler.typechecker.analyzer.ModuleManager;
import com.redhat.ceylon.compiler.typechecker.model.Module;
@@ -17,42 +22,39 @@
public class RuntimeModelLoader extends ReflectionModelLoader {
private Map<Module,ClassLoader> classLoaders = new HashMap<Module,ClassLoader>();
+ private Map<String, Module> moduleCache = new HashMap<String, Module>();
private CachedTOCJars jars = new CachedTOCJars();
public RuntimeModelLoader(ModuleManager moduleManager, Modules modules) {
super(moduleManager, modules, new JULLogger());
}
@Override
- protected List<String> getPackageList(String packageName) {
+ protected List<String> getPackageList(Module module, String packageName) {
return jars.getPackageList(packageName);
}
@Override
- protected boolean packageExists(String packageName) {
+ protected boolean packageExists(Module module, String packageName) {
return jars.packageExists(packageName);
}
@Override
- protected Class<?> loadClass(String name) {
- for(ClassLoader cl : classLoaders.values()){
- try{
- return cl.loadClass(name);
- }catch(ClassNotFoundException x){
- // keep trying
- }
+ protected Class<?> loadClass(Module module, String name) {
+ ClassLoader classLoader = classLoaders.get(module);
+ if(classLoader == null)
+ return null;
+ try{
+ return classLoader.loadClass(name);
+ }catch(ClassNotFoundException x){
+ return null;
}
- return null;
-// // try the system class loader
-// try {
-// return ClassLoader.getSystemClassLoader().loadClass(name);
-// } catch (ClassNotFoundException e) {
-// return null;
-// }
}
@Override
public void addModuleToClassPath(Module module, ArtifactResult artifact) {
+ String cacheKey = cacheKeyByModule(module.getNameAsString(), module.getVersion());
+ moduleCache.put(cacheKey, module);
if(artifact == null)
return;
File file = artifact.artifact();
@@ -66,4 +68,33 @@ public void addModuleClassLoader(Module module, ClassLoader classLoader) {
classLoaders.put(module, classLoader);
}
+ @Override
+ protected Module findModuleForClassMirror(ClassMirror classMirror) {
+ Class<?> klass = ((ReflectionClass)classMirror).klass;
+ return findModuleForClass(klass);
+ }
+
+ public Module findModuleForClass(Class<?> klass){
+ ClassLoader cl = klass.getClassLoader();
+ if(cl instanceof ModuleClassLoader){
+ ModuleClassLoader classLoader = (ModuleClassLoader)cl;
+ org.jboss.modules.Module jbossModule = classLoader.getModule();
+ String name = jbossModule.getIdentifier().getName();
+ String version = jbossModule.getIdentifier().getSlot();
+ String cacheKey = cacheKeyByModule(name, version);
+ return moduleCache.get(cacheKey);
+ }else{
+ // revert to a single classloader version?
+ // FIXME: perhaps we can have other one-classloader-to-one-module setups than jboss modules?
+ String pkgName = ReflectionUtils.getPackageName(klass);
+ return lookupModuleInternal(pkgName);
+ }
+ }
+
+ private String cacheKeyByModule(String name, String version) {
+ if(name.equals(Module.DEFAULT_MODULE_NAME))
+ return name + "/"; // no version
+ return name + "/" + version;
+ }
+
}
@@ -61,10 +61,10 @@ public void loadModule(String name, String version, ArtifactResult artifact, Cla
modelLoader.addModuleToClassPath(module, artifact);
modelLoader.addModuleClassLoader(module, classLoader);
module.setAvailable(true);
+
if(!module.isDefault()){
- Module compiledModule = modelLoader.loadCompiledModule(name);
// FIXME: dependencies of Ceylon modules?
- if(compiledModule == null){
+ if(!modelLoader.loadCompiledModule(module)){
// we didn't find module.class so it must be a java module if it's not the default module
((LazyModule)module).setJava(true);
@@ -86,4 +86,8 @@ public void loadModule(String name, String version, ArtifactResult artifact, Cla
public RuntimeModelLoader getModelLoader() {
return (RuntimeModelLoader) super.getModelLoader();
}
+
+ public Module findModuleForClass(java.lang.Class<?> klass){
+ return getModelLoader().findModuleForClass(klass);
+ }
}
@@ -5,6 +5,7 @@
import com.redhat.ceylon.compiler.loader.ModelLoader.DeclarationType;
import com.redhat.ceylon.compiler.typechecker.model.IntersectionType;
+import com.redhat.ceylon.compiler.typechecker.model.Module;
import com.redhat.ceylon.compiler.typechecker.model.NothingType;
import com.redhat.ceylon.compiler.typechecker.model.ProducedType;
import com.redhat.ceylon.compiler.typechecker.model.TypeDeclaration;
@@ -79,7 +80,8 @@ public ProducedType toProducedType(RuntimeModuleManager moduleManager){
public ProducedType toProducedType(ProducedType qualifyingType, RuntimeModuleManager moduleManager){
String typeName = klass.getName();
- TypeDeclaration decl = (TypeDeclaration) moduleManager.getModelLoader().getDeclaration(typeName, DeclarationType.TYPE);
+ Module module = moduleManager.findModuleForClass(klass);
+ TypeDeclaration decl = (TypeDeclaration) moduleManager.getModelLoader().getDeclaration(module, typeName, DeclarationType.TYPE);
List<ProducedType> typeArgs = new ArrayList<ProducedType>(typeArguments.length);
for(TypeDescriptor typeArg : typeArguments){
typeArgs.add(typeArg.toProducedType(moduleManager));

0 comments on commit ba5776f

Please sign in to comment.