int ScilabJavaEnvironment::compilecode(char * className, char ** code, int size)
{
JavaVM *vm = getScilabJavaVM();
- const int ret = ScilabJavaCompiler::compileCode(vm, className, code, size);
+ try
+ {
+ const int ret = ScilabJavaCompiler::compileCode(vm, className, code, size);
- if (ret != 0 && ret != -1)
+ if (ret != 0 && ret != -1)
+ {
+ ScilabAutoCleaner::registerVariable(envId, ret);
+ }
+
+ return ret;
+ }
+ catch (const GiwsException::JniException & e)
{
- ScilabAutoCleaner::registerVariable(envId, ret);
+ throw ScilabJavaException(__LINE__, __FILE__, gettext("Cannot compile the code:\n%s"), e.getJavaDescription().c_str());
}
-
- return ret;
}
void ScilabJavaEnvironment::enabletrace(const char * filename)
#include <sstream>
#include <string>
-#define BUFFER_SIZE 1024
+#define EOJ_EXCEPTION_BUFFER_SIZE 4096
using namespace org_modules_external_objects;
ScilabJavaException(const int _line, const char * _file, const char * _message, ...) : message(""), file(_file), line(_line)
{
- char str[BUFFER_SIZE];
+ char str[EOJ_EXCEPTION_BUFFER_SIZE];
va_list args;
va_start(args, _message);
- vsnprintf(str, BUFFER_SIZE, _message, args);
+ vsnprintf(str, EOJ_EXCEPTION_BUFFER_SIZE, _message, args);
va_end(args);
message = getDescription(std::string(str));
ScilabJavaException(const int _line, const char * _file, std::string _message, ...) : message(""), file(_file), line(_line)
{
- char str[BUFFER_SIZE];
+ char str[EOJ_EXCEPTION_BUFFER_SIZE];
va_list args;
va_start(args, _message);
- vsnprintf(str, BUFFER_SIZE, _message.c_str(), args);
+ vsnprintf(str, EOJ_EXCEPTION_BUFFER_SIZE, _message.c_str(), args);
va_end(args);
message = getDescription(std::string(str));
package org.scilab.modules.external_objects_java;
+import java.io.BufferedWriter;
+import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.io.Reader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
private static JavaCompiler compiler;
private static boolean ecjLoaded = false;
+ private static boolean isECJ;
+
+
static {
new File(System.getProperty("java.io.tmpdir") + File.separator + "JIMS").mkdir();
new File(BINPATH).mkdir();
ecjLoaded = true;
findCompiler();
}
+
+ isECJ = compiler.getClass().getSimpleName().indexOf("Eclipse") != -1;
}
}
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
String cp = null;
- if (compiler.getClass().getSimpleName().indexOf("Eclipse") != -1) {
+ if (isECJ) {
// it seems that with the embedded ecj, the only way to set the cp is to use java.class.path...
cp = getClasspath();
System.setProperty("java.class.path", cp + File.pathSeparatorChar + System.getProperty("java.class.path"));
ClassFileManager manager = new ClassFileManager(stdFileManager);
List<SimpleJavaFileObject> compilationUnits = new ArrayList<SimpleJavaFileObject>();
+ CharArrayWriter caw = new CharArrayWriter();
+ BufferedWriter out = new BufferedWriter(caw);
boolean isFile = true;
SourceString sourceString = null;
for (String s : code) {
String[] compileOptions = new String[] {"-d", BINPATH};
Iterable<String> options = Arrays.asList(compileOptions);
- CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, compilationUnits);
+ CompilationTask task = compiler.getTask(out, manager, diagnostics, options, null, compilationUnits);
boolean success = task.call();
if (cp != null) {
System.setProperty("java.class.path", s);
}
+ String error = "";
+
+ try {
+ out.flush();
+ error = caw.toString();
+ } catch (IOException e) {
+
+ } finally {
+ try {
+ out.close();
+ } catch (IOException e) { }
+ }
+
if (success) {
if (isFile) {
return -1;
return ScilabClassLoader.loadJavaClass(manager.className, true);
}
} else {
- StringBuffer buf = new StringBuffer();
- for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
- buf.append(diagnostic.toString());
- buf.append("\n");
+ if (!isECJ) {
+ error = getCompilerErrors(diagnostics);
+ }
+
+ throw new ScilabJavaException(error);
+ }
+ }
+
+ /**
+ * Returns the compilation errors from the diagnostics
+ * @param diagnostics the diagnostics returned by the compiler
+ * @return a string containing the errors
+ */
+ public static String getCompilerErrors(DiagnosticCollector<JavaFileObject> diagnostics) {
+ StringBuffer buffer = new StringBuffer();
+ 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) { }
+ }
}
- throw new ScilabJavaException(buf.toString());
+ buffer.append(d.getMessage(Locale.getDefault())).append('\n');
}
+
+ buffer.append("----------\n");
+ return buffer.toString();
}
/**