Sunday, April 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

Error Prone Improves Java Code by Detecting Common Mistakes

learningcode_x1mckf by learningcode_x1mckf
October 27, 2022
in Java
0
Error Prone Improves Java Code by Detecting Common Mistakes
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

So why did they decide to call it Java? – InfoWorld

Senior Java Developer – IT-Online

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

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.





Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

So why did they decide to call it Java? – InfoWorld

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

So why did they decide to call it Java?  InfoWorld Source link

Read more

Senior Java Developer – IT-Online

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Senior Java Developer  IT-On-line Source link

Read more

West Java to provide simultaneous polio vaccinations from Apr 3 – ANTARA English

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

West Java to provide simultaneous polio vaccinations from Apr 3  ANTARA English Source link

Read more

COBOL programming skills gap thwarts modernization to Java – TechTarget

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

COBOL programming skills gap thwarts modernization to Java  TechTarget Source link

Read more

User input with a Java JOptionPane example – TheServerSide.com

by learningcode_x1mckf
April 1, 2023
0
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

User input with a Java JOptionPane example  TheServerSide.com Source link

Read more
Next Post
Programming languages: Why Meta is moving its Android apps from Java to Kotlin

Programming languages: Why Meta is moving its Android apps from Java to Kotlin

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Solidity vs JavaScript: Which Metaverse Language Will Yield a … – Analytics Insight

March 15, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

JetBrains updates IDEs for Java, JavaScript, Rails – InfoWorld

March 30, 2023
Google expands open source bounties, will soon support Javascript fuzzing too – ZDNet

Oracle per-employee Java licensing could benefit rivals – InfoWorld

February 6, 2023

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • So why did they decide to call it Java? – InfoWorld
  • Senior Java Developer – IT-Online
  • 4 Packages for Working With Date and Time in JavaScript – MUO – MakeUseOf

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?