Thursday, May 2, 2024
HomeJavaError Susceptible Assist Introduces New Bug Checks and Refaster Templates

Error Susceptible Assist Introduces New Bug Checks and Refaster Templates


Picnic, the “grocery store on wheels” firm, has launched Error Susceptible Assist, an open supply challenge that extends Google’s Error Susceptible, that comprises thirty new bug checks to detect, and optionally resolve, potential errors throughout compilation of a Java challenge. Greater than fifty new Refaster templates can be found to enhance code uniformity by routinely rewriting the codebase.

Error Susceptible is a Java compiler plugin, open sourced by Google, which performs static evaluation throughout compilation to detect bugs or counsel potential enhancements. The plugin comprises greater than 500 pre-defined bug checks and permits third occasion and customized plugins. After detecting points, Error Susceptible can show a warning or routinely change the code with a predefined answer.

One of many thirty new bug checks supplied by the just lately launched Error Susceptible Assist is the EmptyMethod class, a bug test which is able to show a warning or optionally take away the next strategies:


void methodology() {}
static void staticMethod() {}

Each time an empty methodology is important, the false optimistic could also be suppressed with the next annotation:


@SuppressWarnings("EmptyMethod")

The AutowiredConstructor bug test removes the redundant @Autowired annotation every time a category has only one constructor as within the following examples:


class Course {
	@Autowired
	Course() {}
}

class Pupil {
	@Autowired
	Pupil(String identify) {}
}

The MethodReferenceUsage class detects lambda expressions and converts them to methodology references. For instance, () -> course.methodology() is modified to course::methodology.

Writing a bug test in Error Susceptible includes working with summary syntax timber (AST) and Error Susceptible’s APIs. That is typically essential to detect bugs, however principally not when rewriting code. Google’s Refaster is simpler to make use of and now a part of Error Susceptible. Refaster guidelines could also be used to outline rewrite guidelines in Java with @BeforeTemplate and @AfterTemplate annotations. Error Susceptible Assist affords greater than fifty new Refaster guidelines, such because the StringRules class, which replaces numerous expressions to guage whether or not a String is empty with the String.isEmpty() methodology:


@BeforeTemplate
boolean earlier than(String str) {
    return Refaster.anyOf(str.size() == 0, str.size() <= 0,   
        str.size() < 1);
}

@AfterTemplate
@AlsoNegation
boolean after(String str) {
    return str.isEmpty();
}

The @AlsoNegation annotation signifies that the rule also can match the logical negation of the @BeforeTemplate our bodies. For instance, the code str.size() != 0 can be modified to !str.isEmpty().

The TimeRules class comprises numerous rewrite guidelines for time expressions resembling changing numerous Zone offsets with UTC:


@BeforeTemplate
ZoneId earlier than() {
    // `ZoneId.of("Z")` isn't listed, as a result of Error Susceptible flags it out of the field.
    return Refaster.anyOf(
        ZoneId.of("GMT"),
        ZoneId.of("UTC"),
        ZoneId.of("+0"),
        ZoneId.of("-0"),
        UTC.normalized(),
        ZoneId.from(UTC));
}

@AfterTemplate
ZoneOffset after() {
    return UTC;
}

Or changing the compareTo methodology with the extra readable isBefore methodology:


@BeforeTemplate
boolean earlier than(Immediate a, Immediate b) {
    return a.compareTo(b) < 0;
}

@AfterTemplate
@AlsoNegation
boolean after(Immediate a, Immediate b) {
    return a.isBefore(b);
}

The set up directions for Error Susceptible can be utilized as a foundation, as Error Susceptible Assist is constructed on high of it. After that, the related Error Susceptible Assist modules must be added to the annotationProcessorPaths, for instance, with the maven-compiler-plugin:


<annotationProcessorPaths>
    <!-- Error Susceptible. -->
    <path>
        <groupId>com.google.errorprone</groupId>
        <artifactId>error_prone_core</artifactId>
        <model>${error-prone.model}</model>
    </path>
    <!-- Error Susceptible Assist's bug checks. -->
    <path>
        <groupId>tech.picnic.error-prone-support</groupId>
        <artifactId>error-prone-contrib</artifactId>
        <model>${error-prone-support.model}</model>
    </path>
    <!-- Error Susceptible Assist's Refaster guidelines. -->
    <path>
        <groupId>tech.picnic.error-prone-support</groupId>
        <artifactId>refaster-runner</artifactId>
        <model>${error-prone-support.model}</model>
    </path>
</annotationProcessorPaths>

InfoQ spoke to Rick Ossendrijver, Software program Engineer at Picnic and one of many creators of Error Susceptible Assist:

InfoQ: What made you resolve to create Error Susceptible Assist as a substitute of including the bug checks to Error Susceptible?

Ossendrijver: Google Error Susceptible would not settle for many new checks and Error Susceptible Assist comprises some Picnic-opinionated checks. Releasing the bug checks individually additionally prevents overloading the Google maintainers of Error Susceptible with contributions. A separate challenge additionally permits us to launch quicker and extra typically with out being depending on the Error Susceptible maintainers.

InfoQ: Is the main target of Error Susceptible Assist on code uniformity, bug detection, or each?

Ossendrijver: We positively give attention to each. We extremely worth code consistency (uniformity). For code uniformity we favor refaster guidelines as they’re simpler to put in writing then bug checks which require extra in-depth Java information. For bug detection one normally wants to put in writing a bug test as a extra thorough evaluation is required.

InfoQ: Are you able to already inform us one thing about upcoming options?

Ossendrijver: Sure for certain, we plan numerous enhancements. Initially, the present matching algorithm is comparatively gradual and we made some main enhancements to hurry up Refaster.


Subsequent to that, Error Susceptible bug checks present compilation warnings or errors as proven within the instance on the web site. The message gives a hyperlink to the documentation of that particular bug test. We’ve got help for operating Refaster guidelines “as a bug test”. That signifies that we additionally present compilation warnings or errors. Quickly we are going to add hyperlinks to the documentation web site when a Refaster rule matches within the code. On high of that, we can be introducing new annotations and processing thereof, to supply higher error messages when a Refaster rule flags one thing.


Lastly, we are going to introduce extra bug checks and Refaster guidelines. Our Maven `self-check` profile applies these bug checks and Refaster guidelines on the codebase itself. That method we are able to simply improve the standard of our codebase.

Error Susceptible Assist welcomes contributions and gives contribution pointers.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments