2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.compiler
.impl
.javaCompiler
.api
;
18 import com
.intellij
.compiler
.OutputParser
;
19 import com
.sun
.source
.util
.*;
20 import com
.sun
.tools
.javac
.api
.JavacTool
;
21 import org
.jetbrains
.annotations
.NotNull
;
25 import java
.io
.PrintWriter
;
27 import java
.util
.List
;
28 import java
.util
.concurrent
.BlockingQueue
;
29 import java
.util
.concurrent
.LinkedBlockingQueue
;
34 @SuppressWarnings({"Since15"})
35 public class CompAPIDriver
{
36 private final BlockingQueue
<CompilationEvent
> myCompilationResults
= new LinkedBlockingQueue
<CompilationEvent
>();
37 private static final CompilationEvent GUARD
= new CompilationEvent() {
39 protected void process(OutputParser
.Callback callback
) {
43 public String
toString() {
47 private String myOutputDir
;
49 private volatile boolean compiling
;
50 private static final PrintWriter COMPILER_ERRORS
= new PrintWriter(System
.err
);
52 public CompAPIDriver() {
55 public void compile(List
<String
> commandLine
, List
<File
> paths
, final String outputDir
) {
56 myOutputDir
= outputDir
;
59 assert myCompilationResults
.isEmpty();
60 JavaCompiler compiler
= JavacTool
.create(); //use current classloader
61 MyFileManager manager
= new MyFileManager(this, outputDir
);
63 Iterable
<?
extends JavaFileObject
> input
= manager
.getJavaFileObjectsFromFiles(paths
);
65 DiagnosticListener
<JavaFileObject
> listener
= new DiagnosticListener
<JavaFileObject
>() {
66 public void report(Diagnostic
<?
extends JavaFileObject
> diagnostic
) {
67 CompilationEvent event
= CompilationEvent
.diagnostic(diagnostic
);
68 myCompilationResults
.offer(event
);
72 JavaCompiler
.CompilationTask task
= compiler
.getTask(COMPILER_ERRORS
, manager
, listener
, commandLine
, null, input
);
73 ((JavacTask
)task
).setTaskListener(new TaskListener() {
74 public void started(TaskEvent taskEvent
) {
75 JavaFileObject sourceFile
= taskEvent
.getSourceFile();
76 CompilationEvent event
;
77 switch (taskEvent
.getKind()) {
79 event
= CompilationEvent
.progress("Analyzing ",sourceFile
);
82 event
= CompilationEvent
.progress("Parsing ", sourceFile
);
88 myCompilationResults
.offer(event
);
91 public void finished(TaskEvent taskEvent
) {
92 CompilationEvent event
;
93 switch (taskEvent
.getKind()) {
95 event
= CompilationEvent
.fileProcessed();
101 myCompilationResults
.offer(event
);
109 myCompilationResults
.offer(GUARD
);
113 private volatile boolean processing
;
114 public void processAll(@NotNull OutputParser
.Callback callback
) {
118 CompilationEvent event
= myCompilationResults
.take();
119 if (event
== GUARD
) break;
120 event
.process(callback
);
123 catch (InterruptedException ignored
) {
130 public void finish() {
131 assert !compiling
: "still compiling to "+myOutputDir
;
133 //assert myCompilationResults.isEmpty() : myCompilationResults;
134 myCompilationResults
.clear();
137 public void offerClassFile(URI uri
, byte[] bytes
) {
138 CompilationEvent event
= CompilationEvent
.generateClass(uri
, bytes
);
139 myCompilationResults
.offer(event
);