/**
* InstantJ
*
* Copyright (C) 2002 Nils Meier
* Additional changes (C) 2002 Andy Thomas
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*/
package instantj.compile.pizza;
import instantj.compile.CompilationFailedException;
import instantj.compile.CompiledClass;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Map;
import net.sf.pizzacompiler.compiler.ClassReader;
/**
* A patched ClassReader that reads resouces from
* classpath (e.g. class file). This allows to compile
* classes that reference class-bytes in the current
* context-classloader but not available through the
* global classpath
*/
/*package*/ class ContextClassReader extends ClassReader {
/** the context classloader */
private ClassLoader cl;
/** the library */
private Map lib;
/**
* Constructor
*/
/*package*/ ContextClassReader(ClassLoader classloader, Map library) throws CompilationFailedException {
// remember
cl = classloader;
lib = library;
// 20030305 o.k. at this point I don't understand how class-bytes that
// are not in the classpath can be fed to Pizza. find() seems to be
// called for well known java.lang. classes only and classes that I want
// to load dynamically from classloader/library don't get triggered.
// Tried to play with loadClass() but am no optimistic :(
// Won't support library at this point ...
if (library!=null&&!library.isEmpty())
throw new CompilationFailedException("Pizza integration doesn't support library");
// // publish libary names
// try {
// Iterator it = library.getAll().iterator();
// while (it.hasNext()) {
// CompiledClass cc = (CompiledClass)it.next();
// loadClass(Name.fromString(cc.getName()));
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// done
}
// /**
// * @see net.sf.pizzacompiler.compiler.ClassReader#loadClass(net.sf.pizzacompiler.compiler.Name)
// */
// public ClassSymbol loadClass(Name arg0) throws IOException {
// System.out.println("Load "+arg0);
// return super.loadClass(arg0);
// }
/**
* Override to find class-bytes in local classpath if available
* @see instantj.compile.pizza.PizzaSourceCompiler#find(java.lang.String)
*/
protected InputStream find(String name) {
// patch path
String s = name.replace('\\','/');
// try to load from classloader
InputStream result = cl.getResourceAsStream(s);
if (result!=null) return result;
// Check if the library is sufficient
if (s.endsWith(".class")) {
s = s.substring(0, s.lastIndexOf(".class"));
CompiledClass cc = (CompiledClass)lib.get(s);
if (cc!=null) return new ByteArrayInputStream(cc.getByteCode());
}
// super might do
return super.find(name);
}
} //InstantClassReader
|