Author: tchemit Date: 2008-01-12 19:27:52 +0000 (Sat, 12 Jan 2008) New Revision: 154 Added: trunk/jaxx/src/java/jaxx/compiler/JavaCCompiler.java Log: extraction du compilateur javac du compilateur jaxxc Added: trunk/jaxx/src/java/jaxx/compiler/JavaCCompiler.java =================================================================== --- trunk/jaxx/src/java/jaxx/compiler/JavaCCompiler.java (rev 0) +++ trunk/jaxx/src/java/jaxx/compiler/JavaCCompiler.java 2008-01-12 19:27:52 UTC (rev 154) @@ -0,0 +1,130 @@ +/* + * Copyright 2006 Ethan Nicholas. All rights reserved. + * Use is subject to license terms. + */ +package jaxx.compiler; + +import static jaxx.compiler.JAXXCompiler.URLtoFile; + +import java.io.File; +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URL; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + + +/** + * Compiles java files into Java classes. + * + * @author chemit + */ +public class JavaCCompiler { + + static JAXXCompiler compiler; + static String classpath; + + public static void runJavac(final JAXXCompiler compiler) { + + CompilerOptions options = compiler.getOptions(); + JavaCCompiler.compiler = compiler; + runJavac( + options.getJavacTargetDirectory().getPath(), + options.getJavacOpts(), + compiler.getDest().getPath() + ); + } + + public static void runJavac(String destRoot, String options, String... destJavas) { + + PrintStream oldErr = System.err; + + String classpath = getClassPath(); + + try { + + initOutStream(oldErr); + + for (String destJava : destJavas) { + + String[] javacOpts = initOptions(classpath, destRoot, destJava, options==null?null:options.split("\\s+")); + //System.out.println("launch javac : "+ Arrays.toString(javacOpts)); + com.sun.tools.javac.Main.compile(javacOpts); + } + + } + catch (Exception e) { + System.err.println("An error occurred while invoking javac: "); + e.printStackTrace(); + if (compiler != null) { + compiler.setFailed(true); + } + } finally { + System.setErr(oldErr); + compiler = null; + } + } + + private static String getClassPath() { + if (classpath == null) { + // we do actually use a single jar for jaxx (core + runtime + swing +...) + String PATH = "/jaxx/compiler/JAXXCompiler.class"; + URL jaxxURL = JavaCCompiler.class.getResource(PATH); + if (jaxxURL == null) { + throw new InternalError("Can't-happen error: could not find /jaxx/compiler/JAXXCompiler.class on class path"); + } + String classpath = jaxxURL.toString(); + if (classpath.startsWith("jar:")) { + classpath = classpath.substring("jar:".length()); + classpath = classpath.substring(0, classpath.indexOf("!")); + classpath = URLtoFile(classpath).getPath(); + } else if (classpath.startsWith("file:")) { + // fix-bug : we must take the classpath and not the entire location to class + classpath = classpath.substring(0, classpath.length() - PATH.length()); + classpath = URLtoFile(classpath).getPath(); + } + JavaCCompiler.classpath = classpath; + } + return classpath; + } + + private static String[] initOptions(String classpath, String destRoot, String destJava, String... options) { + List<String> javacOpts = new ArrayList<String>(); + + if (options != null) { + javacOpts.addAll(Arrays.asList(options)); + } + if (destRoot != null) { + javacOpts.add("-d"); + javacOpts.add(destRoot); + classpath += File.pathSeparator + destRoot; + } + javacOpts.add("-classpath"); + javacOpts.add(classpath); + javacOpts.add(destJava); + return javacOpts.toArray(new String[javacOpts.size()]); + } + + private static void initOutStream(final PrintStream oldErr) { + System.setErr(new PrintStream(new FilterOutputStream(oldErr) { + @Override + public void write(byte[] b, int off, int len) throws IOException { + String stringValue = new String(b, off, len).trim(); + if (stringValue.startsWith("Error:") || stringValue.startsWith("Usage:") || stringValue.endsWith("error") || + stringValue.endsWith("errors")) { + if (compiler != null) { + compiler.setFailed(true); + } + } + if (stringValue.endsWith("uses unchecked or unsafe operations.") || + stringValue.endsWith("with -Xlint:unchecked for details.")) { + return; + } + super.write(b, off, len); + } + })); + } + +} \ No newline at end of file
participants (1)
-
tchemit@users.labs.libre-entreprise.org