Skip to content

JSpecifyBestPractices ignores my annotation and rewrites the code #7741

Description

@beeet

Setup

        <plugin>
            <groupId>org.openrewrite.maven</groupId>
            <artifactId>rewrite-maven-plugin</artifactId>
            <version>6.38.0</version>
            <executions>
                <execution>
                    <id>run-rewrite</id>
                    <phase>process-sources</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <exportDatatables>false</exportDatatables>
                <activeRecipes>
                    <recipe>org.openrewrite.java.jspecify.JSpecifyBestPractices</recipe>
                </activeRecipes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.openrewrite.recipe</groupId>
                    <artifactId>rewrite-migrate-java</artifactId>
                    <version>3.34.0</version>
                </dependency>
        </plugin>

My Code BEFORE running rewrite (without JSpecify):

   public byte[] generate(final PrintMode printMode, final PdfFragebogenUiStateDto uiStateDto) throws PdfGenerationException {
        if (printMode == null) {
            throw new IllegalArgumentException("my concrete business exception");
        }
        if (uiStateDto == null) {
            throw new IllegalArgumentException("my concrete business exception");
        }
        //... below the logic
}

My Code AFTER running rewrite:

   public byte[] generate(@NonNull final PrintMode printMode, @NonNull final PdfFragebogenUiStateDto uiStateDto) throws PdfGenerationException {
        // the throwing of the IllegalArgumentExceptions is removed
        //... below the logic
}

I understand this behaviour of removing the exceptions, if NO JSpecify-Annotation is configured.

But if I want to throw an explicit business-related exception (we handle it in the ExceptionHandler), then that shouldn't be rewritten. In such cases, we want to display business-related messages.

So, the following code shouldn't IMHO not rewritten:

   public byte[] generate(@Nullable final PrintMode printMode, @Nullable final PdfFragebogenUiStateDto uiStateDto) throws PdfGenerationException {
        if (printMode == null) {
            throw new IllegalArgumentException("my concrete business exception");
        }
        if (uiStateDto == null) {
            throw new IllegalArgumentException("my concrete business exception");
        }
        //... below the logic
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions