1 11 package org.eclipse.jdt.internal.compiler.tool; 12 13 import java.io.File ; 14 import java.io.InputStream ; 15 import java.io.OutputStream ; 16 import java.io.OutputStreamWriter ; 17 import java.io.PrintWriter ; 18 import java.io.Writer ; 19 import java.nio.charset.Charset ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.EnumSet ; 23 import java.util.Iterator ; 24 import java.util.Locale ; 25 import java.util.Set ; 26 import java.util.WeakHashMap ; 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 44 public class EclipseCompiler implements JavaCompiler { 45 46 private static Set <SourceVersion> SupportedSourceVersions; 47 static { 48 EnumSet <SourceVersion> enumSet = EnumSet.range(SourceVersion.RELEASE_0, SourceVersion.RELEASE_6); 52 SupportedSourceVersions = Collections.unmodifiableSet(enumSet); 54 } 55 56 WeakHashMap <Thread , EclipseCompilerImpl> threadCache; 57 public DiagnosticListener<? super JavaFileObject> diagnosticListener; 58 59 public EclipseCompiler() { 60 this.threadCache = new WeakHashMap <Thread , EclipseCompilerImpl>(); 61 } 62 67 public Set <SourceVersion> getSourceVersions() { 68 return SupportedSourceVersions; 69 } 70 76 public StandardJavaFileManager getStandardFileManager(DiagnosticListener<? super JavaFileObject> diagnosticListener, 77 Locale locale, 78 Charset charset) { 79 this.diagnosticListener = diagnosticListener; 80 return new EclipseFileManager(locale, charset); 81 } 82 89 @SuppressWarnings ("unchecked") 90 public CompilationTask getTask(Writer out, 91 JavaFileManager fileManager, 92 DiagnosticListener<? super JavaFileObject> diagnosticListener, 93 Iterable <String > options, 94 Iterable <String > classes, 95 Iterable <? extends JavaFileObject> compilationUnits) { 96 97 PrintWriter writerOut = null; 98 PrintWriter writerErr = null; 99 if (out == null) { 100 writerOut = new PrintWriter (System.out); 101 writerErr = new PrintWriter (System.err); 102 } else { 103 writerOut = new PrintWriter (out); 104 writerErr = new PrintWriter (out); 105 } 106 final Thread 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 <String > allOptions = new ArrayList <String >(); 128 if (options != null) { 129 for (Iterator <String > iterator = options.iterator(); iterator.hasNext(); ) { 130 eclipseCompiler2.fileManager.handleOption(iterator.next(), iterator); 131 } 132 for (String option : options) { 133 allOptions.add(option); 134 } 135 } 136 137 if (compilationUnits != null) { 138 for (JavaFileObject javaFileObject : compilationUnits) { 139 allOptions.add(new File (javaFileObject.toUri()).getAbsolutePath()); 140 } 141 } 142 143 if (classes != null) { 144 allOptions.add("-classNames"); StringBuilder builder = new StringBuilder (); 146 int i = 0; 147 for (String 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 [] optionsToProcess = new String [allOptions.size()]; 158 allOptions.toArray(optionsToProcess); 159 try { 160 eclipseCompiler2.configure(optionsToProcess); 161 } catch (InvalidInputException e) { 162 throw new RuntimeException (e); 163 } 164 165 if (eclipseCompiler2.fileManager instanceof StandardJavaFileManager) { 166 StandardJavaFileManager javaFileManager = (StandardJavaFileManager) eclipseCompiler2.fileManager; 167 168 Iterable <? extends File > 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 call() { 177 if (this.hasRun) { 179 throw new IllegalStateException ("This task has already been run"); } 181 Boolean value = eclipseCompiler2.call() ? Boolean.TRUE : Boolean.FALSE; 182 this.hasRun = true; 183 return value; 184 } 185 public void setLocale(Locale locale) { 186 eclipseCompiler2.setLocale(locale); 187 } 188 public void setProcessors(Iterable <? extends Processor> processors) { 189 ArrayList <Processor> temp = new ArrayList <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 204 public int isSupportedOption(String option) { 205 return Options.processOptions(option); 206 } 207 208 214 public int run(InputStream in, OutputStream out, OutputStream err, String ... arguments) { 215 boolean succeed = new Main(new PrintWriter (new OutputStreamWriter (out)), new PrintWriter (new OutputStreamWriter (err)), true).compile(arguments); 216 return succeed ? 0 : -1; 217 } 218 } 219 | Popular Tags |