/**
* 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 instantj.compile.Compiler;
import instantj.compile.Source;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.pizzacompiler.compiler.Main;
import net.sf.pizzacompiler.compiler.Report;
import net.sf.pizzacompiler.compiler.SourceReader;
/**
* The concrete implementation for integrating Pizza
* into the compile process
* <pre>
* -Dinstantj.compile.compiler=instantj.compile.pizza.PizzaSourceCompiler
* </pre>
*
* @author <A href="mailto:nils@meiers.net">Nils Meier</A>
*/
public class PizzaSourceCompiler extends Compiler {
/**
* @see instantj.compile.pizza.PizzaSourceCompiler#compileImpl(instantj.compile.Source, boolean, java.util.List)
*/
protected CompiledClass compileImpl(Source source, boolean verbose, List options, Map library) throws CompilationFailedException {
// initialize Pizza
Main.init();
Main.argument("-pizza");
Main.argument("-nowarn");
if (options!=null) for (int i=0; i<options.size(); i++) {
Main.argument(options.get(i).toString());
}
// compile
ContextClassReader cin = new ContextClassReader(getClass().getClassLoader(), library);
HashCompilerOutput cout = new HashCompilerOutput();
SourceReader sin = new InstantSourceReader(source);
PrintStream nil = new PrintStream(new DummyOutputStream());
Main.setClassReader(cin);
Main.compile(
new String[]{ "DoesNotMatter.java" },
sin,
cout,
nil
);
// check compilation result
Set errs = Report.getErrorsAndWarnings();
if (!errs.isEmpty())
throw new CompilationFailedException("Errors or Warnings in Source", errs);
// gather result
CompiledClass result = null;
List inners = new ArrayList();
Iterator it = cout.getNames().iterator();
while (it.hasNext()) {
String name = it.next().toString();
byte[] code = cout.getBytecode(name);
CompiledClass cc = new CompiledClass(name, code);
if (name.equals(source.getName())) result = cc;
else inners.add(cc);
}
if (result==null) throw new CompilationFailedException("Didn't receive byte code for "+source.getName());
if (!inners.isEmpty()) result.addInnerClasses(inners);
if (!library.isEmpty()) result.addDependencies(library.values());
return result;
}
} //PizzaSourceCompiler
|