-
Notifications
You must be signed in to change notification settings - Fork 742
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of a com.sun.source.util.Plugin
This allows Error Prone to be used as a javac plugin (instead of a wrapper) via the plugin API that was added in 8: https://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/com/sun/source/util/Plugin.html Demo: ``` javac -J-Xbootclasspath/p:javac-9-dev-r3297-3.jar \ -processorpath error_prone_ant-2.0.18-SNAPSHOT.jar \ -Xplugin:ErrorProne Test.java ... ShortSet.java:8: error: [CollectionIncompatibleType] Argument 'i - 1' should not be passed to this method; its type int is not compatible with its collection's type argument Short s.remove(i - 1); ^ (see http://errorprone.info/bugpattern/CollectionIncompatibleType) ``` See #535 MOE_MIGRATED_REVID=148835033
- Loading branch information
Showing
4 changed files
with
138 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
core/src/main/java/com/google/errorprone/ErrorProneJavacPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
core/src/test/java/com/google/errorprone/ErrorProneJavacPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Short> 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<JavaFileObject> 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]"); | ||
} | ||
} |
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh, nice! Is this part of an effort to eventually detach error-prone from the Java compiler @cushon ? :)
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error Prone still depends on the specific javac we repackage, and their are no immediate plan to change that. But if there are better-supported alternatives to using internal APIs we should migrate, and maybe some day that will make it possible to detach.
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cushon This seems to work with some caveats. Plain old javac does not like any of errorprone's
-Xep:
options. These are processed by the compiler before creating theJavacTask
implementation being passed over to plugins. Any suggestions on how to deal with this situation?feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syntax for passing arguments to Xplugins is e.g.:
javac -Xplugin:ErrorProne "-XepAllErrorsAsWarnings -Xep:CollectionIncompatibleType:ERROR"
(see https://docs.oracle.com/javase/9/tools/javac.htm#JSWOR627)
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using quotes, I am now seeing
invalid flag: "-Xep:NullablePrimitive:OFF -Xep:DoubleCheckedLocking:OFF...
Am i missing something? The link you sent does not seem to have anything related to plugin args specifically. It just points at the document root and does not point to any section
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cushon ^ :)
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I messed up the quoting. The linked documentation has a brief section on
-Xplugin
that acknowledges it's possible to pass args through to the plugin. This example works for me with JDK 8u152:feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that seemed to work. But it seems to have some trouble loading custom plugins. Has a stacktrace like
Is this expected?
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, can you file a bug with a repro?
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure will do
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cushon It seems like error prone javac does not like
-J-Xbootclasspath/p
. So I did not set it. It could be related. It fails withinvalid flag: -J-Xbootclasspath/p:
when I try to set it withjavac-9-dev-r3297-4.jar
feb4b34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kageiit if you file a bug with a repro I'll investigate.
-J
flags only work with the standard javac launcher. Depending on how you're invoking it you can pass-Xbootclasspath/p
directly to the jvm, e.g.: