Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java]Replace lambdas with method references #14857

Merged

Conversation

iampopovich
Copy link
Contributor

@iampopovich iampopovich commented Dec 5, 2024

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

This pull request includes several refactorings to improve code readability and consistency across multiple files, primarily by replacing lambda expressions with method references and simplifying conditional checks.

Code readability improvements:

Test code simplification:

Motivation and Context

While often it could be a matter of taste, method references are more clear and readable compared to lambdas.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

enhancement, tests


Description

  • Replaced lambda expressions with method references across multiple files to improve code readability.
  • Simplified conditional checks using isEmpty() instead of !isPresent().
  • Updated test assertions to use method references for consistency and clarity.

Changes walkthrough 📝

Relevant files
Enhancement
ChromiumDriver.java
Use method reference in ChromiumDriver constructor             

java/src/org/openqa/selenium/chromium/ChromiumDriver.java

  • Replaced lambda expression with method reference in ChromiumDriver
    constructor.
  • +1/-1     
    SeleniumCdpConnection.java
    Refactor SeleniumCdpConnection with method references and simplified
    conditions

    java/src/org/openqa/selenium/devtools/SeleniumCdpConnection.java

  • Replaced lambda expression with method reference in create method.
  • Simplified conditional check using isEmpty().
  • +2/-2     
    FirefoxDriver.java
    Use method reference in FirefoxDriver constructor               

    java/src/org/openqa/selenium/firefox/FirefoxDriver.java

  • Replaced lambda expression with method reference in FirefoxDriver
    constructor.
  • +1/-1     
    RemoteWebDriver.java
    Refactor addCredential method with method reference           

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java

  • Replaced lambda expression with method reference in addCredential
    method.
  • +1/-1     
    Tests
    NoSuchShadowRootTest.java
    Use method reference in NoSuchShadowRootTest assertion     

    java/test/org/openqa/selenium/NoSuchShadowRootTest.java

  • Replaced lambda expression with method reference in test assertion.
  • +1/-1     
    RelayOptionsTest.java
    Use method reference in RelayOptionsTest assertion             

    java/test/org/openqa/selenium/grid/node/relay/RelayOptionsTest.java

  • Replaced lambda expression with method reference in test assertion.
  • +1/-4     
    BaseServerOptionsTest.java
    Use method reference in BaseServerOptionsTest assertion   

    java/test/org/openqa/selenium/grid/server/BaseServerOptionsTest.java

  • Replaced lambda expression with method reference in test assertion.
  • +1/-6     

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 5, 2024

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Error Handling
    Verify that the error handling and resource cleanup in the catch blocks is sufficient when CDP endpoint connection fails

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 5, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add null checks to prevent potential NullPointerException in stream operations

    The stream concatenation and collection could fail with null values from the
    credential map. Add null checks to prevent NullPointerException.

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java [1247-1250]

     Stream.concat(
    -        credential.toMap().entrySet().stream(),
    +        Objects.requireNonNull(credential.toMap()).entrySet().stream(),
             Stream.of(Map.entry("authenticatorId", id)))
    +    .filter(e -> e.getKey() != null && e.getValue() != null)
         .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)));
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a potential runtime error by adding null checks for credential map values, which could prevent NullPointerException in production code.

    8
    General
    Add logging for suppressed exceptions to improve error diagnostics and debugging

    Consider handling the suppressed exception by logging it rather than just adding it
    to the suppressed exceptions list, as it contains important connection cleanup error
    details.

    java/src/org/openqa/selenium/chromium/ChromiumDriver.java [152-154]

     try {
       client.ifPresent(HttpClient::close);
     } catch (Exception ex) {
    +  LOG.log(Level.WARNING, "Failed to close HTTP client", ex);
       e.addSuppressed(ex);
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Adding logging for suppressed exceptions would improve debugging capabilities by making cleanup errors more visible, while still maintaining the original exception handling behavior.

    6

    💡 Need additional feedback ? start a PR chat

    Copy link
    Contributor

    @pujagani pujagani left a comment

    Choose a reason for hiding this comment

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

    Thank you! @iampopovich

    @pujagani pujagani merged commit 7a93ff7 into SeleniumHQ:trunk Dec 6, 2024
    34 checks passed
    sandeepsuryaprasad pushed a commit to sandeepsuryaprasad/selenium that referenced this pull request Dec 7, 2024
    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