/* * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.tools.sjavac.comp; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.Set; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.api.JavacTool; import com.sun.tools.javac.util.Context; import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Options; import com.sun.tools.sjavac.Log; import com.sun.tools.sjavac.Util; import com.sun.tools.sjavac.comp.dependencies.DependencyCollector; import com.sun.tools.sjavac.comp.dependencies.PublicApiCollector; import com.sun.tools.sjavac.server.CompilationResult; import com.sun.tools.sjavac.server.Sjavac; import com.sun.tools.sjavac.server.SysInfo; /** * The sjavac implementation that interacts with javac and performs the actual * compilation. * *

This is NOT part of any supported API. * If you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice. */ public class SjavacImpl implements Sjavac { static Object lock = new Object(); @Override public SysInfo getSysInfo() { return new SysInfo(Runtime.getRuntime().availableProcessors(), Runtime.getRuntime().maxMemory()); } @Override public CompilationResult compile(String protocolId, String invocationId, String[] args, List explicitSources, Set sourcesToCompile, Set visibleSources) { JavacTool compiler = (JavacTool) ToolProvider.getSystemJavaCompiler(); try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) { SmartFileManager sfm = new SmartFileManager(fm); Context context = new Context(); // Now setup the actual compilation CompilationResult compilationResult = new CompilationResult(0); // First deal with explicit source files on cmdline and in at file ListBuffer explicitJFOs = new ListBuffer<>(); for (JavaFileObject i : fm.getJavaFileObjectsFromFiles(explicitSources)) { explicitJFOs.append(i); } // Now deal with sources supplied as source_to_compile ListBuffer sourcesToCompileFiles = new ListBuffer<>(); for (URI u : sourcesToCompile) sourcesToCompileFiles.append(new File(u)); for (JavaFileObject i : fm.getJavaFileObjectsFromFiles(sourcesToCompileFiles)) explicitJFOs.append(i); // Create a new logger StringWriter stdoutLog = new StringWriter(); StringWriter stderrLog = new StringWriter(); PrintWriter stdout = new PrintWriter(stdoutLog); PrintWriter stderr = new PrintWriter(stderrLog); com.sun.tools.javac.main.Main.Result rc = com.sun.tools.javac.main.Main.Result.OK; DependencyCollector depsCollector = new DependencyCollector(context); PublicApiCollector pubApiCollector = new PublicApiCollector(context); PathAndPackageVerifier papVerifier = new PathAndPackageVerifier(); try { if (explicitJFOs.size() > 0) { sfm.setVisibleSources(visibleSources); sfm.cleanArtifacts(); sfm.setLog(stdout); // Do the compilation! JavacTaskImpl task = (JavacTaskImpl) compiler.getTask(stderr, sfm, null, Arrays.asList(args), null, explicitJFOs, context); sfm.setSymbolFileEnabled(!Options.instance(context).isSet("ignore.symbol.file")); task.addTaskListener(depsCollector); task.addTaskListener(pubApiCollector); task.addTaskListener(papVerifier); Log.debug("Invoking javac with args"); Arrays.asList(args).forEach(arg -> Log.debug(" " + arg)); rc = task.doCall(); Log.debug("javac returned with code " + rc); sfm.flush(); } } catch (Exception e) { Log.error(Util.getStackTrace(e)); stderrLog.append(Util.getStackTrace(e)); rc = com.sun.tools.javac.main.Main.Result.ERROR; } compilationResult.packageArtifacts = sfm.getPackageArtifacts(); if (papVerifier.errorsDiscovered()) rc = com.sun.tools.javac.main.Main.Result.ERROR; Dependencies deps = Dependencies.instance(context); compilationResult.packageDependencies = deps.getDependencies(explicitJFOs); compilationResult.packageCpDependencies = deps.getCpDependencies(explicitJFOs); compilationResult.packagePubapis = deps.getPubapis(explicitJFOs, true); compilationResult.dependencyPubapis = deps.getPubapis(explicitJFOs, false); compilationResult.stdout = stdoutLog.toString(); compilationResult.stderr = stderrLog.toString(); compilationResult.returnCode = rc.exitCode; return compilationResult; } catch (IOException e) { throw new Error(e); } } @Override public void shutdown() { // Nothing to clean up // ... maybe we should wait for any current request to finish? } @Override public String serverSettings() { return ""; } }