diff --git a/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java b/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java deleted file mode 100644 index f09145fa567..00000000000 --- a/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Copyright 2016 The Error Prone Authors. - * - * 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.errorprone; - -import static com.google.common.base.Preconditions.checkNotNull; - -import com.google.common.collect.ImmutableList; -import com.google.errorprone.MaskedClassLoader.MaskedFileManager; -import com.google.errorprone.scanner.ScannerSupplier; -import com.sun.tools.javac.main.CommandLine; -import com.sun.tools.javac.main.Main.Result; -import java.io.BufferedWriter; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.UncheckedIOException; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.List; -import javax.annotation.processing.Processor; -import javax.tools.DiagnosticListener; -import javax.tools.JavaCompiler; -import javax.tools.JavaCompiler.CompilationTask; -import javax.tools.JavaFileManager; -import javax.tools.JavaFileObject; -import javax.tools.StandardJavaFileManager; - -/** - * An Error Prone compiler that matches the interface of {@link com.sun.tools.javac.Main}. - * - * @deprecated prefer {@link BaseErrorProneJavaCompiler}, which implements the standard {@code - * javax.tools.JavacCompiler} interface. - */ -@Deprecated -public class BaseErrorProneCompiler { - - private final DiagnosticListener diagnosticListener; - private final PrintWriter errOutput; - private final ScannerSupplier scannerSupplier; - - private BaseErrorProneCompiler( - PrintWriter errOutput, - DiagnosticListener diagnosticListener, - ScannerSupplier scannerSupplier) { - this.errOutput = errOutput; - this.diagnosticListener = diagnosticListener; - this.scannerSupplier = checkNotNull(scannerSupplier); - } - - /** Returns a {@link BaseErrorProneCompiler} builder. */ - public static Builder builder() { - return new Builder(); - } - - /** A {@link BaseErrorProneCompiler} builder. */ - public static class Builder { - private DiagnosticListener diagnosticListener = null; - private PrintWriter errOutput = - new PrintWriter( - new BufferedWriter(new OutputStreamWriter(System.err, Charset.defaultCharset())), true); - private ScannerSupplier scannerSupplier; - - public BaseErrorProneCompiler build() { - return new BaseErrorProneCompiler(errOutput, diagnosticListener, scannerSupplier); - } - - public Builder redirectOutputTo(PrintWriter errOutput) { - this.errOutput = errOutput; - return this; - } - - public Builder listenToDiagnostics(DiagnosticListener listener) { - this.diagnosticListener = listener; - return this; - } - - public Builder report(ScannerSupplier scannerSupplier) { - this.scannerSupplier = scannerSupplier; - return this; - } - } - - public Result run(String[] argv) { - try { - argv = CommandLine.parse(argv); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - List javacOpts = new ArrayList<>(); - List sources = new ArrayList<>(); - for (String arg : argv) { - // TODO(cushon): is there a better way to categorize javacopts? - if (!arg.startsWith("-") && arg.endsWith(".java")) { - sources.add(arg); - } else { - javacOpts.add(arg); - } - } - StandardJavaFileManager fileManager = new MaskedFileManager(); - return run( - javacOpts.toArray(new String[0]), - fileManager, - ImmutableList.copyOf(fileManager.getJavaFileObjectsFromStrings(sources)), - /* processors= */ null); - } - - public Result run(String[] argv, List javaFileObjects) { - return run(argv, null, javaFileObjects, ImmutableList.of()); - } - - public Result run( - String[] args, - JavaFileManager fileManager, - List javaFileObjects, - Iterable processors) { - JavaCompiler compiler = new BaseErrorProneJavaCompiler(scannerSupplier); - try { - CompilationTask task = - compiler.getTask( - errOutput, - fileManager, - diagnosticListener, - ImmutableList.copyOf(args), - null /*classes*/, - javaFileObjects); - if (processors != null) { - task.setProcessors(processors); - } - return task.call() ? Result.OK : Result.ERROR; - } catch (InvalidCommandLineOptionException e) { - errOutput.print(e); - errOutput.flush(); - return Result.CMDERR; - } - } -} diff --git a/core/src/main/java/com/google/errorprone/ErrorProneCompiler.java b/core/src/main/java/com/google/errorprone/ErrorProneCompiler.java deleted file mode 100644 index 869e15b2e3d..00000000000 --- a/core/src/main/java/com/google/errorprone/ErrorProneCompiler.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2011 The Error Prone Authors. - * - * 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.errorprone; - -import com.google.errorprone.scanner.BuiltInCheckerSuppliers; -import com.google.errorprone.scanner.ScannerSupplier; -import com.sun.tools.javac.main.Main.Result; -import java.io.PrintWriter; -import javax.tools.DiagnosticListener; -import javax.tools.JavaFileObject; - -/** - * An Error Prone compiler that matches the interface of {@link com.sun.tools.javac.main.Main}. - * - *

Unlike {@link BaseErrorProneCompiler}, it enables all built-in Error Prone checks by default. - * - *

Used by plexus-java-compiler-errorprone. - * - * @author alexeagle@google.com (Alex Eagle) - * @deprecated prefer {@link ErrorProneJavaCompiler}, which implements the standard {@code - * javax.tools.JavacCompiler} interface. - */ -@Deprecated -public class ErrorProneCompiler { - - /** - * Entry point for compiling Java code with error-prone enabled. All default checks are run, and - * the compile fails if they find a bug. - * - * @param args the same args which could be passed to javac on the command line - */ - public static void main(String[] args) { - System.exit(compile(args).exitCode); - } - - /** - * Compiles in-process. - * - * @param listener listens to the diagnostics produced by error-prone - * @param args the same args which would be passed to javac on the command line - * @return result from the compiler invocation - */ - public static Result compile(DiagnosticListener listener, String[] args) { - return ErrorProneCompiler.builder().listenToDiagnostics(listener).build().run(args); - } - - /** - * Programmatic interface to the error-prone Java compiler. - * - * @param args the same args which would be passed to javac on the command line - * @return result from the compiler invocation - */ - public static Result compile(String[] args) { - return builder().build().run(args); - } - - /** - * Programmatic interface to the error-prone Java compiler. - * - * @param args the same args which would be passed to javac on the command line - * @param out a {@link PrintWriter} to which to send diagnostic output - * @return result from the compiler invocation - */ - public static Result compile(String[] args, PrintWriter out) { - return ErrorProneCompiler.builder().redirectOutputTo(out).build().run(args); - } - - private final BaseErrorProneCompiler compiler; - - private ErrorProneCompiler(BaseErrorProneCompiler compiler) { - this.compiler = compiler; - } - - public static Builder builder() { - return new Builder(); - } - - public static class Builder { - private final BaseErrorProneCompiler.Builder builder = - new BaseErrorProneCompiler.Builder().report(BuiltInCheckerSuppliers.defaultChecks()); - - public ErrorProneCompiler build() { - return new ErrorProneCompiler(builder.build()); - } - - public Builder redirectOutputTo(PrintWriter errOutput) { - builder.redirectOutputTo(errOutput); - return this; - } - - public Builder listenToDiagnostics(DiagnosticListener listener) { - builder.listenToDiagnostics(listener); - return this; - } - - public Builder report(ScannerSupplier scannerSupplier) { - builder.report(scannerSupplier); - return this; - } - - /** @deprecated prefer {@link #builder()} */ - @Deprecated - public Builder() {} - } - - public Result run(String[] args) { - return compiler.run(args); - } -} diff --git a/core/src/main/java/com/google/errorprone/refaster/ExpressionTemplate.java b/core/src/main/java/com/google/errorprone/refaster/ExpressionTemplate.java index 7d23b3e8419..0057b2fdb9c 100644 --- a/core/src/main/java/com/google/errorprone/refaster/ExpressionTemplate.java +++ b/core/src/main/java/com/google/errorprone/refaster/ExpressionTemplate.java @@ -282,7 +282,7 @@ private static int getPrecedence(JCTree leaf, Context context) { return (typeCast.expr == leaf) ? TreeInfo.prefixPrec : TreeInfo.noPrec; } else if (parent instanceof JCInstanceOf) { JCInstanceOf instanceOf = (JCInstanceOf) parent; - return TreeInfo.ordPrec + ((instanceOf.clazz == leaf) ? 1 : 0); + return TreeInfo.ordPrec + ((instanceOf.getType() == leaf) ? 1 : 0); } else if (parent instanceof JCArrayAccess) { JCArrayAccess arrayAccess = (JCArrayAccess) parent; return (arrayAccess.indexed == leaf) ? TreeInfo.postfixPrec : TreeInfo.noPrec; diff --git a/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java b/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java index fe9a3d64c4e..b4f32735d2e 100644 --- a/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java +++ b/test_helpers/src/main/java/com/google/errorprone/BugCheckerRefactoringTestHelper.java @@ -230,6 +230,7 @@ private JCCompilationUnit doCompile( throw new IllegalArgumentException("Exception during argument processing: " + e); } context.put(ErrorProneOptions.class, errorProneOptions); + fileManager.createAndInstallTempFolderForOutput(); JavacTaskImpl task = (JavacTaskImpl) tool.getTask( diff --git a/test_helpers/src/main/java/com/google/errorprone/CompilationTestHelper.java b/test_helpers/src/main/java/com/google/errorprone/CompilationTestHelper.java index 779e9c9a00e..47e27597f88 100644 --- a/test_helpers/src/main/java/com/google/errorprone/CompilationTestHelper.java +++ b/test_helpers/src/main/java/com/google/errorprone/CompilationTestHelper.java @@ -337,7 +337,7 @@ private Result compile() { if (checkWellFormed) { checkWellFormed(sources, processedArgs); } - createAndInstallTempFolderForOutput(fileManager); + fileManager.createAndInstallTempFolderForOutput(); return compiler .getTask( new PrintWriter( @@ -353,30 +353,8 @@ private Result compile() { : Result.ERROR; } - private static void createAndInstallTempFolderForOutput( - ErrorProneInMemoryFileManager fileManager) { - Path tempDirectory; - try { - tempDirectory = - Files.createTempDirectory( - fileManager.fileSystem().getRootDirectories().iterator().next(), ""); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - Arrays.stream(StandardLocation.values()) - .filter(StandardLocation::isOutputLocation) - .forEach( - outputLocation -> { - try { - fileManager.setLocationFromPaths(outputLocation, ImmutableList.of(tempDirectory)); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); - } - private void checkWellFormed(Iterable sources, List args) { - createAndInstallTempFolderForOutput(fileManager); + fileManager.createAndInstallTempFolderForOutput(); JavaCompiler compiler = JavacTool.create(); OutputStream outputStream = new ByteArrayOutputStream(); List remainingArgs = null; diff --git a/test_helpers/src/main/java/com/google/errorprone/ErrorProneInMemoryFileManager.java b/test_helpers/src/main/java/com/google/errorprone/ErrorProneInMemoryFileManager.java index 8430fc9221c..bc401195ae8 100644 --- a/test_helpers/src/main/java/com/google/errorprone/ErrorProneInMemoryFileManager.java +++ b/test_helpers/src/main/java/com/google/errorprone/ErrorProneInMemoryFileManager.java @@ -34,8 +34,10 @@ import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.List; import javax.tools.JavaFileObject; +import javax.tools.StandardLocation; /** An in-memory file manager for testing that uses {@link JavacFileManager} and {@link Jimfs}. */ public class ErrorProneInMemoryFileManager extends JavacFileManager { @@ -168,4 +170,24 @@ public boolean exists(String fileName) { public FileSystem fileSystem() { return fileSystem; } + + void createAndInstallTempFolderForOutput() { + Path tempDirectory; + try { + tempDirectory = + Files.createTempDirectory(fileSystem().getRootDirectories().iterator().next(), ""); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + Arrays.stream(StandardLocation.values()) + .filter(StandardLocation::isOutputLocation) + .forEach( + outputLocation -> { + try { + setLocationFromPaths(outputLocation, ImmutableList.of(tempDirectory)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }); + } }