Stephan Schroevers 52932ab538 This is a squash commit of the following previous commits:
Merge branch 'master' into website

---

Undo Refaster test package split, glue it together

---

Introduce `RefasterRuleTestExtractor` for documentation generation

This new `Extractor` implementation collects Refaster example input and
output code from rule collection tests.

This change also introduces explicit compilation steps for the test
code. As a side-effect this produces faster feedback in case of invalid
input or output code.

(cherry picked from commit c8c51078332388055746b433318148f428239c18)

---

Try fix build

---

Post-rebase fix

---

Publish Error Prone compatibility matrix on website

The new `website/generate-version-compatibility-overview.sh` script
tests all combinations, and stores the result in a Jekyll data file.

---

Some cleanup

---

Make tests more maintainable

---

Try to fix htmlproofer

---

Fix source links

---

Introduce `BugPatternTestExtractor` with tests

---

Simplify tests

---

Further simplify testing setup

---

Kill another mutant and drop unused imports

---

This actually kills the mutant

---

Suggestions

---

Suggestions

---

Suggestions

---

Fix JDK 11 compatibility

---

PSM-1717 Pass `ClassLoader` to `ServiceLoader`

---

Introduce documentation generation

This is a squash commit of the following previous commits:

---

Introduce `documentation-support` module to extract website data from source code

By adding a compilation `TaskListener` that extracts data from the Bug pattern
and Refaster rule collection (test) classes and writing to JSON output files
in the target directory. This extraction happens as part of the Maven build
using the `docgen` profile.

---

Improve website styling

Co-authored-by: japborst <japborst@gmail.com>
Co-authored-by: Gijs de Jong <berichtaangijs@gmail.com>

---

Generate Markdown files from existing content for the website

---

Upgrade dependencies to the latest versions

---

Compile and install project jars before docgen

---

Run validation in build and exclude self-url

---

Reintroduce `htmlproofer`, improve templates, cleanup setup and dependencies

---

Delete directory if it exists

---

Add SCSS for GitHub button and fix bug pattern sample rendering

---

Fix bug pattern GitHub link

---

Small styling tweaks (incl. for dark theme)

---

Use single mvn command

---

Move mustache templates

---

Hardcode anchors for headings

---

Add supression to bugpatterns

---

Remove self ignore for html-proofer

---

Add refaster supressions and use callouts

---

Use v0.4.1-SNAPSHOT

---

Revert "Use single mvn command"

This reverts commit 594471d1ed23a1c19d7fe88d925d1b7f828716cd.

---

Extract Refaster samples from source code instead of AST

---

Skip verification, for now

---

Add notes on disabling bugpatterns

---

Set default layout and image

---

Fix mobile navigation

---

Revert "Set default layout and image"

This reverts commit 67a4aa7b5b4d14c0f2b783f345f53affe6ef3ec5.

---

Add supression for refaster rules

---

Post-rebase fixes

---

Fix the tests

---

Doh

---

Version bump

---

Exclude ThirdPartyLibraryTest from Bug Pattern test output

---

Remove only last occurence of 'Test' in Bug Pattern tests doc generation

---

Move `MapRulesTest` resources

---

Add exclusion for docgen of `SourceCodeTest`

---

Bump version

---

Extra fixes after rebase

---

Delete custom nav footer and `assets/images/favicon.ico`

---

Post rebase fixes with version bump to `0.8.1-SNAPSHOT`

---

Use new BugPatternTest extraction method

---

Fit Refaster extractors into new documentation support setup

---

Drop Docgen Maven profile

---

Move new rule collection

---

Improve extractor matching and update README

---

Move `SuggestedFixRules` test files

---

Fix Refaster exclusion regexes

---

Disable external link checking until the `website` branch is up-to-date with `master` again

---

Post-rebase fix

---

Not sure why `-Dverification.skip` fails while `-Dverification.warn` doesn't; won't investigate right now

---

Check external links again

---

Upgrade dependencies

---

WIP: towards dropping Mustache

---

WIP: Java doc generator

---

WIP: switch over

---

WIP: Cleanup

---

Polish

---

Bump dependencies

---

Bump Ruby

---

Sync more

---

Fix bug checker GitHub URLs

---

Fix HTMLProofer config
2025-03-26 14:07:02 +01:00

Error Prone Support logo

Error Prone Support

Error Prone Support is a Picnic-opinionated extension of Google's Error Prone. It aims to improve code quality, focussing on maintainability, consistency and avoidance of common pitfalls.

Error Prone is a static analysis tool for Java that catches common programming mistakes at compile-time.

To learn more about Error Prone (Support), how you can start using Error Prone in practice, and how we use it at Picnic, watch the conference talk Automating away bugs with Error Prone in practice. Also consider checking out the blog post Picnic loves Error Prone: producing high-quality and consistent Java code.

Maven Central Reproducible Builds OpenSSF Best Practices OpenSSF Scorecard CodeQL Analysis GitHub Actions Mutation tested with PIT Quality Gate Status Maintainability Rating Reliability Rating Security Rating Coverage Duplicated Lines (%) Technical Debt License PRs Welcome

Getting startedDeveloping Error Prone SupportHow it worksContributing


Getting started

Installation

This library is built on top of Error Prone. To use it, read the installation guide for Maven or Gradle below. The library requires that your build is executed using JDK 17 or above, but supports builds that target older versions of Java.

Maven

  1. First, follow Error Prone's installation guide.

  2. Next, edit your pom.xml file to add one or more Error Prone Support modules to the annotationProcessorPaths of the maven-compiler-plugin:

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <!-- Prefer using the latest release. -->
                    <version>3.12.0</version>
                    <configuration>
                        <annotationProcessorPaths>
                            <!-- Error Prone itself. -->
                            <path>
                                <groupId>com.google.errorprone</groupId>
                                <artifactId>error_prone_core</artifactId>
                                <version>${error-prone.version}</version>
                            </path>
                            <!-- Error Prone Support's additional bug checkers. -->
                            <path>
                                <groupId>tech.picnic.error-prone-support</groupId>
                                <artifactId>error-prone-contrib</artifactId>
                                <version>${error-prone-support.version}</version>
                            </path>
                            <!-- Error Prone Support's Refaster rules. -->
                            <path>
                                <groupId>tech.picnic.error-prone-support</groupId>
                                <artifactId>refaster-runner</artifactId>
                                <version>${error-prone-support.version}</version>
                            </path>
                        </annotationProcessorPaths>
                        <compilerArgs>
                            <arg>
                                -Xplugin:ErrorProne
                                <!-- Add other Error Prone flags here. See
                                https://errorprone.info/docs/flags. -->
                            </arg>
                            <arg>-XDcompilePolicy=simple</arg>
                        </compilerArgs>
                        <!-- Enable this if you'd like to fail your build upon warnings. -->
                        <!-- <failOnWarning>true</failOnWarning> -->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
    

Gradle

  1. First, follow the installation guide of the gradle-errorprone-plugin.

  2. Next, edit your build.gradle file to add one or more Error Prone Support modules:

    dependencies {
        // Error Prone itself.
        errorprone("com.google.errorprone:error_prone_core:${errorProneVersion}")
        // Error Prone Support's additional bug checkers.
        errorprone("tech.picnic.error-prone-support:error-prone-contrib:${errorProneSupportVersion}")
        // Error Prone Support's Refaster rules.
        errorprone("tech.picnic.error-prone-support:refaster-runner:${errorProneSupportVersion}")
    }
    
    tasks.withType(JavaCompile).configureEach {
        options.errorprone.disableWarningsInGeneratedCode = true
        // Add other Error Prone flags here. See:
        // - https://github.com/tbroyer/gradle-errorprone-plugin#configuration
        // - https://errorprone.info/docs/flags
    }
    

Seeing it in action

Consider the following example code:

import com.google.common.collect.ImmutableSet;
import java.math.BigDecimal;

public class Example {
  static BigDecimal getNumber() {
    return BigDecimal.valueOf(0);
  }

  public ImmutableSet<Integer> getSet() {
    ImmutableSet<Integer> set = ImmutableSet.of(1);
    return ImmutableSet.copyOf(set);
  }
}

If the installation was successful, then building the above code with Maven should yield two compiler warnings:

$ mvn clean install
...
[INFO] Example.java:[9,34] [Refaster Rule] BigDecimalRules.BigDecimalZero: Refactoring opportunity
    (see https://error-prone.picnic.tech/refasterrules/BigDecimalRules#BigDecimalZero)
  Did you mean 'return BigDecimal.ZERO;'?
...
[WARNING] Example.java:[13,35] [IdentityConversion] This method invocation appears redundant; remove it or suppress this warning and add a comment explaining its purpose
    (see https://error-prone.picnic.tech/bugpatterns/IdentityConversion)
  Did you mean 'return set;' or '@SuppressWarnings("IdentityConversion") public ImmutableSet<Integer> getSet() {'?
...

Two things are kicking in here:

  1. An Error Prone BugChecker that flags unnecessary identity conversions.
  2. A Refaster rule capable of rewriting expressions of the form BigDecimal.valueOf(0) and new BigDecimal(0) to BigDecimal.ZERO.

Be sure to check out all bug checks and refaster rules.

👷 Developing Error Prone Support

This is a Maven project, so running mvn clean install performs a full clean build and installs the library to your local Maven repository.

Once you've made changes, the build may fail due to a warning or error emitted by static code analysis. The flags and commands listed below allow you to suppress or (in a large subset of cases) automatically fix such cases. Make sure to carefully check the available options, as this can save you significant amounts of development time!

Relevant Maven build parameters:

  • -Dverification.warn makes the warnings and errors emitted by various plugins and the Java compiler non-fatal, where possible.
  • -Dverification.skip disables various non-essential plugins and compiles the code with minimal checks (i.e. without linting, Error Prone checks, etc.).
  • -Dversion.error-prone=some-version runs the build using the specified version of Error Prone. This is useful e.g. when testing a locally built Error Prone SNAPSHOT.
  • -Perror-prone-fork runs the build using Picnic's Error Prone fork, hosted using GitHub Packages. This fork generally contains a few changes on top of the latest Error Prone release. Using this profile generally requires passing -s settings.xml, with suitably configured GITHUB_ACTOR and GITHUB_TOKEN environment variables.
  • -Pself-check runs the checks defined by this project against itself.

Other highly relevant commands:

  • mvn fmt:format formats the code using google-java-format.
  • ./run-full-build.sh builds the project twice, where the second pass validates compatibility with Picnic's Error Prone fork and compliance of the code with any rules defined within this project. (Consider running this before opening a pull request, as the PR checks also perform this validation.)
  • ./apply-error-prone-suggestions.sh applies Error Prone and Error Prone Support code suggestions to this project. Before running this command, make sure to have installed the project (mvn clean install) and make sure that the current working directory does not contain unstaged or uncommited changes.
  • ./run-branch-mutation-tests.sh uses Pitest to run mutation tests against code that is modified relative to the upstream default branch. The results can be reviewed by opening the respective target/pit-reports/index.html files. One can use ./run-mutation-tests.sh to run mutation tests against all code in the current working directory. For more information check the PIT Maven plugin.

Opening the project in IntelliJ IDEA may require running mvn clean install first. Additionally, when running the project's tests using the IDE, you might see the following error:

java: exporting a package from system module jdk.compiler is not allowed with --release

If this happens, go to Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler and deselect the option Use '--release' option for cross-compilation (Java 9 and later). See IDEA-288052 for details.

💡 How it works

This project provides additional BugChecker implementations.

✍️ Contributing

Want to report or fix a bug, suggest or add a new feature, or improve the documentation? That's awesome! Please read our contribution guidelines.

Security

If you want to report a security vulnerability, please do so through a private channel; please see our security policy for details.

Description
No description provided
Readme 9.9 MiB
Languages
Java 98.9%
Shell 1%