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 logging #15108

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

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.logging.LogEntries
  • org.openqa.selenium.logging.LogEntry
  • org.openqa.selenium.logging.LogLevelMapping
  • org.openqa.selenium.logging.SessionLogs

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, Tests


Description

  • Added JSpecify @NullMarked annotations to logging-related classes.

  • Enhanced null handling in LogLevelMapping with Optional.

  • Improved null safety in SessionLogs using Require.nonNull.

  • Added test case for null log level handling in LoggingTest.


Changes walkthrough 📝

Relevant files
Enhancement
LogEntries.java
Add `@NullMarked` annotation to `LogEntries`.                       

java/src/org/openqa/selenium/logging/LogEntries.java

  • Added @NullMarked annotation to the class.
+2/-0     
LogEntry.java
Add `@NullMarked` annotation to `LogEntry`.                           

java/src/org/openqa/selenium/logging/LogEntry.java

  • Added @NullMarked annotation to the class.
+2/-0     
LogLevelMapping.java
Enhance null handling in `LogLevelMapping`.                           

java/src/org/openqa/selenium/logging/LogLevelMapping.java

  • Added @NullMarked annotation to the class.
  • Introduced @Nullable for toLevel parameter.
  • Used Optional for null-safe log level mapping.
  • Refactored default log level handling.
  • +11/-4   
    SessionLogs.java
    Improve null safety in `SessionLogs`.                                       

    java/src/org/openqa/selenium/logging/SessionLogs.java

  • Added @NullMarked annotation to the class.
  • Used Require.nonNull for null safety in fromJSON.
  • +4/-1     
    Tests
    LoggingTest.java
    Add test for null log level in `LoggingTest`.                       

    java/test/org/openqa/selenium/logging/LoggingTest.java

    • Added test case for null log level handling.
    +1/-0     

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

    mk868 commented Jan 17, 2025

    Fixed NullAway errors:

    java/src/org/openqa/selenium/logging/LogLevelMapping.java:82: error: [NullAway] returning @Nullable expression from method with @NonNull return type
        return levelMap.get(Level.parse(logLevelName).intValue());
        ^
        (see http://t.uber.com/nullaway )
    java/src/org/openqa/selenium/logging/SessionLogs.java:69: error: [NullAway] dereferenced expression ((Number) rawEntry.get("timestamp")) is @Nullable
                    ((Number) rawEntry.get("timestamp")).longValue(),
                                                        ^
        (see http://t.uber.com/nullaway )
    

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    14291 - Partially compliant

    Compliant requirements:

    • Add JSpecify Nullness annotations to Selenium framework code
    • Annotations should be transparent to IDEs and static code analyzers

    Non-compliant requirements:

    • Annotations should specify which parameters and return values can be null (only class-level @NullMarked added, but specific method parameters not marked)

    Requires further human verification:

    • Improve interoperability with Kotlin (needs testing with Kotlin codebase)
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Incomplete Nullness

    While @NullMarked is added at class level, method parameters and return values lack specific @nullable or @NotNull annotations as shown in the ticket example

    public static Level toLevel(@Nullable String logLevelName) {
      if (logLevelName == null || logLevelName.isEmpty()) {
        return DEFAULT_LEVEL;
      }
    
      if (logLevelName.equals(DEBUG)) {
        return Level.FINE;
      }
      return Optional.ofNullable(levelMap.get(Level.parse(logLevelName).intValue()))
          .orElse(DEFAULT_LEVEL);
    }

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add error handling for invalid log level parsing to prevent runtime exceptions

    The toLevel() method should validate the input before parsing to avoid potential
    NumberFormatException when calling Level.parse() on invalid input. Add input
    validation before parsing.

    java/src/org/openqa/selenium/logging/LogLevelMapping.java [88-89]

    -return Optional.ofNullable(levelMap.get(Level.parse(logLevelName).intValue()))
    -    .orElse(DEFAULT_LEVEL);
    +try {
    +  return Optional.ofNullable(levelMap.get(Level.parse(logLevelName).intValue()))
    +      .orElse(DEFAULT_LEVEL);
    +} catch (IllegalArgumentException e) {
    +  return DEFAULT_LEVEL;
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion addresses a critical error handling issue where invalid log level strings could cause runtime exceptions. Adding try-catch block is essential for robust error handling and maintains the default level fallback behavior.

    8
    Add null validation for required message field to prevent runtime exceptions

    Add null check for the 'message' field in fromJSON() to prevent potential
    NullPointerException when calling String.valueOf() on null message.

    java/src/org/openqa/selenium/logging/SessionLogs.java [73]

    -String.valueOf(rawEntry.get("message"))
    +String.valueOf(Require.nonNull("message", rawEntry.get("message")))
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion enhances null safety by using Require.nonNull for message validation, consistent with the PR's null safety improvements and preventing potential NullPointerException in production code.

    8

    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