2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - 2011 - Calixte DENIZET <calixte@contrib.scilab.org>
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
13 package org.scilab.modules.external_objects_java;
15 import java.io.BufferedWriter;
16 import java.io.CharArrayWriter;
18 import java.io.FileReader;
19 import java.io.IOException;
20 import java.io.Reader;
21 import java.io.StringWriter;
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.net.MalformedURLException;
26 import java.net.URLClassLoader;
28 import java.util.Arrays;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Locale;
32 import java.util.ServiceLoader;
33 import java.util.logging.Level;
35 import javax.swing.SwingUtilities;
36 import javax.tools.Diagnostic;
37 import javax.tools.DiagnosticCollector;
38 import javax.tools.FileObject;
39 import javax.tools.ForwardingJavaFileManager;
40 import javax.tools.JavaCompiler;
41 import javax.tools.StandardLocation;
42 import javax.tools.JavaFileObject;
43 import javax.tools.SimpleJavaFileObject;
44 import javax.tools.StandardJavaFileManager;
45 import javax.tools.ToolProvider;
46 import javax.tools.JavaCompiler.CompilationTask;
47 import javax.tools.JavaFileObject.Kind;
49 import org.scilab.modules.commons.ScilabCommonsUtils;
52 * Class to provide a java compiler to JIMS.
53 * Try to find the compiler provide with JDK and if it is not found, use the Eclipse Compiler for Java
54 * @author Calixte DENIZET
56 @SuppressWarnings("serial")
57 public class ScilabJavaCompiler {
59 private static final String JAVACOMPILER = "javax.tools.JavaCompiler";
60 private static final String BINPATH = System.getProperty("java.io.tmpdir") + File.separator + "JIMS" + File.separator + "bin";
62 private static JavaCompiler compiler;
63 private static boolean ecjLoaded = false;
65 private static boolean isECJ;
69 new File(System.getProperty("java.io.tmpdir") + File.separator + "JIMS").mkdir();
70 new File(BINPATH).mkdir();
72 URL binURL = new File(BINPATH).toURI().toURL();
73 addURLToClassPath(binURL);
74 } catch (MalformedURLException e) {
75 System.err.println(e);
80 * Just find a compiler
82 private static void findCompiler() throws ScilabJavaException {
83 if (compiler == null) {
85 compiler = ToolProvider.getSystemJavaCompiler();
86 } catch (Exception e) { }
88 if (compiler == null) {
89 ServiceLoader<JavaCompiler> jcompilers = ServiceLoader.load(JavaCompiler.class);
90 for (JavaCompiler jc : jcompilers) {
98 if (compiler == null) {
100 throw new ScilabJavaException("No java compiler in the classpath\nCheck for tools.jar (comes from JDK) or ecj-3.6.x.jar (Eclipse Compiler for Java)");
103 // Compiler should be in thirdparty so we load it
104 ScilabCommonsUtils.loadOnUse("external_objects_java");
109 isECJ = compiler.getClass().getSimpleName().indexOf("Eclipse") != -1;
114 * Compile code got as string
115 * @param className the class name
116 * @param code the lines giving the code to compile
117 * @return an integer corresponding to the compiled and loaded class.
119 public static int compileCode(String className, String[] code) throws ScilabJavaException {
120 if (SwingUtilities.isEventDispatchThread()) {
124 SwingUtilities.invokeAndWait(new Runnable() {
129 } catch (ScilabJavaException e) {
130 // TODO Auto-generated catch block
135 } catch (InvocationTargetException e) {
136 // TODO Auto-generated catch block
138 } catch (InterruptedException e) {
139 // TODO Auto-generated catch block
144 DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
145 StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);
149 // it seems that with the embedded ecj, the only way to set the cp is to use java.class.path...
151 System.setProperty("java.class.path", cp + File.pathSeparatorChar + System.getProperty("java.class.path"));
154 stdFileManager.setLocation(StandardLocation.CLASS_PATH, getClasspathFiles());
155 } catch (Exception e) { }
158 ClassFileManager manager = new ClassFileManager(stdFileManager);
159 List<SimpleJavaFileObject> compilationUnits = new ArrayList<SimpleJavaFileObject>();
160 CharArrayWriter caw = new CharArrayWriter();
161 BufferedWriter out = new BufferedWriter(caw);
162 boolean isFile = true;
163 SourceString sourceString = null;
164 for (String s : code) {
165 File f = new File(s);
166 if (!f.exists() || !f.canRead()) {
173 for (String s : code) {
174 File f = new File(s);
175 compilationUnits.add(new SourceFile(f));
178 sourceString = new SourceString(className, code);
179 compilationUnits.add(sourceString);
182 String[] compileOptions = new String[] {"-d", BINPATH};
183 Iterable<String> options = Arrays.asList(compileOptions);
185 CompilationTask task = compiler.getTask(out, manager, diagnostics, options, null, compilationUnits);
186 boolean success = task.call();
189 final String s = System.getProperty("java.class.path").replace(cp + File.pathSeparatorChar, "");
190 System.setProperty("java.class.path", s);
197 error = caw.toString();
198 } catch (IOException e) {
203 } catch (IOException e) { }
210 if (manager.className != null && !manager.className.isEmpty()) {
211 return ScilabClassLoader.loadJavaClass(BINPATH, manager.className);
218 error = getCompilerErrors(diagnostics);
221 throw new ScilabJavaException(error);
226 * Returns the compilation errors from the diagnostics
227 * @param diagnostics the diagnostics returned by the compiler
228 * @return a string containing the errors
230 public static String getCompilerErrors(DiagnosticCollector<JavaFileObject> diagnostics) {
231 StringBuffer buffer = new StringBuffer();
233 buffer.append("----------\n");
234 for (Diagnostic <? extends JavaFileObject > d : diagnostics.getDiagnostics()) {
235 buffer.append(Integer.toString(cpt++)).append(". ").append(d.getKind());
236 if (d.getSource() != null) {
237 buffer.append(" in ").append(d.getSource().toUri().getPath()).append(" (at line ").append(Long.toString(d.getLineNumber())).append(")\n");
239 Reader reader = null;
241 reader = d.getSource().openReader(true);
242 reader.skip(d.getStartPosition());
243 char[] data = new char[(int) (d.getEndPosition() - d.getStartPosition() + 1)];
245 buffer.append(" ").append(data).append('\n');
246 Arrays.fill(data, '^');
247 buffer.append(" ").append(data).append('\n');
248 } catch (IOException e) {
251 if (reader != null) {
254 } catch (IOException e) { }
258 // this is not a file related error
261 buffer.append(d.getMessage(Locale.getDefault())).append('\n');
264 buffer.append("----------\n");
265 return buffer.toString();
269 * Get the current classpath with the correct separator (':' under Unix and ';' under Windows)
270 * @return the classpath
272 public static String getClasspath() {
273 URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
274 URL[] urls = loader.getURLs();
275 StringBuffer buffer = new StringBuffer();
277 for (URL url : urls) {
278 buffer.append(url.getPath()).append(File.pathSeparatorChar);
282 return buffer.toString();
286 * Get the files in the classpath
289 public static List<File> getClasspathFiles() {
290 URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
291 URL[] urls = loader.getURLs();
292 List<File> files = new ArrayList<File>(urls.length);
294 for (URL url : urls) {
296 files.add(new File(url.toURI()));
297 } catch (Exception e) { }
304 * Add a class in the classpath
305 * @param url the class url
307 public static void addURLToClassPath(URL url) {
308 URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
310 final Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] {URL.class});
311 method.setAccessible(true);
312 method.invoke(sysloader , new Object[] {url});
313 } catch (NoSuchMethodException e) {
314 System.err.println("Error: Cannot find the declared method: " + e.getLocalizedMessage());
315 } catch (IllegalAccessException e) {
316 System.err.println("Error: Illegal access: " + e.getLocalizedMessage());
317 } catch (InvocationTargetException e) {
318 System.err.println("Error: Could not invocate target: " + e.getLocalizedMessage());
323 * Inner class to handle String as File
325 private static class SourceString extends SimpleJavaFileObject {
327 private final String code;
329 private SourceString(String className, String[] code) {
330 super(new File(BINPATH + "/" + className.replace('.', '/') + Kind.SOURCE.extension).toURI(), Kind.SOURCE);
332 StringBuffer buf = new StringBuffer();
333 for (String str : code) {
337 this.code = buf.toString();
340 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
346 * Inner class to handle String as File
348 private static class SourceFile extends SimpleJavaFileObject {
352 private SourceFile(File f) {
353 super(f.toURI(), Kind.SOURCE);
357 public CharSequence getCharContent(boolean ignoreEncodingErrors) {
359 FileReader reader = new FileReader(f);
360 char[] buffer = new char[1024];
361 StringBuffer sb = new StringBuffer();
364 while ((r = reader.read(buffer, 0, 1024)) != -1) {
365 sb.append(buffer, 0, r);
371 } catch (Exception e) {
377 private static class ClassFileManager extends ForwardingJavaFileManager {
381 public ClassFileManager(StandardJavaFileManager standardManager) {
382 super(standardManager);
386 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
387 if (sibling instanceof SourceString && className != null && className.indexOf('$') == -1) {
388 this.className = className.replace('/', '.');
391 if (ScilabJavaObject.debug) {
392 ScilabJavaObject.logger.log(Level.INFO, "Compilation of class \'" + className + "\'");
395 return super.getJavaFileForOutput(location, className, kind, sibling);