Error Prone, a Java compiler plugin open sourced by Google, performs static evaluation throughout compilation to detect bugs or recommend attainable enhancements. The plugin incorporates greater than 500 pre-defined bug checks and permits third social gathering and customized plugins. After detecting points, Error Susceptible can show a warning or robotically change the code with a predefined resolution. Error Susceptible helps Java 8, 11 and 17 and could also be used for bug fixing or giant scale refactoring.Set up and configuration directions for Maven, Bazel, Ant or Gradle may be discovered within the documentation. The compiler must be configured to make use of Error Susceptible as an annotation processor, for instance when making a take a look at undertaking with Maven:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<model>3.10.1</model>
<configuration>
<launch>17</launch>
<encoding>UTF-8</encoding>
<compilerArgs>
<arg>-XDcompilePolicy=easy</arg>
<arg>-Xplugin:ErrorProne</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_core</artifactId>
<model>2.15.0</model>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Now an instance class may be created. Think about the next technique that makes use of equals
to check two arrays, extra exactly it compares the objects and never the content material of the arrays:
public boolean examine(String firstList[], String secondList[])
return firstList.equals(secondList);
Executing mvn clear confirm
triggers the Error Susceptible evaluation and leads to the next error message:
[ERROR] Did not execute purpose org.apache.maven.plugins:maven-compiler-plugin:3.10.1:
compile (default-compile) on undertaking ErrorProne: Compilation failure
[ERROR] …/ErrorProne/src/primary/java/org/instance/Important.java:[5,28]
[ArrayEquals] Reference equality used to check arrays
[ERROR] (see https://errorprone.data/bugpattern/ArrayEquals)
[ERROR] Did you imply 'return Arrays.equals(firstList, secondList);'?
The ArrayEquals bug sample is discovered and Error Susceptible suggests to alter the implementation with the intention to examine the content material of the array as an alternative of the objects:
return Arrays.equals(firstList, secondList);
Receiving an error helps to enhance the code, however it’s additionally attainable to let Error Susceptible apply the answer robotically. The -XepPatchChecks
argument ought to include a comma separated checklist of bug patterns to use. On this case, solely the ArrayEquals resolution is utilized to the codebase. The -XepPatchLocation
argument is used to specify the placement of the answer file. On this case the unique file is modified:
<compilerArgs>
<arg>-XDcompilePolicy=easy</arg>
<arg>-Xplugin:ErrorProne -XepPatchChecks:ArrayEquals
-XepPatchLocation:IN_PLACE</arg>
</compilerArgs>
Now, after executing mvn clear confirm
the category file is robotically modified to:
public boolean examine(String firstList[], String secondList[])
return Arrays.equals(firstList, secondList);
The documentation gives extra details about the command-line flags.
Other than the built-in bug patterns, it is also attainable to make use of patterns from different organizations equivalent to SLF4J or to create a customized plugin. The supply code for the built-in guidelines gives varied examples which may be used as the idea for a brand new plugin. For instance, a customized Error Susceptible plugin can substitute the older @Before
JUnit annotation with the brand new JUnit 5 @BeforeEach
annotation.
The customized Error Susceptible plugin must be positioned in one other Maven module than the instance undertaking proven earlier. Error Susceptible makes use of the service loader mechanism to load the bug checks. Usually, that requires some configuration, nevertheless Google’s AutoService undertaking simplifies the configuration through the use of the @AutoService
annotation. The @BugPattern
annotation is used to configure the identify, abstract and severity of the bug. The next instance returns Description.NO_MATCH
if no @Earlier than
annotation is discovered or the SuggestedFix
which replaces the @Earlier than
annotation with an @BeforeEach
annotation:
@AutoService(BugChecker.class)
@BugPattern(
identify = "BeforeCheck",
abstract = "JUnit 4's @Earlier than is changed by JUnit 5's @BeforeEach",
severity = BugPattern.SeverityLevel.SUGGESTION
)
public class BeforeCheck extends BugChecker implements BugChecker.AnnotationTreeMatcher
personal static remaining Matcher<AnnotationTree> matcher =
isType("org.junit.Earlier than");
@Override
public Description matchAnnotation(AnnotationTree annotationTree,
VisitorState visitorState)
if (!matcher.matches(annotationTree, visitorState))
return Description.NO_MATCH;
return describeMatch(annotationTree,
SuggestedFix.substitute(annotationTree, "@BeforeEach"));
Error Susceptible and AutoService dependencies are required to construct the customized Error Susceptible plugin:
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
<model>2.15.0</model>
</dependency>
<dependency>
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_check_api</artifactId>
<model>2.15.0</model>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
<model>1.0.1</model>
</dependency>
The AutoService must be configured as an annotation processor:
<annotationProcessorPaths>
<path>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<model>1.0.1</model>
</path>
</annotationProcessorPaths>
Now the customized Error Susceptible plugin may be put in to the native Maven repository with the mvn set up
command. After executing the command, the instance undertaking must be configured to make use of the brand new customized plugin as an annotation processor:
<annotationProcessorPaths>
<path>
<groupId>org.instance.customized.plugin</groupId>
<artifactId>ErrorProneBeforeCheck</artifactId>
<model>1.0-SNAPSHOT</model>
</path>
</annotationProcessorPaths>
The brand new BeforeCheck
must be added to the Error Susceptible evaluation:
<compilerArgs>
<arg>-XDcompilePolicy=easy</arg>
<arg>-Xplugin:ErrorProne -XepPatchChecks:BeforeCheck
-XepPatchLocation:IN_PLACE</arg>
</compilerArgs>
An instance Take a look at class could also be added which incorporates a mixture of each @Earlier than
and @BeforeEach
annotations:
public class ErrorProneTest
@Earlier than
void earlier than()
@BeforeEach
void beforeEach()
When working mvn confirm
the brand new customized Error Susceptible plugin replaces the @Earlier than
annotation with the @BeforeEach
annotation:
public class ErrorProneTest
@BeforeEach
void earlier than()
@BeforeEach
void beforeEach()
Error Susceptible makes use of Java internals, which might be these days hidden, which could end in errors equivalent to:
java.lang.IllegalAccessError: class com.google.errorprone.BaseErrorProneJavaCompiler
(in unnamed module @0x1a6cf771)
can't entry class com.solar.instruments.javac.api.BasicJavacTask (in module jdk.compiler)
as a result of module jdk.compiler doesn't export
com.solar.instruments.javac.api to unnamed module @0x1a6cf771
The solution for Maven is to reveal the Java internals by making a listing known as .mvn within the root of the undertaking containing a jvm.config file with the next content material:
--add-exports jdk.compiler/com.solar.instruments.javac.api=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.file=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.primary=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.mannequin=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.parser=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.processing=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.tree=ALL-UNNAMED
--add-exports jdk.compiler/com.solar.instruments.javac.util=ALL-UNNAMED
--add-opens jdk.compiler/com.solar.instruments.javac.code=ALL-UNNAMED
--add-opens jdk.compiler/com.solar.instruments.javac.comp=ALL-UNNAMED
Alternatively the --add-exports
and --add-opens
configuration could also be equipped to the Maven Compiler Plugin’s arguments within the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<model>3.10.1</model>
<configuration>
<compilerArgs>
<arg>--add-exports</arg>
<arg>jdk.compiler/com.solar.instruments.javac.util=ALL-UNNAMED</arg>
…
Extra details about utilizing Error Susceptible with Bazel, Ant and Gradle may be discovered within the installation instructions.