Skip to content

[ErrorProne] Fix UnnecessaryLambda Error Prone issues#37765

Merged
kennknowles merged 1 commit intoapache:masterfrom
stankiewicz:error_prone_UnnecessaryLambda
Mar 6, 2026
Merged

[ErrorProne] Fix UnnecessaryLambda Error Prone issues#37765
kennknowles merged 1 commit intoapache:masterfrom
stankiewicz:error_prone_UnnecessaryLambda

Conversation

@stankiewicz
Copy link
Contributor

This PR removes the UnnecessaryLambda check from the disabledChecks list in BeamModulePlugin.groovy and addresses all resulting compilation errors. The UnnecessaryLambda pattern flags lambdas that are saved in constants or returned from helper methods, recommending direct method implementations or method references instead.
I refactored these cases across the codebase, resulting in cleaner and more idiomatic Java code by utilizing method references where appropriate.


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.

@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 focuses on improving code quality and maintainability by re-enabling a specific Error Prone check and subsequently refactoring existing lambda expressions into more concise and idiomatic method references. This effort streamlines the codebase, making it easier to read and potentially more performant by leveraging Java's method reference capabilities.

Highlights

  • Error Prone Check Re-enabled: The UnnecessaryLambda Error Prone check has been re-enabled by removing it from the disabledChecks list in BeamModulePlugin.groovy. This check flags lambdas that can be replaced by direct method implementations or method references.
  • Lambda Refactoring: Numerous lambda expressions across the codebase, particularly those assigned to Function, BiFunction, Runnable, Supplier, and Predicate interfaces, have been refactored into static or instance method references. This change improves code readability and adheres to the UnnecessaryLambda check's recommendations.

🧠 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 'UnnecessaryLambda' from the list of disabled Error Prone checks.
  • runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/StreamingDataflowWorkerTest.java
    • Replaced a static Function lambda with a static helper method emptyDataResponder.
    • Updated all usages of the EMPTY_DATA_RESPONDER lambda to use the emptyDataResponder method reference.
  • runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/status/WorkerStatusPagesTest.java
    • Removed unused java.util.function.BooleanSupplier import.
    • Replaced a BooleanSupplier lambda with a private method mockHealthyIndicator and updated its usage to a method reference.
  • runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/streaming/harness/SingleSourceWorkerHarnessTest.java
    • Removed unused java.util.function.Function import.
    • Replaced Runnable and Function lambdas with private methods waitForResources and computationStateFetcher, and updated their usages to method references.
  • runners/google-cloud-dataflow-java/worker/src/test/java/org/apache/beam/runners/dataflow/worker/windmill/work/refresh/ActiveWorkRefresherTest.java
    • Replaced a static Supplier<Instant> lambda with a static helper method aLongTimeAgo and updated its usage to a method reference.
  • runners/spark/3/src/main/java/org/apache/beam/runners/spark/structuredstreaming/translation/helpers/EncoderHelpers.java
    • Removed unused java.util.function.Function import.
    • Replaced a static Function lambda with a static helper method encoderFactory and updated its usage to a method reference.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/fn/test/TestStreams.java
    • Refactored Consumer, Runnable, and Supplier lambdas into static methods throwingErrorHandler, noopRunnable, and alwaysTrueSupplier respectively.
    • Updated calls to use the new static method references.
  • sdks/java/core/src/main/java/org/apache/beam/sdk/util/construction/resources/PipelineResources.java
    • Removed unused java.util.function.Predicate import.
    • Replaced a static Predicate lambda with a static helper method isStageable and updated its usage to a method reference.
  • sdks/java/core/src/test/java/org/apache/beam/sdk/transforms/errorhandling/BadRecordRouterTest.java
    • Removed unused java.util.function.BiFunction import.
    • Replaced a static BiFunction lambda with a static helper method ignoreStacktraceMatcher and updated its usage.
  • sdks/java/core/src/test/java/org/apache/beam/sdk/util/UnboundedScheduledExecutorServiceTest.java
    • Replaced static Runnable and Callable lambdas with static helper methods runnable, callable, and failingCallable.
    • Updated all usages of these lambdas to use the new static method references.
  • sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/EFOShardSubscribersPool.java
    • Removed unused java.util.function.BiConsumer import.
    • Replaced a BiConsumer lambda with a private method errorHandler and updated its usage to a method reference.
  • sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/kinesis/RateLimitPolicyFactory.java
    • Replaced a static Supplier<Duration> lambda with a static helper method defaultDelay and updated its usage to a method reference.
  • sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/sqs/SqsIO.java
    • Replaced a BiFunction lambda with a private method getLocked within the Dynamic class and updated its usage to a method reference.
  • sdks/java/io/kafka/src/test/java/org/apache/beam/sdk/io/kafka/KafkaIOReadImplementationCompatibilityTest.java
    • Refactored Function lambdas legacyDecoratorFunction and sdfDecoratorFunction into private methods.
    • Updated their usages to use method references.
Activity
  • The UnnecessaryLambda Error Prone check was re-enabled in the build configuration.
  • Multiple lambda expressions across various Java files were identified and refactored into method references or static helper methods to comply with the re-enabled check.
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.

@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

assign set of reviewers

@github-actions
Copy link
Contributor

github-actions bot commented Mar 4, 2026

Assigning reviewers:

R: @kennknowles for label java.
R: @liferoad for label build.
R: @sjvanrossum for label kafka.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@kennknowles
Copy link
Member

The failures actually look potentially real, at least real flakes/race conditions. I'll re-review too.

@kennknowles
Copy link
Member

/gemini review

@stankiewicz
Copy link
Contributor Author

thanks Kenn, I've seen prism failing with same error few times, I will take a look at it tomorrow.

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 refactors the codebase to address the UnnecessaryLambda Error Prone check by replacing lambda expressions with method references or dedicated methods. The changes are consistent and improve code clarity and maintainability. I have one suggestion to further improve the readability of a long if-else chain.

Comment on lines +117 to +146
if (cls.equals(PaneInfo.class)) {
return paneInfoEncoder();
} else if (cls.equals(GlobalWindow.class)) {
return binaryEncoder(GlobalWindow.Coder.INSTANCE, false);
} else if (cls.equals(IntervalWindow.class)) {
return binaryEncoder(IntervalWindowCoder.of(), false);
} else if (cls.equals(Instant.class)) {
return instantEncoder();
} else if (cls.equals(String.class)) {
return Encoders.STRING();
} else if (cls.equals(Boolean.class)) {
return Encoders.BOOLEAN();
} else if (cls.equals(Integer.class)) {
return Encoders.INT();
} else if (cls.equals(Long.class)) {
return Encoders.LONG();
} else if (cls.equals(Float.class)) {
return Encoders.FLOAT();
} else if (cls.equals(Double.class)) {
return Encoders.DOUBLE();
} else if (cls.equals(BigDecimal.class)) {
return Encoders.DECIMAL();
} else if (cls.equals(byte[].class)) {
return Encoders.BINARY();
} else if (cls.equals(Byte.class)) {
return Encoders.BYTE();
} else if (cls.equals(Short.class)) {
return Encoders.SHORT();
}
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For better readability and maintainability, you could refactor this long if-else if chain into a switch statement on the canonical name of the class. This makes the code cleaner and easier to extend.

    String canonicalName = cls.getCanonicalName();
    if (canonicalName == null) {
      return null;
    }
    switch (canonicalName) {
      case "org.apache.beam.sdk.transforms.windowing.PaneInfo":
        return paneInfoEncoder();
      case "org.apache.beam.sdk.transforms.windowing.GlobalWindow":
        return binaryEncoder(GlobalWindow.Coder.INSTANCE, false);
      case "org.apache.beam.sdk.transforms.windowing.IntervalWindow":
        return binaryEncoder(IntervalWindowCoder.of(), false);
      case "org.joda.time.Instant":
        return instantEncoder();
      case "java.lang.String":
        return Encoders.STRING();
      case "java.lang.Boolean":
        return Encoders.BOOLEAN();
      case "java.lang.Integer":
        return Encoders.INT();
      case "java.lang.Long":
        return Encoders.LONG();
      case "java.lang.Float":
        return Encoders.FLOAT();
      case "java.lang.Double":
        return Encoders.DOUBLE();
      case "java.math.BigDecimal":
        return Encoders.DECIMAL();
      case "byte[]":
        return Encoders.BINARY();
      case "java.lang.Byte":
        return Encoders.BYTE();
      case "java.lang.Short":
        return Encoders.SHORT();
      default:
        return null;
    }

@stankiewicz
Copy link
Contributor Author

@kennknowles it's green now

@kennknowles kennknowles merged commit 52c6603 into apache:master Mar 6, 2026
29 of 31 checks passed
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.

2 participants