Java tutorial
/* * Copyright 2016 Sam Sun <me@samczsun.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.samczsun.helios.transformers.compilers; import com.samczsun.helios.Helios; import com.samczsun.helios.handler.ExceptionHandler; import com.samczsun.helios.utils.SWTUtil; import com.samczsun.helios.utils.Utils; import com.sun.tools.javac.main.Main; import org.apache.commons.io.FileUtils; import org.eclipse.swt.widgets.Shell; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.file.Files; public class JavaCompiler extends Compiler { JavaCompiler() { super("java", "Java"); } @Override public boolean isApplicable(String className) { return className.endsWith(".java"); } @Override public byte[] compile(String name, String contents) { File tmpdir = null; File javaFile = null; File classFile = null; File classpathFile = null; try { tmpdir = Files.createTempDirectory("javac").toFile(); javaFile = new File(tmpdir, name + ".java"); classFile = new File(tmpdir, name + ".class"); classpathFile = new File(tmpdir, "classpath.jar"); FileUtils.write(javaFile, contents, "UTF-8", false); Utils.save(classpathFile, Helios.getAllLoadedData()); StringWriter stringWriter = new StringWriter(); com.sun.tools.javac.main.Main compiler = new com.sun.tools.javac.main.Main("javac", new PrintWriter(stringWriter)); int responseCode = compiler.compile(new String[] { "-d", tmpdir.getAbsolutePath(), "-classpath", buildPath(classFile), javaFile.getAbsolutePath() }).exitCode; if (responseCode != Main.Result.OK.exitCode) { System.out.println(stringWriter.toString()); Shell shell = SWTUtil.generateLongMessage("Error", stringWriter.toString()); shell.getDisplay().syncExec(() -> { SWTUtil.center(shell); shell.open(); }); } else { return FileUtils.readFileToByteArray(classFile); } } catch (Exception e) { ExceptionHandler.handle(e); } finally { FileUtils.deleteQuietly(javaFile); FileUtils.deleteQuietly(classFile); FileUtils.deleteQuietly(classpathFile); FileUtils.deleteQuietly(tmpdir); } return null; } }