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] Add nullness for enums #15105

Merged
merged 3 commits into from
Jan 20, 2025
Merged

[java] Add nullness for enums #15105

merged 3 commits into from
Jan 20, 2025

Conversation

mk868
Copy link
Contributor

@mk868 mk868 commented Jan 17, 2025

User description

Description

In this PR I'm adding nullness annotations for classes:

  • org.openqa.selenium.PageLoadStrategy
  • org.openqa.selenium.UnexpectedAlertBehaviour
  • org.openqa.selenium.WindowType

NullAway analysis: #14421

Motivation and Context

The JSpecify nullness annotations will give developers better exposure to potential problems with their code to avoid NullPointerExceptions.
Related issue: #14291

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

  • Added JSpecify nullness annotations to improve null safety.

  • Annotated fromString methods in enums with @Nullable.

  • Marked enums with @NullMarked for nullness enforcement.

  • Enhanced IDE/static analyzer interoperability and Kotlin compatibility.


Changes walkthrough 📝

Relevant files
Enhancement
PageLoadStrategy.java
Add nullness annotations to PageLoadStrategy enum               

java/src/org/openqa/selenium/PageLoadStrategy.java

  • Added @NullMarked annotation to the enum.
  • Annotated fromString method with @Nullable for parameters and return
    type.
  • Improved null safety and static analysis compatibility.
  • +5/-1     
    UnexpectedAlertBehaviour.java
    Add nullness annotations to UnexpectedAlertBehaviour enum

    java/src/org/openqa/selenium/UnexpectedAlertBehaviour.java

  • Added @NullMarked annotation to the enum.
  • Annotated fromString method with @Nullable for parameters and return
    type.
  • Enhanced null safety and IDE support.
  • +5/-1     
    WindowType.java
    Add nullness annotations to WindowType enum                           

    java/src/org/openqa/selenium/WindowType.java

  • Added @NullMarked annotation to the enum.
  • Annotated fromString method with @Nullable for parameters and return
    type.
  • Improved null safety and Kotlin interoperability.
  • +5/-1     

    Need help?
  • Type /help how to ... in the comments thread for any question about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis ✅

    14291 - Fully compliant

    Compliant requirements:

    • Add JSpecify Nullness annotations to Selenium framework code
    • Annotations should specify which parameters and return values can be null
    • Improve IDE and static code analyzer support
    • Enhance Kotlin interoperability
    ⏱️ Estimated effort to review: 1 🔵⚪⚪⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Return Value Documentation

    The fromString method can return null when the input text doesn't match any enum value, but this behavior is not documented in the method's JavaDoc

    public static @Nullable PageLoadStrategy fromString(@Nullable String text) {

    @mk868
    Copy link
    Contributor Author

    mk868 commented Jan 17, 2025

    Fixed NullAway errors:

    java/src/org/openqa/selenium/WindowType.java:45: error: [NullAway] returning @Nullable expression from method with @NonNull return type
        return null;
        ^
        (see http://t.uber.com/nullaway )
    java/src/org/openqa/selenium/PageLoadStrategy.java:44: error: [NullAway] returning @Nullable expression from method with @NonNull return type
        return null;
        ^
        (see http://t.uber.com/nullaway )
    java/src/org/openqa/selenium/UnexpectedAlertBehaviour.java:46: error: [NullAway] returning @Nullable expression from method with @NonNull return type
        return null;
        ^
        (see http://t.uber.com/nullaway )
    

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Return a default value instead of null for invalid inputs to prevent runtime errors and maintain type safety

    The fromString method should return a default value (like NORMAL) instead of null
    when the input is invalid, as enum values are typically used in non-null contexts.
    This prevents potential NullPointerExceptions and maintains type safety.

    java/src/org/openqa/selenium/PageLoadStrategy.java [40-44]

    -public static @Nullable PageLoadStrategy fromString(@Nullable String text) {
    +public static PageLoadStrategy fromString(@Nullable String text) {
       if (text != null) {
         for (PageLoadStrategy b : PageLoadStrategy.values()) {
           if (text.equalsIgnoreCase(b.text)) {
             return b;
    +      }
    +    }
    +  }
    +  return NORMAL;  // Default value instead of null
    • Apply this suggestion
    Suggestion importance[1-10]: 2

    Why: While the suggestion aims to prevent null pointer exceptions, it contradicts the PR's explicit goal of improving null safety through @nullable annotations. The PR deliberately makes the return type nullable to handle invalid cases properly.

    2

    @VietND96 VietND96 merged commit 3c16d81 into SeleniumHQ:trunk Jan 20, 2025
    34 checks passed
    @mk868 mk868 deleted the jspecify-enums branch January 20, 2025 06:52
    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