do not use VFS to read .class file content,
[fedora-idea.git] / java / compiler / impl / src / com / intellij / compiler / impl / javaCompiler / api / CompAPIDriver.java
blobbe4f6f1703203eba412ec380d44c1bbf330a369d
1 /*
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;
23 import javax.tools.*;
24 import java.io.File;
25 import java.io.PrintWriter;
26 import java.net.URI;
27 import java.util.List;
28 import java.util.concurrent.BlockingQueue;
29 import java.util.concurrent.LinkedBlockingQueue;
31 /**
32 * @author cdr
34 @SuppressWarnings({"Since15"})
35 public class CompAPIDriver {
36 private final BlockingQueue<CompilationEvent> myCompilationResults = new LinkedBlockingQueue<CompilationEvent>();
37 private static final CompilationEvent GUARD = new CompilationEvent() {
38 @Override
39 protected void process(OutputParser.Callback callback) {
42 @Override
43 public String toString() {
44 return "FINISH";
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;
57 compiling = true;
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);
71 try {
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()) {
78 case ANALYZE:
79 event = CompilationEvent.progress("Analyzing ",sourceFile);
80 break;
81 case PARSE:
82 event = CompilationEvent.progress("Parsing ", sourceFile);
83 break;
84 default:
85 event = null;
87 if (event != null) {
88 myCompilationResults.offer(event);
91 public void finished(TaskEvent taskEvent) {
92 CompilationEvent event;
93 switch (taskEvent.getKind()) {
94 case ENTER:
95 event = CompilationEvent.fileProcessed();
96 break;
97 default:
98 event = null;
100 if (event != null) {
101 myCompilationResults.offer(event);
105 task.call();
107 finally {
108 compiling = false;
109 myCompilationResults.offer(GUARD);
113 private volatile boolean processing;
114 public void processAll(@NotNull OutputParser.Callback callback) {
115 try {
116 processing = true;
117 while (true) {
118 CompilationEvent event = myCompilationResults.take();
119 if (event == GUARD) break;
120 event.process(callback);
123 catch (InterruptedException ignored) {
125 finally {
126 processing = false;
130 public void finish() {
131 assert !compiling : "still compiling to "+myOutputDir;
132 assert !processing;
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);