Skip to content

Conversation

@nielsdebruin
Copy link
Contributor

@nielsdebruin nielsdebruin commented Oct 14, 2024

What's changed?

The PR implements the recipe outlined in #328. The new recipe will automatically add a @org.jspecify.annotation.Nullable to non-private methods that may return null. It does so by scanning methods and checking their return statements for potential null values. Apart from the literal return null it will also take methods on collection which are know to be null (based on : error-prone), such as Map.get, Map.put, Queue.poll..etc. Null that are the result of lambda, or streams, are currently ignored.

What's your motivation?

To quote this issue #328:

Nullability annotations on method return types help tools and devs to better understand and reason about the code. Folks might forget to add these annotations though. A recipe could look through the body of a method and add the missing nullability annotations on the return type.

Anything in particular you'd like reviewers to focus on?

This is my first recipe, so I might have formatted make some call, etc.

Anyone you would like to review specifically?

@timtebeek

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-35
  • src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java
    • lines 28-28
    • lines 59-60

@nielsdebruin nielsdebruin force-pushed the add-nullable-annotations branch from e295fc3 to 4a6a356 Compare October 14, 2024 09:31
@nielsdebruin nielsdebruin force-pushed the add-nullable-annotations branch from 4a6a356 to 8bf01d4 Compare October 14, 2024 09:31
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-35
  • src/main/java/org/openrewrite/staticanalysis/UseCollectionInterfaces.java
    • lines 28-28
    • lines 59-60

timtebeek and others added 2 commits October 14, 2024 12:08
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

@nielsdebruin
Copy link
Contributor Author

Hi @timtebeek I see that in your suggested change you have moved the @Nullable after the access modifier instead of before, which is counter to the issue #328. Can I assume that this will be the final way, or should it be place before after all?

Copy link
Contributor

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leverage functional style and existing APIs over boilerplate imperative approaches to maximize code efficiency and readability.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

@nielsdebruin nielsdebruin force-pushed the add-nullable-annotations branch from 13907c2 to 21873f9 Compare October 16, 2024 08:44
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

…ethods.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

@timtebeek
Copy link
Member

timtebeek commented Oct 22, 2024

I've been running this recipe at scale against serialized LSTs from OpenRewrite and Apache Maven projects, but so far do not see the recipe making any changes there; any thoughts on that? I'll try a few more iterations, but might make sense to quickly sync so you can do the same.

Seeing the suggested changes now after setting my recipe as the active recipe from my IDE and then running

 mod run . --active-recipe

One minor imperfection on the second one here; notice the Nullable annotation appears twice.
image

Maybe it's good to either fix NullableOnMethodReturnType to not allow these duplicates, or not rely on NullableOnMethodReturnType for pushing the annotation down from the method to the return type, but immediately adding it on the return type.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-36

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java
    • lines 35-35

@timtebeek
Copy link
Member

Some suggestions could not be made:

  • src/main/java/org/openrewrite/staticanalysis/ChainStringBuilderAppendCalls.java

    • lines 35-35

If you see these types of messages your branch is likely behind on the main branch that contains the fixes. Be sure to click this button in such cases to reduce the noise on your PRs:
image

Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see! Would you might once more running it against OpenRewrite from your IDE as a preview, and then once we merge this run it through the Moderne platform across all OpenRewrite repositories & created PRs to add these missing annotations and potentially resolve any nullability issues that then can reported downstream.

methodDeclaration.getMethodType() == null ||
methodDeclaration.getMethodType().getReturnType() instanceof JavaType.Primitive ||
service(AnnotationService.class).matches(getCursor(), NULLABLE_ANNOTATION_MATCHER) ||
service(AnnotationService.class).matches(new Cursor(null, methodDeclaration.getReturnTypeExpression()), NULLABLE_ANNOTATION_MATCHER)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My IDE gives me a warning on this line; something this recipe intends to make more common:
image

Be sure to enable that warning in your IDE if you haven't already, and keep pushing F2 and Alt + Enter to resolve issues before a commit/push and definitely before a merge.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once that's no longer yellow we can merge. :)

@timtebeek timtebeek merged commit 81459cb into openrewrite:main Oct 25, 2024
@nielsdebruin nielsdebruin deleted the add-nullable-annotations branch October 27, 2024 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Add missing nullable annotation on method declaration return type

3 participants