Skip to content

[ErrorProne] Fix BadInstanceof and enable check#37753

Open
stankiewicz wants to merge 4 commits intoapache:masterfrom
stankiewicz:error_prone_badInstanceOf
Open

[ErrorProne] Fix BadInstanceof and enable check#37753
stankiewicz wants to merge 4 commits intoapache:masterfrom
stankiewicz:error_prone_badInstanceOf

Conversation

@stankiewicz
Copy link
Contributor

This PR removes the BadInstanceof ErrorProne check from the disabledChecks list in src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy to enforce it globally.
Additionally, it resolves all existing BadInstanceof warnings across the project. Most of these violations consisted of checking if an object was an instance of its own declared type or a supertype (which is always true unless the object is null). To resolve these, the redundant instanceof checks have been replaced with appropriate null checks (!= null or == null).


Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:

  • Mention the appropriate issue in your description (for example: addresses #123), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment fixes #<ISSUE NUMBER> instead.
  • Update CHANGES.md with noteworthy changes.
  • If this contribution is large, please file an Apache Individual Contributor License Agreement.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md

GitHub Actions Tests Status (on master branch)

Build python source distribution and wheels
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

…lability checks across various components.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances code quality by enforcing a previously disabled ErrorProne check, BadInstanceof, throughout the codebase. The primary impact is the removal of unnecessary type checks that were effectively checking for nullability, leading to cleaner and more robust code by explicitly handling null conditions where appropriate.

Highlights

  • ErrorProne Check Enabled: The BadInstanceof ErrorProne check has been removed from the disabled checks list in BeamModulePlugin.groovy, enabling it globally across the project.
  • Code Quality Improvement: All existing BadInstanceof warnings have been resolved by replacing redundant instanceof checks (which were always true for non-null objects) with explicit null checks (!= null or == null).

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy
    • Removed 'BadInstanceof' from the list of disabled ErrorProne checks.
  • runners/flink/2.0/src/test/java/org/apache/beam/runners/flink/translation/functions/ImpulseSourceFunctionTest.java
    • Replaced instanceof WindowedValue with a null check o != null in the matches method.
  • runners/flink/src/main/java/org/apache/beam/runners/flink/FlinkStreamingTranslationContext.java
    • Updated output coders stream filter from instanceof PCollection to e.getValue() != null.
  • runners/flink/src/main/java/org/apache/beam/runners/flink/PipelineTranslationModeOptimizer.java
    • Changed hasUnboundedOutput filter from instanceof PCollection to value != null.
  • runners/flink/src/main/java/org/apache/beam/runners/flink/translation/utils/LookupPipelineVisitor.java
    • Replaced instanceof PCollection with e.getValue() != null in getOutputCoders.
  • runners/flink/src/test/java/org/apache/beam/runners/flink/translation/functions/ImpulseSourceFunctionTest.java
    • Replaced instanceof WindowedValue with a null check o != null in the matches method.
  • runners/jet/src/main/java/org/apache/beam/runners/jet/Utils.java
    • Changed getOutputValueCoders filter from instanceof PCollection to e.getValue() != null.
  • runners/samza/src/main/java/org/apache/beam/runners/samza/translation/ParDoBoundMultiTranslator.java
    • Updated doTranslate output coders filter to e.getValue() != null.
    • Changed if (!(taggedOutput.getValue() instanceof PCollection)) to if (!(taggedOutput.getValue() != null)).
  • runners/samza/src/main/java/org/apache/beam/runners/samza/translation/SplittableParDoTranslators.java
    • Changed conditional check from instanceof PCollection to taggedOutput.getValue() != null.
  • runners/spark/src/main/java/org/apache/beam/runners/spark/translation/EvaluationContext.java
    • Updated getOutputCoders filter from instanceof PCollection to e.getValue() != null.
  • runners/twister2/src/main/java/org/apache/beam/runners/twister2/Twister2TranslationContext.java
    • Changed getOutputCoders filter from instanceof PCollection to e.getValue() != null.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/util/construction/PTransformReplacements.java
    • Replaced input.getValue() instanceof PCollection with input.getValue() != null in getSingletonMainInput.
    • Updated error message to handle null input.getValue().
  • sdks/java/core/src/main/java/org/apache/beam/sdk/util/construction/ParDoTranslation.java
    • Updated getOutputCoders filter from instanceof PCollection to e.getValue() != null.
  • sdks/java/expansion-service/src/main/java/org/apache/beam/sdk/expansion/service/TransformProvider.java
    • Changed else if (output instanceof POutput) to else if (output != null) in extractOutputs.
  • sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/ReadSpannerSchemaTest.java
    • Replaced !(argument instanceof Statement) with argument == null in prepareColumnMetadata.
    • Replaced !(argument instanceof Statement) with argument == null in preparePkMetadata.
  • sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/spanner/SpannerIOWriteTest.java
    • Replaced !(argument instanceof Statement) with argument == null in prepareColumnMetadata.
    • Replaced !(argument instanceof Statement) with argument == null in preparePgColumnMetadata.
    • Replaced !(argument instanceof Statement) with argument == null in preparePkMetadata.
    • Replaced !(argument instanceof Iterable) with argument == null in mutationsInNoOrder.
  • sdks/java/testing/nexmark/src/main/java/org/apache/beam/sdk/nexmark/queries/Query4Model.java
    • Changed Assert.assertTrue condition from obj.getValue() instanceof CategoryPrice to obj.getValue() != null.
  • sdks/java/testing/nexmark/src/main/java/org/apache/beam/sdk/nexmark/queries/Query6Model.java
    • Changed Assert.assertTrue condition from obj.getValue() instanceof SellerPrice to obj.getValue() != null.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@stankiewicz
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enables the BadInstanceof ErrorProne check globally and fixes the existing violations. The changes correctly replace redundant instanceof checks with null checks. The approach is sound and the modifications across the various files are consistent with the goal. I've added a couple of suggestions to improve readability by simplifying a boolean expression, consistent with changes made in other parts of this PR.

stankiewicz and others added 3 commits March 3, 2026 22:47
…slation/SplittableParDoTranslators.java

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
…slation/ParDoBoundMultiTranslator.java

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

github-actions bot commented Mar 4, 2026

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@stankiewicz
Copy link
Contributor Author

Run Java_Amazon-Web-Services2_IO_Direct PreCommit

@stankiewicz stankiewicz changed the title Fix ErrorProne BadInstanceof warnings and enable check globally [ErrorProne] Fix BadInstanceof and enable check Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant