001: /**
002: * InstantJ
003: *
004: * Copyright (C) 2002 Nils Meier
005: * Additional changes (C) 2002 Andy Thomas
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: */package instantj.compile.pizza;
018:
019: import instantj.compile.CompilationFailedException;
020: import instantj.compile.CompiledClass;
021: import instantj.compile.Compiler;
022: import instantj.compile.Source;
023:
024: import java.io.PrintStream;
025: import java.util.ArrayList;
026: import java.util.Iterator;
027: import java.util.List;
028: import java.util.Map;
029: import java.util.Set;
030:
031: import net.sf.pizzacompiler.compiler.Main;
032: import net.sf.pizzacompiler.compiler.Report;
033: import net.sf.pizzacompiler.compiler.SourceReader;
034:
035: /**
036: * The concrete implementation for integrating Pizza
037: * into the compile process
038: * <pre>
039: * -Dinstantj.compile.compiler=instantj.compile.pizza.PizzaSourceCompiler
040: * </pre>
041: *
042: * @author <A href="mailto:nils@meiers.net">Nils Meier</A>
043: */
044: public class PizzaSourceCompiler extends Compiler {
045:
046: /**
047: * @see instantj.compile.pizza.PizzaSourceCompiler#compileImpl(instantj.compile.Source, boolean, java.util.List)
048: */
049: protected CompiledClass compileImpl(Source source, boolean verbose,
050: List options, Map library)
051: throws CompilationFailedException {
052:
053: // initialize Pizza
054: Main.init();
055: Main.argument("-pizza");
056: Main.argument("-nowarn");
057:
058: if (options != null)
059: for (int i = 0; i < options.size(); i++) {
060: Main.argument(options.get(i).toString());
061: }
062:
063: // compile
064: ContextClassReader cin = new ContextClassReader(getClass()
065: .getClassLoader(), library);
066: HashCompilerOutput cout = new HashCompilerOutput();
067: SourceReader sin = new InstantSourceReader(source);
068: PrintStream nil = new PrintStream(new DummyOutputStream());
069: Main.setClassReader(cin);
070: Main.compile(new String[] { "DoesNotMatter.java" }, sin, cout,
071: nil);
072:
073: // check compilation result
074: Set errs = Report.getErrorsAndWarnings();
075: if (!errs.isEmpty())
076: throw new CompilationFailedException(
077: "Errors or Warnings in Source", errs);
078:
079: // gather result
080: CompiledClass result = null;
081: List inners = new ArrayList();
082:
083: Iterator it = cout.getNames().iterator();
084: while (it.hasNext()) {
085: String name = it.next().toString();
086: byte[] code = cout.getBytecode(name);
087: CompiledClass cc = new CompiledClass(name, code);
088: if (name.equals(source.getName()))
089: result = cc;
090: else
091: inners.add(cc);
092: }
093:
094: if (result == null)
095: throw new CompilationFailedException(
096: "Didn't receive byte code for " + source.getName());
097: if (!inners.isEmpty())
098: result.addInnerClasses(inners);
099: if (!library.isEmpty())
100: result.addDependencies(library.values());
101: return result;
102: }
103:
104: } //PizzaSourceCompiler
|