KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > builder > JDTCompiler


1 package spoon.support.builder;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7
8 import org.eclipse.jdt.internal.compiler.CompilationResult;
9 import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
10 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
11 import org.eclipse.jdt.internal.compiler.IProblemFactory;
12 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
13 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
14 import org.eclipse.jdt.internal.compiler.batch.Main;
15 import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
16 import org.eclipse.jdt.internal.compiler.util.Util;
17
18 import spoon.reflect.Factory;
19
20 public class JDTCompiler extends Main implements ICompilerRequestor {
21
22     class Compiler extends org.eclipse.jdt.internal.compiler.Compiler {
23
24         @SuppressWarnings JavaDoc("deprecation")
25         public Compiler(INameEnvironment environment,
26                 IErrorHandlingPolicy policy, Map JavaDoc settings,
27                 ICompilerRequestor requestor, IProblemFactory problemFactory,
28                 PrintWriter JavaDoc out, boolean statementsRecovery) {
29             super(environment, policy, settings, requestor, problemFactory,
30                     statementsRecovery);
31         }
32
33         public CompilationUnitDeclaration[] compileUnits(
34                 CompilationUnit[] sourceUnits) {
35             CompilationUnitDeclaration unit = null;
36             int i = 0;
37             // build and record parsed units
38
beginToCompile(sourceUnits);
39             // process all units (some more could be injected in the loop by
40
// the lookup environment)
41
for (; i < this.totalUnits; i++) {
42                 unit = unitsToProcess[i];
43                 this.parser.getMethodBodies(unit);
44
45                 // fault in fields & methods
46
if (unit.scope != null)
47                     unit.scope.faultInTypes();
48                 // verify inherited methods
49
if (unit.scope != null)
50                     unit.scope
51                             .verifyMethods(lookupEnvironment.methodVerifier());
52                 // type checking
53
unit.resolve();
54                 // flow analysis
55
unit.analyseCode();
56
57                 requestor.acceptResult(unit.compilationResult.tagAsAccepted());
58             }
59             return this.unitsToProcess;
60         }
61     }
62
63     public static int javaCompilant = 5;
64
65     public static void compileSrc(Factory f, List JavaDoc<CtFile> files)
66             throws Throwable JavaDoc {
67         // Build input
68
List JavaDoc<String JavaDoc> args = new ArrayList JavaDoc<String JavaDoc>();
69         args.add("-1." + javaCompilant);
70         args.add("-preserveAllLocals");
71         args.add("-enableJavadoc");
72         args.add("-nowarn");
73         args.add(".");
74
75         JDTCompiler compiler = new JDTCompiler(new PrintWriter JavaDoc(System.out),
76                 new PrintWriter JavaDoc(System.err));
77         compiler.configure(args.toArray(new String JavaDoc[0]));
78
79         CompilationUnitDeclaration[] units = compiler.getUnits(files);
80
81         JDTTreeBuilder builder = new JDTTreeBuilder(f);
82
83         for (CompilationUnitDeclaration unit : units) {
84             try {
85                 unit.traverse(builder, unit.scope);
86             } catch (Throwable JavaDoc e) {
87                 e.printStackTrace();
88             }
89         }
90     }
91
92     public static void compileTemplate(Factory f, List JavaDoc<CtFile> streams)
93             throws Throwable JavaDoc {
94         // Build input
95
List JavaDoc<String JavaDoc> args = new ArrayList JavaDoc<String JavaDoc>();
96         args.add("-1." + javaCompilant);
97         args.add("-preserveAllLocals");
98         args.add("-enableJavadoc");
99         args.add("-nowarn");
100         args.add(".");
101
102         JDTCompiler compiler = new JDTCompiler(new PrintWriter JavaDoc(System.out),
103                 new PrintWriter JavaDoc(System.err));
104         compiler.configure(args.toArray(new String JavaDoc[0]));
105
106         CompilationUnitDeclaration[] units = compiler.getUnits(streams);
107
108         JDTTreeBuilder builder = new JDTTreeBuilder(f);
109         builder.template = true;
110         for (CompilationUnitDeclaration unit : units) {
111             unit.traverse(builder, unit.scope);
112         }
113     }
114
115     Compiler JavaDoc batchCompiler;
116
117     PrintWriter JavaDoc out;
118
119     public JDTCompiler(PrintWriter JavaDoc outWriter, PrintWriter JavaDoc errWriter) {
120         super(outWriter, errWriter, false);
121     }
122
123     /*
124      * Build the set of compilation source units
125      */

126     public CompilationUnit[] getCompilationUnits(List JavaDoc<CtFile> streams)
127             throws Exception JavaDoc {
128         CompilationUnit[] units = new CompilationUnit[streams.size()];
129         int i = 0;
130         for (CtFile stream : streams) {
131             units[i] = new CompilationUnit(Util.getInputStreamAsCharArray(
132                     stream.getContent(), -1, null), stream.getName(), null);
133             i++;
134         }
135         return units;
136     }
137
138     public CompilationUnitDeclaration[] getUnits(List JavaDoc<CtFile> streams)
139             throws Exception JavaDoc {
140         this.startTime = System.currentTimeMillis();
141         INameEnvironment environment = getLibraryAccess();
142         this.batchCompiler = new Compiler JavaDoc(environment, getHandlingPolicy(),
143                 this.options, this, getProblemFactory(), this.out, false);
144         return batchCompiler.compileUnits(getCompilationUnits(streams));
145     }
146
147     public void acceptResult(CompilationResult result) {
148         if (result.hasErrors()) {
149             System.err.println(result);
150         }
151     }
152 }
153
Popular Tags