2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - 2011 - Calixte DENIZET <calixte@contrib.scilab.org>
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
16 package org.scilab.modules.external_objects_java;
18 import java.io.BufferedWriter;
19 import java.io.CharArrayWriter;
21 import java.io.FileReader;
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.io.StringWriter;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.net.MalformedURLException;
29 import java.net.URLClassLoader;
31 import java.util.Arrays;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.ServiceLoader;
36 import java.util.logging.Level;
38 import javax.swing.SwingUtilities;
39 import javax.tools.Diagnostic;
40 import javax.tools.DiagnosticCollector;
41 import javax.tools.FileObject;
42 import javax.tools.ForwardingJavaFileManager;
43 import javax.tools.JavaCompiler;
44 import javax.tools.StandardLocation;
45 import javax.tools.JavaFileObject;
46 import javax.tools.SimpleJavaFileObject;
47 import javax.tools.StandardJavaFileManager;
48 import javax.tools.ToolProvider;
49 import javax.tools.JavaCompiler.CompilationTask;
50 import javax.tools.JavaFileObject.Kind;
52 import org.scilab.modules.commons.ScilabCommonsUtils;
55 * Class to provide a java compiler to JIMS.
56 * Try to find the compiler provide with JDK and if it is not found, use the Eclipse Compiler for Java
57 * @author Calixte DENIZET
59 @SuppressWarnings("serial")
60 public class ScilabJavaCompiler {
62 private static final String JAVACOMPILER = "javax.tools.JavaCompiler";
63 private static final String BINPATH = System.getProperty("java.io.tmpdir") + File.separator + "JIMS" + File.separator + "bin";
65 private static JavaCompiler compiler;
66 private static boolean ecjLoaded = false;
68 private static boolean isECJ;
72 new File(System.getProperty("java.io.tmpdir") + File.separator + "JIMS").mkdir();
73 new File(BINPATH).mkdir();
75 URL binURL = new File(BINPATH).toURI().toURL();
76 addURLToClassPath(binURL);
77 } catch (MalformedURLException e) {
78 System.err.println(e);
83 * Just find a compiler
85 private static void findCompiler() throws ScilabJavaException {
86 if (compiler == null) {
88 compiler = ToolProvider.getSystemJavaCompiler();
89 } catch (Exception e) { }
91 if (compiler == null) {
92 ServiceLoader<JavaCompiler> jcompilers = ServiceLoader.load(JavaCompiler.class);
93 for (JavaCompiler jc : jcompilers) {
101 if (compiler == null) {
103 throw new ScilabJavaException("No java compiler in the classpath\nCheck for tools.jar (comes from JDK) or ecj-4.4.x.jar (Eclipse Compiler for Java)");
106 // Compiler should be in thirdparty so we load it
107 ScilabCommonsUtils.loadOnUse("external_objects_java");
112 isECJ = compiler.getClass().getSimpleName().indexOf("Eclipse") != -1;
117 * Compile code got as string
118 * @param className the class name
119 * @param code the lines giving the code to compile
120 * @return an integer corresponding to the compiled and loaded class.
122 public static int compileCode(String className, String[] code) throws ScilabJavaException {
123 if (SwingUtilities.isEventDispatchThread()) {
127 SwingUtilities.invokeAndWait(new Runnable() {
132 } catch (ScilabJavaException e) {
133 // TODO Auto-generated catch block
138 } catch (InvocationTargetException e) {
139 // TODO Auto-generated catch block
141 } catch (InterruptedException e) {
142 // TODO Auto-generated catch block
147 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
148 StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
152 // it seems that with the embedded ecj, the only way to set the cp is to use java.class.path...
154 System.setProperty("java.class.path", cp + File.pathSeparatorChar + System.getProperty("java.class.path"));
157 stdFileManager.setLocation(StandardLocation.CLASS_PATH, getClasspathFiles());
158 } catch (Exception e) { }
161 ClassFileManager manager = new ClassFileManager(stdFileManager);
162 List<SimpleJavaFileObject> compilationUnits = new ArrayList<SimpleJavaFileObject>();
163 CharArrayWriter caw = new CharArrayWriter();
164 BufferedWriter out = new BufferedWriter(caw);
165 boolean isFile = true;
166 SourceString sourceString = null;
167 for (String s : code) {
168 File f = new File(s);
169 if (!f.exists() || !f.canRead()) {
176 for (String s : code) {
177 File f = new File(s);
178 compilationUnits.add(new SourceFile(f));
181 sourceString = new SourceString(className, code);
182 compilationUnits.add(sourceString);
185 String[] compileOptions = new String[] {"-d", BINPATH};
186 Iterable<String> options = Arrays.asList(compileOptions);
188 CompilationTask task = compiler.getTask(out, manager, diagnostics, options, null, compilationUnits);
189 boolean success = task.call();
192 final String s = System.getProperty("java.class.path").replace(cp + File.pathSeparatorChar, "");
193 System.setProperty("java.class.path", s);
200 error = caw.toString();
201 } catch (IOException e) {
206 } catch (IOException e) { }
213 if (manager.className != null && !manager.className.isEmpty()) {
214 return ScilabClassLoader.loadJavaClass(BINPATH, manager.className);
221 error = getCompilerErrors(diagnostics);
224 throw new ScilabJavaException(error);
229 * Returns the compilation errors from the diagnostics
230 * @param diagnostics the diagnostics returned by the compiler
231 * @return a string containing the errors
233 public static String getCompilerErrors(DiagnosticCollector<JavaFileObject> diagnostics) {
234 StringBuffer buffer = new StringBuffer();
236 buffer.append("----------\n");
237 for (Diagnostic <? extends JavaFileObject > d : diagnostics.getDiagnostics()) {
238 buffer.append(Integer.toString(cpt++)).append(". ").append(d.getKind());
239 if (d.getSource() != null) {
240 buffer.append(" in ").append(d.getSource().toUri().getPath()).append(" (at line ").append(Long.toString(d.getLineNumber())).append(")\n");
242 Reader reader = null;
244 reader = d.getSource().openReader(true);
245 reader.skip(d.getStartPosition());
246 char[] data = new char[(int) (d.getEndPosition() - d.getStartPosition() + 1)];
248 buffer.append(" ").append(data).append('\n');
249 Arrays.fill(data, '^');
250 buffer.append(" ").append(data).append('\n');
251 } catch (IOException e) {
254 if (reader != null) {
257 } catch (IOException e) { }
261 // this is not a file related error
264 buffer.append(d.getMessage(Locale.getDefault())).append('\n');
267 buffer.append("----------\n");
268 return buffer.toString();
272 * Get the current classpath with the correct separator (':' under Unix and ';' under Windows)
273 * @return the classpath
275 public static String getClasspath() {
276 URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
277 URL[] urls = loader.getURLs();
278 StringBuffer buffer = new StringBuffer();
280 for (URL url : urls) {
281 buffer.append(url.getPath()).append(File.pathSeparatorChar);
285 return buffer.toString();
289 * Get the files in the classpath
292 public static List<File> getClasspathFiles() {
293 URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
294 URL[] urls = loader.getURLs();
295 List<File> files = new ArrayList<File>(urls.length);
297 for (URL url : urls) {
299 files.add(new File(url.toURI()));
300 } catch (Exception e) { }
307 * Add a class in the classpath
308 * @param url the class url
310 public static void addURLToClassPath(URL url) {
311 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
313 final Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] {URL.class});
314 method.setAccessible(true);
315 method.invoke(sysloader , new Object[] {url});
316 } catch (NoSuchMethodException e) {
317 System.err.println("Error: Cannot find the declared method: " + e.getLocalizedMessage());
318 } catch (IllegalAccessException e) {
319 System.err.println("Error: Illegal access: " + e.getLocalizedMessage());
320 } catch (InvocationTargetException e) {
321 System.err.println("Error: Could not invocate target: " + e.getLocalizedMessage());
326 * Inner class to handle String as File
328 private static class SourceString extends SimpleJavaFileObject {
330 private final String code;
332 private SourceString(String className, String[] code) {
333 super(new File(BINPATH + "/" + className.replace('.', '/') + Kind.SOURCE.extension).toURI(), Kind.SOURCE);
335 StringBuffer buf = new StringBuffer();
336 for (String str : code) {
340 this.code = buf.toString();
343 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
349 * Inner class to handle String as File
351 private static class SourceFile extends SimpleJavaFileObject {
355 private SourceFile(File f) {
356 super(f.toURI(), Kind.SOURCE);
360 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
362 FileReader reader = new FileReader(f);
363 char[] buffer = new char[1024];
364 StringBuffer sb = new StringBuffer();
367 while ((r = reader.read(buffer, 0, 1024)) != -1) {
368 sb.append(buffer, 0, r);
374 } catch (Exception e) {
380 private static class ClassFileManager extends ForwardingJavaFileManager {
384 public ClassFileManager(StandardJavaFileManager standardManager) {
385 super(standardManager);
389 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
390 if (sibling instanceof SourceString && className != null && className.indexOf('$') == -1) {
391 this.className = className.replace('/', '.');
394 if (ScilabJavaObject.debug) {
395 ScilabJavaObject.logger.log(Level.INFO, "Compilation of class \'" + className + "\'");
398 return super.getJavaFileForOutput(location, className, kind, sibling);