diff --git a/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java b/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java
index 1a0e5364347..a08338bfd31 100644
--- a/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java
+++ b/check_api/src/main/java/com/google/errorprone/BaseErrorProneCompiler.java
@@ -37,6 +37,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
+import java.util.ResourceBundle;
import java.util.Set;
import javax.annotation.Nullable;
import javax.annotation.processing.Processor;
@@ -293,10 +294,9 @@ private Result wrapPotentialRefactoringCall(
}
}
- /**
- * Registers our message bundle.
- */
+ /** Registers our message bundle. */
public static void setupMessageBundle(Context context) {
- JavacMessages.instance(context).add("com.google.errorprone.errors");
+ ResourceBundle bundle = ResourceBundle.getBundle("com.google.errorprone.errors");
+ JavacMessages.instance(context).add(l -> bundle);
}
}
diff --git a/core/pom.xml b/core/pom.xml
index 226b2416de0..ceb2cb5865f 100644
--- a/core/pom.xml
+++ b/core/pom.xml
@@ -267,6 +267,13 @@
r6
test
+
+
+ com.google.auto.service
+ auto-service
+ 1.0-rc2
+ provided
+
diff --git a/core/src/main/java/com/google/errorprone/ErrorProneJavacPlugin.java b/core/src/main/java/com/google/errorprone/ErrorProneJavacPlugin.java
new file mode 100644
index 00000000000..c7fcdc8ed3d
--- /dev/null
+++ b/core/src/main/java/com/google/errorprone/ErrorProneJavacPlugin.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2017 Google Inc. 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.errorprone;
+
+import com.google.auto.service.AutoService;
+import com.google.errorprone.scanner.BuiltInCheckerSuppliers;
+import com.sun.source.util.JavacTask;
+import com.sun.source.util.Plugin;
+import com.sun.tools.javac.api.BasicJavacTask;
+import com.sun.tools.javac.util.Context;
+
+/** A javac {@link Plugin} that runs Error Prone. */
+@AutoService(Plugin.class)
+public class ErrorProneJavacPlugin implements Plugin {
+ @Override
+ public String getName() {
+ return "ErrorProne";
+ }
+
+ @Override
+ public void init(JavacTask javacTask, String... args) {
+ Context context = ((BasicJavacTask) javacTask).getContext();
+ BaseErrorProneCompiler.setupMessageBundle(context);
+ javacTask.addTaskListener(
+ ErrorProneAnalyzer.createByScanningForPlugins(
+ BuiltInCheckerSuppliers.defaultChecks(), ErrorProneOptions.processArgs(args), context));
+ }
+}
diff --git a/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java b/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java
new file mode 100644
index 00000000000..66201794fc3
--- /dev/null
+++ b/core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2017 Google Inc. 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.errorprone;
+
+import static com.google.common.collect.MoreCollectors.onlyElement;
+import static com.google.common.truth.Truth.assertThat;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Locale.ENGLISH;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.jimfs.Configuration;
+import com.google.common.jimfs.Jimfs;
+import com.sun.source.util.JavacTask;
+import com.sun.tools.javac.api.JavacTool;
+import com.sun.tools.javac.file.JavacFileManager;
+import com.sun.tools.javac.util.Context;
+import java.io.IOException;
+import java.nio.file.FileSystem;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import javax.tools.Diagnostic;
+import javax.tools.DiagnosticCollector;
+import javax.tools.JavaFileObject;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** {@link ErrorProneJavacPlugin}Test */
+@RunWith(JUnit4.class)
+public class ErrorProneJavacPluginTest {
+ @Test
+ public void hello() throws IOException {
+ FileSystem fileSystem = Jimfs.newFileSystem(Configuration.unix());
+ Path source = fileSystem.getPath("Test.java");
+ Files.write(
+ source,
+ ImmutableList.of(
+ "import java.util.HashSet;",
+ "import java.util.Set;",
+ "class Test {",
+ " public static void main(String[] args) {",
+ " Set s = new HashSet<>();",
+ " for (short i = 0; i < 100; i++) {",
+ " s.add(i);",
+ " s.remove(i - 1);",
+ " }",
+ " System.out.println(s.size());",
+ " }",
+ "}"),
+ UTF_8);
+ JavacFileManager fileManager = new JavacFileManager(new Context(), false, UTF_8);
+ DiagnosticCollector diagnosticCollector = new DiagnosticCollector<>();
+ JavacTask task =
+ JavacTool.create()
+ .getTask(
+ null,
+ fileManager,
+ diagnosticCollector,
+ ImmutableList.of("-Xplugin:ErrorProne"),
+ ImmutableList.of(),
+ fileManager.getJavaFileObjects(source));
+ assertThat(task.call()).isFalse();
+ Diagnostic extends JavaFileObject> diagnostic =
+ diagnosticCollector
+ .getDiagnostics()
+ .stream()
+ .filter(d -> d.getKind() == Diagnostic.Kind.ERROR)
+ .collect(onlyElement());
+ assertThat(diagnostic.getMessage(ENGLISH)).contains("[CollectionIncompatibleType]");
+ }
+}