KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > tool > EclipseCompiler


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.tool;
12
13 import java.io.File JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.OutputStreamWriter JavaDoc;
17 import java.io.PrintWriter JavaDoc;
18 import java.io.Writer JavaDoc;
19 import java.nio.charset.Charset JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.EnumSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Locale JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.WeakHashMap JavaDoc;
27
28 import javax.annotation.processing.Processor;
29 import javax.lang.model.SourceVersion;
30 import javax.tools.DiagnosticListener;
31 import javax.tools.JavaCompiler;
32 import javax.tools.JavaFileManager;
33 import javax.tools.JavaFileObject;
34 import javax.tools.StandardJavaFileManager;
35 import javax.tools.StandardLocation;
36
37 import org.eclipse.jdt.core.compiler.InvalidInputException;
38 import org.eclipse.jdt.internal.compiler.batch.Main;
39 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
40
41 /**
42  * Implementation of a batch compiler that supports the jsr199
43  */

44 public class EclipseCompiler implements JavaCompiler {
45
46     private static Set JavaDoc<SourceVersion> SupportedSourceVersions;
47     static {
48         // Eclipse compiler supports all possible versions from version 0 to
49
// version 6
50
// we don't care about the order
51
EnumSet JavaDoc<SourceVersion> enumSet = EnumSet.range(SourceVersion.RELEASE_0, SourceVersion.RELEASE_6);
52         // we don't want anybody to modify this list
53
SupportedSourceVersions = Collections.unmodifiableSet(enumSet);
54     }
55
56     WeakHashMap JavaDoc<Thread JavaDoc, EclipseCompilerImpl> threadCache;
57     public DiagnosticListener<? super JavaFileObject> diagnosticListener;
58
59     public EclipseCompiler() {
60         this.threadCache = new WeakHashMap JavaDoc<Thread JavaDoc, EclipseCompilerImpl>();
61     }
62     /*
63      * (non-Javadoc)
64      *
65      * @see javax.tools.Tool#getSourceVersions()
66      */

67     public Set JavaDoc<SourceVersion> getSourceVersions() {
68         return SupportedSourceVersions;
69     }
70     /*
71      * (non-Javadoc)
72      *
73      * @see javax.tools.JavaCompiler#getStandardFileManager(javax.tools.DiagnosticListener,
74      * java.util.Locale, java.nio.charset.Charset)
75      */

76     public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener,
77             Locale JavaDoc locale,
78             Charset JavaDoc charset) {
79         this.diagnosticListener = diagnosticListener;
80         return new EclipseFileManager(locale, charset);
81     }
82     /*
83      * (non-Javadoc)
84      *
85      * @see javax.tools.JavaCompiler#getTask(java.io.Writer,
86      * javax.tools.JavaFileManager, javax.tools.DiagnosticListener,
87      * java.lang.Iterable, java.lang.Iterable, java.lang.Iterable)
88      */

89     @SuppressWarnings JavaDoc("unchecked")
90     public CompilationTask getTask(Writer JavaDoc out,
91             JavaFileManager fileManager,
92             DiagnosticListener<? super JavaFileObject> diagnosticListener,
93             Iterable JavaDoc<String JavaDoc> options,
94             Iterable JavaDoc<String JavaDoc> classes,
95             Iterable JavaDoc<? extends JavaFileObject> compilationUnits) {
96
97         PrintWriter JavaDoc writerOut = null;
98         PrintWriter JavaDoc writerErr = null;
99         if (out == null) {
100             writerOut = new PrintWriter JavaDoc(System.out);
101             writerErr = new PrintWriter JavaDoc(System.err);
102         } else {
103             writerOut = new PrintWriter JavaDoc(out);
104             writerErr = new PrintWriter JavaDoc(out);
105         }
106         final Thread JavaDoc currentThread = Thread.currentThread();
107         EclipseCompilerImpl eclipseCompiler = this.threadCache.get(currentThread);
108         if (eclipseCompiler == null) {
109             eclipseCompiler = new EclipseCompilerImpl(writerOut, writerErr, false);
110             this.threadCache.put(currentThread, eclipseCompiler);
111         } else {
112             eclipseCompiler.initialize(writerOut, writerErr, false, null);
113         }
114         final EclipseCompilerImpl eclipseCompiler2 = new EclipseCompilerImpl(writerOut, writerErr, false);
115         eclipseCompiler2.compilationUnits = compilationUnits;
116         eclipseCompiler2.diagnosticListener = diagnosticListener;
117         if (fileManager != null) {
118             eclipseCompiler2.fileManager = fileManager;
119         } else {
120             eclipseCompiler2.fileManager = this.getStandardFileManager(diagnosticListener, null, null);
121         }
122
123         eclipseCompiler2.options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
124         eclipseCompiler2.options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
125         eclipseCompiler2.options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
126
127         ArrayList JavaDoc<String JavaDoc> allOptions = new ArrayList JavaDoc<String JavaDoc>();
128         if (options != null) {
129             for (Iterator JavaDoc<String JavaDoc> iterator = options.iterator(); iterator.hasNext(); ) {
130                 eclipseCompiler2.fileManager.handleOption(iterator.next(), iterator);
131             }
132             for (String JavaDoc option : options) {
133                 allOptions.add(option);
134             }
135         }
136
137         if (compilationUnits != null) {
138             for (JavaFileObject javaFileObject : compilationUnits) {
139                 allOptions.add(new File JavaDoc(javaFileObject.toUri()).getAbsolutePath());
140             }
141         }
142
143         if (classes != null) {
144             allOptions.add("-classNames"); //$NON-NLS-1$
145
StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
146             int i = 0;
147             for (String JavaDoc className : classes) {
148                 if (i != 0) {
149                     builder.append(',');
150                 }
151                 builder.append(className);
152                 i++;
153             }
154             allOptions.add(String.valueOf(builder));
155         }
156
157         final String JavaDoc[] optionsToProcess = new String JavaDoc[allOptions.size()];
158         allOptions.toArray(optionsToProcess);
159         try {
160             eclipseCompiler2.configure(optionsToProcess);
161         } catch (InvalidInputException e) {
162             throw new RuntimeException JavaDoc(e);
163         }
164
165         if (eclipseCompiler2.fileManager instanceof StandardJavaFileManager) {
166             StandardJavaFileManager javaFileManager = (StandardJavaFileManager) eclipseCompiler2.fileManager;
167
168             Iterable JavaDoc<? extends File JavaDoc> location = javaFileManager.getLocation(StandardLocation.CLASS_OUTPUT);
169             if (location != null) {
170                 eclipseCompiler2.setDestinationPath(location.iterator().next().getAbsolutePath());
171             }
172         }
173
174         return new CompilationTask() {
175             private boolean hasRun = false;
176             public Boolean JavaDoc call() {
177                 // set up compiler with passed options
178
if (this.hasRun) {
179                     throw new IllegalStateException JavaDoc("This task has already been run"); //$NON-NLS-1$
180
}
181                 Boolean JavaDoc value = eclipseCompiler2.call() ? Boolean.TRUE : Boolean.FALSE;
182                 this.hasRun = true;
183                 return value;
184             }
185             public void setLocale(Locale JavaDoc locale) {
186                 eclipseCompiler2.setLocale(locale);
187             }
188             public void setProcessors(Iterable JavaDoc<? extends Processor> processors) {
189                 ArrayList JavaDoc<Processor> temp = new ArrayList JavaDoc<Processor>();
190                 for (Processor processor : processors) {
191                     temp.add(processor);
192                 }
193                 Processor[] processors2 = new Processor[temp.size()];
194                 temp.toArray(processors2);
195                 eclipseCompiler2.processors = processors2;
196             }
197         };
198     }
199     /*
200      * (non-Javadoc)
201      *
202      * @see javax.tools.OptionChecker#isSupportedOption(java.lang.String)
203      */

204     public int isSupportedOption(String JavaDoc option) {
205         return Options.processOptions(option);
206     }
207
208     /*
209      * (non-Javadoc)
210      *
211      * @see javax.tools.Tool#run(java.io.InputStream, java.io.OutputStream,
212      * java.io.OutputStream, java.lang.String[])
213      */

214     public int run(InputStream JavaDoc in, OutputStream JavaDoc out, OutputStream JavaDoc err, String JavaDoc... arguments) {
215         boolean succeed = new Main(new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(out)), new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(err)), true).compile(arguments);
216         return succeed ? 0 : -1;
217     }
218 }
219
Popular Tags