Error Inclined, a Java compiler plugin open sourced by Google, performs static evaluation throughout compilation to detect bugs or counsel attainable enhancements. The plugin accommodates greater than 500 pre-defined bug checks and permits third social gathering and customized plugins. After detecting points, Error Inclined can show a warning or mechanically change the code with a predefined resolution. Error Inclined 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 might be discovered within the documentation. The compiler needs to be configured to make use of Error Inclined as an annotation processor, for instance when making a take a look at challenge 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 might be created. Take into account the next methodology 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 evaluate(String firstList[], String secondList[]) {
return firstList.equals(secondList);
}
Executing mvn clear confirm
triggers the Error Inclined evaluation and leads to the next error message:
[ERROR] Did not execute aim org.apache.maven.plugins:maven-compiler-plugin:3.10.1:
compile (default-compile) on challenge ErrorProne: Compilation failure
[ERROR] …/ErrorProne/src/principal/java/org/instance/Essential.java:[5,28]
[ArrayEquals] Reference equality used to check arrays
[ERROR] (see https://errorprone.information/bugpattern/ArrayEquals)
[ERROR] Did you imply 'return Arrays.equals(firstList, secondList);'?
The ArrayEquals bug sample is discovered and Error Inclined suggests to vary the implementation with the intention to evaluate the content material of the array as a substitute of the objects:
return Arrays.equals(firstList, secondList);
Receiving an error helps to enhance the code, however it’s additionally attainable to let Error Inclined apply the answer mechanically. The -XepPatchChecks
argument ought to comprise a comma separated listing 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 mechanically modified to:
public boolean evaluate(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 corresponding to SLF4J or to create a customized plugin. The supply code for the built-in guidelines gives numerous examples which may be used as the idea for a brand new plugin. For instance, a customized Error Inclined plugin can exchange the older @Earlier than
JUnit annotation with the brand new JUnit 5 @BeforeEach
annotation.
The customized Error Inclined plugin needs to be positioned in one other Maven module than the instance challenge proven earlier. Error Inclined makes use of the service loader mechanism to load the bug checks. Usually, that requires some configuration, nonetheless Google’s AutoService challenge simplifies the configuration through the use of the @AutoService
annotation. The @BugPattern
annotation is used to configure the title, 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(
title = "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 {
non-public static closing 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.exchange(annotationTree, "@BeforeEach"));
}
}
Error Inclined and AutoService dependencies are required to construct the customized Error Inclined 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 needs to 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 Inclined plugin might be put in to the native Maven repository with the mvn set up
command. After executing the command, the instance challenge needs to 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
needs to be added to the Error Inclined 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 accommodates 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 Inclined plugin replaces the @Earlier than
annotation with the @BeforeEach
annotation:
public class ErrorProneTest {
@BeforeEach
void earlier than() {
}
@BeforeEach
void beforeEach() {
}
}
Error Inclined makes use of Java internals, which might be these days hidden, which could lead to errors corresponding 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 resolution for Maven is to reveal the Java internals by making a listing referred to as .mvn within the root of the challenge 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.principal=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 Inclined with Bazel, Ant and Gradle might be discovered within the set up directions.