Skip to content

[java] Refactored the UsernameAndPassword class to a record #15360

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

Closed
wants to merge 10 commits into from

Conversation

manuelsblanco
Copy link
Contributor

@manuelsblanco manuelsblanco commented Mar 2, 2025

User description

Refactored the UsernameAndPassword class to a record, simplifying the implementation and ensuring immutability. Removed the redundant of method and Supplier<Credentials> usage, as they are no longer needed with the record structure.

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.

Motivation and Context

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


Description

  • Refactored UsernameAndPassword class into a Java record.

  • Simplified implementation by removing redundant methods.

  • Ensured immutability and streamlined the code structure.


Changes walkthrough 📝

Relevant files
Enhancement
UsernameAndPassword.java
Refactored `UsernameAndPassword` class to a `record`         

java/src/org/openqa/selenium/UsernameAndPassword.java

  • Converted UsernameAndPassword class to a Java record.
  • Removed the of method and Supplier usage.
  • Eliminated redundant getter methods for username and password.
  • Simplified constructor with Require.nonNull checks.
  • +1/-22   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Refactored the `UsernameAndPassword` class to a `record`, simplifying the implementation and ensuring immutability. Removed the redundant `of` method and `Supplier<Credentials>` usage, as they are no longer needed with the record structure.
    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 2, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ No major issues detected

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 2, 2025

    PR Code Suggestions ✨

    No code suggestions found for the PR.

    @VietND96 VietND96 requested a review from pujagani March 4, 2025 02:55
    @VietND96 VietND96 changed the title Java record [java] Refactored the UsernameAndPassword class to a record Mar 4, 2025
    @VietND96
    Copy link
    Member

    VietND96 commented Mar 4, 2025

    @manuelsblanco Can you resolve the conflict?


    public UsernameAndPassword(String username, String password) {
    this.username = Require.nonNull("User name", username);
    this.password = Require.nonNull("Password", password);
    }

    public static Supplier<Credentials> of(String username, String password) {
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    Please, note that this method is used by Selenium users, for example:

    import org.openqa.selenium.UsernameAndPassword;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class Main {
    
      public static void main(String[] args) throws InterruptedException {
        var driver = new ChromeDriver();
        driver.register(UsernameAndPassword.of("foo", "bar"));
    
        driver.get("http://httpbin.org/basic-auth/foo/bar");
    
        Thread.sleep(10_000);
        driver.quit();
      }
    }

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    @mk868 New commit with the new code added. Thanks

    @manuelsblanco
    Copy link
    Contributor Author

    @manuelsblanco Can you resolve the conflict?

    Sure, I will try this wkd.

    - Added the static factory method `of(String username, String password)` to maintain compatibility with existing Selenium code that relies on it.
    - Optimized the constructor by using a compact form, ensuring `username` and `password` are non-null without explicitly redefining fields.
    - Removed redundant `username()` and `password()` methods, as they are automatically generated by the `record` structure.
    - Improved code clarity, maintainability, and adherence to Java's record best practices.
    @manuelsblanco
    Copy link
    Contributor Author

    @manuelsblanco Can you resolve the conflict?

    This is ready to check @VietND96

    @VietND96
    Copy link
    Member

    Not sure why the PR still showing branch has conflicts that must be resolved

    @manuelsblanco
    Copy link
    Contributor Author

    Not sure why the PR still showing branch has conflicts that must be resolved

    Fixed @VietND96

    }
    }
    }
    Copy link
    Member

    Choose a reason for hiding this comment

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

    Add newline here

    Copy link
    Contributor Author

    Choose a reason for hiding this comment

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

    @Delta456 newline added

    @manuelsblanco
    Copy link
    Contributor Author

    does this look ok? @pujagani If we want to update the code, we have several classes that could be converted into record classes.

    }

    public String password() {
    return password;
    public static UsernameAndPassword of(String username, String password) {
    Copy link
    Contributor

    Choose a reason for hiding this comment

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

    This is a breaking change. It is changing the function return type.

    @titusfortner
    Copy link
    Member

    Even worse, record is not available until Java 17 and we must continue to support Java 11.
    Thank you for your contribution, but we cannot do this one at this time.

    @manuelsblanco
    Copy link
    Contributor Author

    Even worse, record is not available until Java 17 and we must continue to support Java 11. Thank you for your contribution, but we cannot do this one at this time.

    thanks I will keep it in mind to write code for JAVA 11

    @manuelsblanco manuelsblanco deleted the recordClass branch March 24, 2025 17:19
    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.

    6 participants