import java.util.ServiceLoader;
import java.util.logging.Level;
+import javax.swing.SwingUtilities;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.FileObject;
* @return an integer corresponding to the compiled and loaded class.
*/
public static int compileCode(String className, String[] code) throws ScilabJavaException {
- findCompiler();
+ if (SwingUtilities.isEventDispatchThread()) {
+ findCompiler();
+ } else {
+ try {
+ SwingUtilities.invokeAndWait(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ findCompiler();
+ } catch (ScilabJavaException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ });
+ } catch (InvocationTargetException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
int cpt = 1;
buffer.append("----------\n");
for (Diagnostic <? extends JavaFileObject > d : diagnostics.getDiagnostics()) {
- buffer.append(Integer.toString(cpt++)).append(". ").append(d.getKind()).append(" in ").append(d.getSource().toUri().getPath()).append(" (at line ").append(Long.toString(d.getLineNumber())).append(")\n");
-
- Reader reader = null;
- try {
- reader = d.getSource().openReader(true);
- reader.skip(d.getStartPosition());
- char[] data = new char[(int) (d.getEndPosition() - d.getStartPosition() + 1)];
- reader.read(data);
- buffer.append(" ").append(data).append('\n');
- Arrays.fill(data, '^');
- buffer.append(" ").append(data).append('\n');
- } catch (IOException e) {
-
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e) { }
+ buffer.append(Integer.toString(cpt++)).append(". ").append(d.getKind());
+ if (d.getSource() != null) {
+ buffer.append(" in ").append(d.getSource().toUri().getPath()).append(" (at line ").append(Long.toString(d.getLineNumber())).append(")\n");
+
+ Reader reader = null;
+ try {
+ reader = d.getSource().openReader(true);
+ reader.skip(d.getStartPosition());
+ char[] data = new char[(int) (d.getEndPosition() - d.getStartPosition() + 1)];
+ reader.read(data);
+ buffer.append(" ").append(data).append('\n');
+ Arrays.fill(data, '^');
+ buffer.append(" ").append(data).append('\n');
+ } catch (IOException e) {
+
+ } finally {
+ if (reader != null) {
+ try {
+ reader.close();
+ } catch (IOException e) { }
+ }
}
+ } else {
+ // this is not a file related error
+ buffer.append('\n');
}
-
buffer.append(d.getMessage(Locale.getDefault())).append('\n');
}