001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.support.builder;
019:
020: import java.io.InputStream;
021: import java.io.PrintWriter;
022: import java.util.ArrayList;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.eclipse.jdt.core.compiler.CategorizedProblem;
027: import org.eclipse.jdt.internal.compiler.CompilationResult;
028: import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
029: import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
030: import org.eclipse.jdt.internal.compiler.IProblemFactory;
031: import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
032: import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
033: import org.eclipse.jdt.internal.compiler.batch.Main;
034: import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
035: import org.eclipse.jdt.internal.compiler.util.Util;
036:
037: import spoon.reflect.Factory;
038:
039: public class JDTCompiler extends Main implements ICompilerRequestor {
040:
041: class Compiler extends org.eclipse.jdt.internal.compiler.Compiler {
042:
043: @SuppressWarnings("deprecation")
044: public Compiler(INameEnvironment environment,
045: IErrorHandlingPolicy policy, Map<?, ?> settings,
046: ICompilerRequestor requestor,
047: IProblemFactory problemFactory, PrintWriter out,
048: boolean statementsRecovery) {
049: super (environment, policy, settings, requestor,
050: problemFactory, statementsRecovery);
051: }
052:
053: public CompilationUnitDeclaration[] compileUnits(
054: CompilationUnit[] sourceUnits) {
055: CompilationUnitDeclaration unit = null;
056: int i = 0;
057: // build and record parsed units
058: beginToCompile(sourceUnits);
059: // process all units (some more could be injected in the loop by
060: // the lookup environment)
061: for (; i < this .totalUnits; i++) {
062: unit = unitsToProcess[i];
063: this .parser.getMethodBodies(unit);
064:
065: // fault in fields & methods
066: if (unit.scope != null)
067: unit.scope.faultInTypes();
068: // verify inherited methods
069: if (unit.scope != null)
070: unit.scope.verifyMethods(lookupEnvironment
071: .methodVerifier());
072: // type checking
073: unit.resolve();
074: // flow analysis
075: unit.analyseCode();
076:
077: requestor.acceptResult(unit.compilationResult
078: .tagAsAccepted());
079: }
080: return this .unitsToProcess;
081: }
082: }
083:
084: public static int JAVA_COMPLIANCE = 5;
085:
086: boolean success = true;
087:
088: public boolean compileSrc(Factory f, List<CtFile> files)
089: throws Exception {
090: if (files.isEmpty())
091: return true;
092: // long t=System.currentTimeMillis();
093: // Build input
094: List<String> args = new ArrayList<String>();
095: args.add("-1." + JAVA_COMPLIANCE);
096: args.add("-preserveAllLocals");
097: args.add("-enableJavadoc");
098: args.add("-noExit");
099: // args.add("-nowarn");
100: args.add(f.getEnvironment().getSourcePath());
101:
102: // JDTCompiler compiler = new JDTCompiler(new PrintWriter(System.out),
103: // new PrintWriter(System.err));
104: configure(args.toArray(new String[0]));
105: // f.getEnvironment().debugMessage("compiling src: "+files);
106: CompilationUnitDeclaration[] units = getUnits(files, f);
107: // f.getEnvironment().debugMessage("got units in "+(System.currentTimeMillis()-t)+" ms");
108:
109: JDTTreeBuilder builder = new JDTTreeBuilder(f);
110:
111: for (CompilationUnitDeclaration unit : units) {
112: try {
113: // t=System.currentTimeMillis();
114: unit.traverse(builder, unit.scope);
115: // f.getEnvironment().debugMessage("built unit "+new String(unit.getMainTypeName())+" in "+(System.currentTimeMillis()-t)+" ms");
116: } catch (Exception e) {
117: e.printStackTrace();
118: }
119: }
120: return success;
121: }
122:
123: public boolean compileTemplate(Factory f, List<CtFile> streams)
124: throws Exception {
125: if (streams.isEmpty())
126: return true;
127: // Build input
128: List<String> args = new ArrayList<String>();
129: args.add("-1." + JAVA_COMPLIANCE);
130: args.add("-preserveAllLocals");
131: args.add("-enableJavadoc");
132: args.add("-noExit");
133: args.add("-nowarn");
134: args.add(".");
135:
136: // JDTCompiler compiler = new JDTCompiler(new PrintWriter(System.out),
137: // new PrintWriter(System.err));
138: configure(args.toArray(new String[0]));
139:
140: CompilationUnitDeclaration[] units = getUnits(streams, f);
141:
142: JDTTreeBuilder builder = new JDTTreeBuilder(f);
143: builder.template = true;
144: for (CompilationUnitDeclaration unit : units) {
145: unit.traverse(builder, unit.scope);
146: }
147: return success;
148: }
149:
150: Compiler batchCompiler;
151:
152: PrintWriter out;
153:
154: public JDTCompiler(PrintWriter outWriter, PrintWriter errWriter) {
155: super (outWriter, errWriter, false);
156: }
157:
158: public JDTCompiler() {
159: super (new PrintWriter(System.out), new PrintWriter(System.err),
160: false);
161: }
162:
163: /*
164: * Build the set of compilation source units
165: */
166: public CompilationUnit[] getCompilationUnits(List<CtFile> streams)
167: throws Exception {
168: CompilationUnit[] units = new CompilationUnit[streams.size()];
169: int i = 0;
170: for (CtFile stream : streams) {
171: InputStream in = stream.getContent();
172: units[i] = new CompilationUnit(Util
173: .getInputStreamAsCharArray(in, -1, null), stream
174: .getPath(), null);
175: in.close();
176: i++;
177: }
178: return units;
179: }
180:
181: INameEnvironment environment = null;
182:
183: public void setEnvironment(INameEnvironment environment) {
184: this .environment = environment;
185: }
186:
187: public CompilationUnitDeclaration[] getUnits(List<CtFile> streams,
188: Factory f) throws Exception {
189: this .startTime = System.currentTimeMillis();
190: INameEnvironment environment = this .environment;
191: if (environment == null)
192: environment = getLibraryAccess();
193: this .batchCompiler = new Compiler(environment,
194: getHandlingPolicy(), this .options, this ,
195: getProblemFactory(), this .out, false);
196: return batchCompiler.compileUnits(getCompilationUnits(streams));
197: }
198:
199: List<CategorizedProblem[]> probs;
200:
201: public List<CategorizedProblem[]> getProbs() {
202: if (probs == null) {
203: probs = new ArrayList<CategorizedProblem[]>();
204:
205: }
206: return probs;
207: }
208:
209: public void acceptResult(CompilationResult result) {
210: if (result.hasErrors()) {
211: System.err.println(result);
212: getProbs().add(result.problems);
213: success = false;
214: }
215: }
216: }
|