Compile with ECJ. See http://j.mp/8paifz for documentation. @param sketch Sketch object to be compiled, used for placing exceptions @param buildPath Where the temporary files live and will be built from. @return true if successful. @throws SketchException Only if there's a problem. Only then.
(JavaBuild build)
| 57 | * @throws SketchException Only if there's a problem. Only then. |
| 58 | */ |
| 59 | static public boolean compile(JavaBuild build) throws SketchException { |
| 60 | |
| 61 | // This will be filled in if anyone gets angry |
| 62 | SketchException exception = null; |
| 63 | boolean success = false; |
| 64 | |
| 65 | String baseCommand[] = new String[] { |
| 66 | "-g", |
| 67 | "-Xemacs", |
| 68 | //"-noExit", // not necessary for ecj |
| 69 | "-source", "1.7", |
| 70 | "-target", "1.7", |
| 71 | "-encoding", "utf8", |
| 72 | "-classpath", build.getClassPath(), |
| 73 | "-nowarn", // we're not currently interested in warnings (works in ecj) |
| 74 | "-d", build.getBinFolder().getAbsolutePath() // output the classes in the buildPath |
| 75 | }; |
| 76 | //PApplet.println(baseCommand); |
| 77 | |
| 78 | String[] sourceFiles = Util.listFiles(build.getSrcFolder(), false, ".java"); |
| 79 | String[] command = PApplet.concat(baseCommand, sourceFiles); |
| 80 | //PApplet.println(command); |
| 81 | |
| 82 | try { |
| 83 | // Load errors into a local StringBuilder |
| 84 | final StringBuilder errorBuffer = new StringBuilder(); |
| 85 | |
| 86 | // Create single method dummy writer class to slurp errors from ecj |
| 87 | Writer internalWriter = new Writer() { |
| 88 | public void write(char[] buf, int off, int len) { |
| 89 | errorBuffer.append(buf, off, len); |
| 90 | } |
| 91 | |
| 92 | public void flush() { } |
| 93 | |
| 94 | public void close() { } |
| 95 | }; |
| 96 | // Wrap as a PrintWriter since that's what compile() wants |
| 97 | PrintWriter writer = new PrintWriter(internalWriter); |
| 98 | |
| 99 | //result = com.sun.tools.javac.Main.compile(command, writer); |
| 100 | |
| 101 | PrintWriter outWriter = new PrintWriter(System.out); |
| 102 | |
| 103 | // Version that's not dynamically loaded |
| 104 | //CompilationProgress progress = null; |
| 105 | //success = BatchCompiler.compile(command, outWriter, writer, progress); |
| 106 | |
| 107 | // Version that *is* dynamically loaded. First gets the mode class loader |
| 108 | // so that it can grab the compiler JAR files from it. |
| 109 | ClassLoader loader = build.mode.getClassLoader(); |
| 110 | try { |
| 111 | Class<?> batchClass = |
| 112 | Class.forName("org.eclipse.jdt.core.compiler.batch.BatchCompiler", false, loader); |
| 113 | Class<?> progressClass = |
| 114 | Class.forName("org.eclipse.jdt.core.compiler.CompilationProgress", false, loader); |
| 115 | Class<?>[] compileArgs = |
| 116 | new Class<?>[] { String[].class, PrintWriter.class, PrintWriter.class, progressClass }; |
no test coverage detected