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] Replacing some deprecated methods #15443

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

Conversation

zodac
Copy link

@zodac zodac commented Mar 17, 2025

User description

Motivation and Context

There were several places where some deprecated method calls were being used (some for internal project code, others for libraries). Where possible and obvious, I've replaced this code with the non-deprecated methods.

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)
  • Refactoring (no changed to public API)

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. (tested with bazel test //java/... --test_size_filters=small)

PR Type

Enhancement, Bug fix, Tests


Description

  • Replaced deprecated newInstance with getDeclaredConstructor().newInstance for reflection-based object creation.

  • Updated SlowLoadableComponent constructors to use Duration.ofSeconds for timeout values.

  • Migrated from MockitoAnnotations.initMocks to MockitoAnnotations.openMocks in test setup methods.

  • Improved type safety in assertions using InstanceOfAssertFactories in tests.


Changes walkthrough 📝

Relevant files
Enhancement
7 files
DescribedOption.java
Replace deprecated newInstance with
getDeclaredConstructor().newInstance
+3/-2     
Types.java
Update GraphQL coercing methods to include additional parameters
+10/-6   
TerseFormatter.java
Make `levelNumberToCommonsLevelName` method static             
+1/-1     
LocalNodeFactory.java
Replace deprecated newInstance with
getDeclaredConstructor().newInstance
+3/-2     
WebDriverDecorator.java
Replace deprecated newInstance with
getDeclaredConstructor().newInstance
+1/-1     
AjaxElementLocator.java
Use `Duration.ofSeconds` for timeout in `SlowLoadableComponent`
+3/-2     
SlowLoadableComponentTest.java
Use `Duration.ofSeconds` for timeout in `SlowLoadableComponent`
+4/-4     
Tests
6 files
RemoteLogsTest.java
Replace `initMocks` with `openMocks` in test setup             
+1/-1     
TracedCommandExecutorTest.java
Replace `initMocks` with `openMocks` in test setup             
+1/-1     
ExpectedConditionsTest.java
Replace `initMocks` with `openMocks` in test setup             
+1/-1     
FluentWaitTest.java
Replace `initMocks` with `openMocks` in test setup             
+1/-1     
WebDriverWaitTest.java
Replace `initMocks` with `openMocks` in test setup             
+1/-1     
VirtualAuthenticatorTest.java
Improve type safety in assertions using `InstanceOfAssertFactories`
+2/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • In several locations I've removed some minor deprecated code and used the the recommended replacements.
    @CLAassistant
    Copy link

    CLAassistant commented Mar 17, 2025

    CLA assistant check
    All committers have signed the CLA.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Reflection Error Handling

    The updated code now catches additional exceptions (InvocationTargetException and NoSuchMethodException), but the error message doesn't reflect these new exception types, still referring only to field access and instantiation issues.

      Object fieldInstance = field.get(clazz.getDeclaredConstructor().newInstance());
      fieldValue = fieldInstance == null ? "" : fieldInstance.toString();
    } catch (IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ignore) {
      // We'll swallow this exception since we are just trying to get field's default value
    Method Signature Changes

    The method signatures have been updated to match the new GraphQL API, but there's no verification that the new parameters (graphQLContext, locale) are properly used in the method implementations.

                public String serialize(Object o, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
                  if (o instanceof String) {
                    return (String) o;
                  }
    
                  if (o instanceof URL || o instanceof URI) {
                    return String.valueOf(o);
                  }
    
                  throw new CoercingSerializeException("Unable to coerce " + o);
                }
    
                @Override
                public URI parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
                  if (input == null) {
                    return null;
                  }
    
                  if (input instanceof URI) {
                    return (URI) input;
                  }
    
                  try {
                    if (input instanceof String) {
                      return new URI((String) input);
                    }
    
                    if (input instanceof URL) {
                      return ((URL) input).toURI();
                    }
                  } catch (URISyntaxException e) {
                    throw new CoercingParseValueException("Unable to create URI: " + input, e);
                  }
    
                  throw new CoercingParseValueException("Unable to create URI: " + input);
                }
    
                @Override
                public URI parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
                  if (!(input instanceof StringValue)) {
                    throw new CoercingParseLiteralException("Cannot convert to URL: " + input);
                  }
    
                  try {
                    return new URI(((StringValue) input).getValue());
                  } catch (URISyntaxException e) {
                    throw new CoercingParseLiteralException("Unable to parse: " + input);
                  }
                }
              })
          .build();
    }
    
    private static GraphQLScalarType urlType() {
      return GraphQLScalarType.newScalar()
          .name("Url")
          .coercing(
              new Coercing<URL, String>() {
                @Override
                public String serialize(Object o, GraphQLContext graphQLContext, Locale locale) throws CoercingSerializeException {
                  if (o instanceof String) {
                    return (String) o;
                  }
    
                  if (o instanceof URL || o instanceof URI) {
                    return String.valueOf(o);
                  }
    
                  throw new CoercingSerializeException("Unable to coerce " + o);
                }
    
                @Override
                public URL parseValue(Object input, GraphQLContext graphQLContext, Locale locale) throws CoercingParseValueException {
                  if (input == null) {
                    return null;
                  }
    
                  if (input instanceof URL) {
                    return (URL) input;
                  }
    
                  try {
                    if (input instanceof String) {
                      return new URL((String) input);
                    }
    
                    if (input instanceof URI) {
                      return ((URI) input).toURL();
                    }
                  } catch (MalformedURLException e) {
                    throw new CoercingParseValueException("Unable to create URL: " + input, e);
                  }
    
                  throw new CoercingParseValueException("Unable to create URL: " + input);
                }
    
                @Override
                public URL parseLiteral(Value<?> input, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) throws CoercingParseLiteralException {
                  if (!(input instanceof StringValue)) {
                    throw new CoercingParseLiteralException("Cannot convert to URL: " + input);
                  }

    Copy link
    Contributor

    qodo-merge-pro bot commented Mar 17, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    General
    Reduce excessive timeout value

    The value 1000L seconds is extremely high for a timeout in a test. This could
    lead to very long test execution times if there's an issue. Consider using a
    more reasonable timeout value.

    java/src/org/openqa/selenium/support/ui/SlowLoadableComponentTest.java [151]

     public HasError() {
    -  super(new TickingClock(), Duration.ofSeconds(1000L));
    +  super(new TickingClock(), Duration.ofSeconds(10L));
     }

    [To ensure code accuracy, apply this suggestion manually]

    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies an excessively high timeout value (1000 seconds) in a test class, which could lead to very long test execution times if there's an issue. Reducing it to 10 seconds is a reasonable improvement that maintains test functionality while preventing unnecessarily long test runs.

    Medium
    • Update

    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