63 * |
63 * |
64 * @author Peter von der Ahé |
64 * @author Peter von der Ahé |
65 * @author Jonathan Gibbons |
65 * @author Jonathan Gibbons |
66 */ |
66 */ |
67 public class JavacTaskImpl extends JavacTask { |
67 public class JavacTaskImpl extends JavacTask { |
68 private JavacTool tool; |
68 private ClientCodeWrapper ccw; |
69 private Main compilerMain; |
69 private Main compilerMain; |
70 private JavaCompiler compiler; |
70 private JavaCompiler compiler; |
71 private Locale locale; |
71 private Locale locale; |
72 private String[] args; |
72 private String[] args; |
73 private Context context; |
73 private Context context; |
78 private AtomicBoolean used = new AtomicBoolean(); |
78 private AtomicBoolean used = new AtomicBoolean(); |
79 private Iterable<? extends Processor> processors; |
79 private Iterable<? extends Processor> processors; |
80 |
80 |
81 private Integer result = null; |
81 private Integer result = null; |
82 |
82 |
83 JavacTaskImpl(JavacTool tool, |
83 JavacTaskImpl(Main compilerMain, |
84 Main compilerMain, |
|
85 String[] args, |
84 String[] args, |
86 Context context, |
85 Context context, |
87 List<JavaFileObject> fileObjects) { |
86 List<JavaFileObject> fileObjects) { |
88 this.tool = tool; |
87 this.ccw = ClientCodeWrapper.instance(context); |
89 this.compilerMain = compilerMain; |
88 this.compilerMain = compilerMain; |
90 this.args = args; |
89 this.args = args; |
91 this.context = context; |
90 this.context = context; |
92 this.fileObjects = fileObjects; |
91 this.fileObjects = fileObjects; |
93 setLocale(Locale.getDefault()); |
92 setLocale(Locale.getDefault()); |
94 // null checks |
93 // null checks |
95 compilerMain.getClass(); |
94 compilerMain.getClass(); |
96 args.getClass(); |
95 args.getClass(); |
97 context.getClass(); |
|
98 fileObjects.getClass(); |
96 fileObjects.getClass(); |
99 } |
97 } |
100 |
98 |
101 JavacTaskImpl(JavacTool tool, |
99 JavacTaskImpl(Main compilerMain, |
102 Main compilerMain, |
|
103 Iterable<String> flags, |
100 Iterable<String> flags, |
104 Context context, |
101 Context context, |
105 Iterable<String> classes, |
102 Iterable<String> classes, |
106 Iterable<? extends JavaFileObject> fileObjects) { |
103 Iterable<? extends JavaFileObject> fileObjects) { |
107 this(tool, compilerMain, toArray(flags, classes), context, toList(fileObjects)); |
104 this(compilerMain, toArray(flags, classes), context, toList(fileObjects)); |
108 } |
105 } |
109 |
106 |
110 static private String[] toArray(Iterable<String> flags, Iterable<String> classes) { |
107 static private String[] toArray(Iterable<String> flags, Iterable<String> classes) { |
111 ListBuffer<String> result = new ListBuffer<String>(); |
108 ListBuffer<String> result = new ListBuffer<String>(); |
112 if (flags != null) |
109 if (flags != null) |
129 |
126 |
130 public Boolean call() { |
127 public Boolean call() { |
131 if (!used.getAndSet(true)) { |
128 if (!used.getAndSet(true)) { |
132 initContext(); |
129 initContext(); |
133 notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>(); |
130 notYetEntered = new HashMap<JavaFileObject, JCCompilationUnit>(); |
134 compilerMain.setFatalErrors(true); |
131 compilerMain.setAPIMode(true); |
135 result = compilerMain.compile(args, context, fileObjects, processors); |
132 result = compilerMain.compile(args, context, fileObjects, processors); |
136 cleanup(); |
133 cleanup(); |
137 return result == 0; |
134 return result == 0; |
138 } else { |
135 } else { |
139 throw new IllegalStateException("multiple calls to method 'call'"); |
136 throw new IllegalStateException("multiple calls to method 'call'"); |
183 private void initContext() { |
180 private void initContext() { |
184 context.put(JavacTaskImpl.class, this); |
181 context.put(JavacTaskImpl.class, this); |
185 if (context.get(TaskListener.class) != null) |
182 if (context.get(TaskListener.class) != null) |
186 context.put(TaskListener.class, (TaskListener)null); |
183 context.put(TaskListener.class, (TaskListener)null); |
187 if (taskListener != null) |
184 if (taskListener != null) |
188 context.put(TaskListener.class, wrap(taskListener)); |
185 context.put(TaskListener.class, ccw.wrap(taskListener)); |
189 //initialize compiler's default locale |
186 //initialize compiler's default locale |
190 context.put(Locale.class, locale); |
187 context.put(Locale.class, locale); |
191 } |
|
192 // where |
|
193 private TaskListener wrap(final TaskListener tl) { |
|
194 tl.getClass(); // null check |
|
195 return new TaskListener() { |
|
196 public void started(TaskEvent e) { |
|
197 try { |
|
198 tl.started(e); |
|
199 } catch (Throwable t) { |
|
200 throw new ClientCodeException(t); |
|
201 } |
|
202 } |
|
203 |
|
204 public void finished(TaskEvent e) { |
|
205 try { |
|
206 tl.finished(e); |
|
207 } catch (Throwable t) { |
|
208 throw new ClientCodeException(t); |
|
209 } |
|
210 } |
|
211 |
|
212 }; |
|
213 } |
188 } |
214 |
189 |
215 void cleanup() { |
190 void cleanup() { |
216 if (compiler != null) |
191 if (compiler != null) |
217 compiler.close(); |
192 compiler.close(); |