// Copyright 2015 The Bazel Authors. All rights reserved. // // 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.google.devtools.build.java.bazel; import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Writer; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.nio.charset.Charset; import java.util.List; import java.util.Locale; import java.util.Set; import javax.lang.model.SourceVersion; import javax.tools.DiagnosticListener; import javax.tools.JavaCompiler; import javax.tools.JavaFileManager; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.StandardLocation; /** * Provides a {@link JavaCompiler} that has behavior as similar as possible to the java compiler * provided by default by Bazel. Replace calls to {@link * javax.tools.ToolProvider#getSystemJavaCompiler} with calls to {@link * BazelJavaCompiler#newInstance}. * *
This class is typically used only from a host build tool or in tests. When using this in
* production, langtools.jar and the bootclasspath jars are deployed as separate jar files within
* the runfiles directory.
*/
public class BazelJavaCompiler {
// The default blessed javac options.
private static final String DEFAULT_BOOTCLASSPATH = JavacBootclasspath.asString();
private static final ImmutableList More precisely, this method works analogously to {@link
* javax.tools.ToolProvider#getSystemJavaCompiler}, but returns a {@code JavaCompiler} that
* differs in these details:
*
* This class ensures that (by default) the {@code -source}, {@code -target} and {@code
* -bootclasspath} flags all agree and specify the same (blessed) JDK version, for language and
* API compatibility.
*
* This method finds the javac implementation using a custom classloader that does not consult
* the user's classpath. This works well, unless the return value is cast to a
* javac-implementation class like {@code JavacTask}, in which case the dreaded classloader error
* "can't cast JavacTaskImpl to JavacTask" raises its ugly head, in which case you should use
* {@link #newInstance(ClassLoader)} instead.
*/
public static JavaCompiler newInstance() {
try {
return newInstance(JAVA_COMPILER_CLASS.getConstructor().newInstance());
} catch (Exception e) {
throw new RuntimeException("Cannot get java compiler", e);
}
}
/**
* Returns a new {@link JavaCompiler} that has behavior as similar as possible to the java
* compiler provided by default by bazel, independent of the user-specified {@code JAVABASE}.
*
* This method has effect identical to {@link #newInstance()} (and that method is generally
* preferred to this one), except that the javac implementation is found via the provided
* classloader instead of defining a custom classloader that knows the standard location of the
* blessed javac implementation.
*
* This method is needed when the return value is cast to a javac-implementation class like
* {@code JavacTask}, to avoid the dreaded multiple classloader error "can't cast JavacTaskImpl to
* JavacTask".
*
* Typically, users should pass {@code ClassLoader.getSystemClassLoader()} as the argument to
* this method.
*/
public static JavaCompiler newInstance(ClassLoader cl) {
try {
return newInstance(getJavaCompilerClass(cl).getConstructor().newInstance());
} catch (Exception e) {
throw new RuntimeException("Cannot get java compiler", e);
}
}
private static JavaCompiler newInstance(final JavaCompiler delegate) {
// We forward most operations to the JavaCompiler implementation in langtools.jar.
return new JavaCompiler() {
@Override
public CompilationTask getTask(
Writer out,
JavaFileManager fileManager,
DiagnosticListener super JavaFileObject> diagnosticListener,
Iterable
*
*
*