Skip to content

Commit

Permalink
[java] remove most usages of guava from the support package
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Oct 17, 2023
1 parent 7e0f0c1 commit d3a167e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 32 deletions.
2 changes: 0 additions & 2 deletions java/src/org/openqa/selenium/support/events/BUILD.bazel
@@ -1,4 +1,3 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//java:defs.bzl", "java_library")

java_library(
Expand All @@ -10,6 +9,5 @@ java_library(
deps = [
"//java/src/org/openqa/selenium:core",
"//java/src/org/openqa/selenium/support/decorators",
artifact("com.google.guava:guava"),
],
)
Expand Up @@ -17,7 +17,6 @@

package org.openqa.selenium.support.events;

import com.google.common.primitives.Primitives;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
Expand Down Expand Up @@ -316,7 +315,29 @@ private boolean parametersMatch(Method m, Object[] args) {
return false;
}
for (int i = 0; i < params.length; i++) {
if (args[i] != null && !Primitives.wrap(params[i]).isAssignableFrom(args[i].getClass())) {
Class<?> param = params[i];
if (param.isPrimitive()) {
if (boolean.class.equals(param)) {
param = Boolean.class;
} else if (byte.class.equals(param)) {
param = Byte.class;
} else if (char.class.equals(param)) {
param = Character.class;
} else if (double.class.equals(param)) {
param = Double.class;
} else if (float.class.equals(param)) {
param = Float.class;
} else if (int.class.equals(param)) {
param = Integer.class;
} else if (long.class.equals(param)) {
param = Long.class;
} else if (short.class.equals(param)) {
param = Short.class;
} else if (void.class.equals(param)) {
param = Void.class;
}
}
if (args[i] != null && !param.isAssignableFrom(args[i].getClass())) {
return false;
}
}
Expand Down
2 changes: 0 additions & 2 deletions java/src/org/openqa/selenium/support/locators/BUILD.bazel
@@ -1,4 +1,3 @@
load("@rules_jvm_external//:defs.bzl", "artifact")
load("//common:defs.bzl", "copy_file")
load("//java:defs.bzl", "java_library")

Expand All @@ -16,7 +15,6 @@ java_library(
"//java:auto-service",
"//java/src/org/openqa/selenium/json",
"//java/src/org/openqa/selenium/remote",
artifact("com.google.guava:guava"),
],
)

Expand Down
27 changes: 12 additions & 15 deletions java/src/org/openqa/selenium/support/locators/RelativeLocator.java
Expand Up @@ -20,10 +20,10 @@
import static org.openqa.selenium.json.Json.MAP_TYPE;
import static org.openqa.selenium.support.locators.RelativeLocatorScript.FIND_ELEMENTS;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.SearchContext;
Expand Down Expand Up @@ -82,7 +82,7 @@ public static class RelativeBy extends By implements By.Remotable {
private final List<Map<String, Object>> filters;

private RelativeBy(Object rootLocator) {
this(rootLocator, ImmutableList.of());
this(rootLocator, List.of());
}

private RelativeBy(Object rootLocator, List<Map<String, Object>> filters) {
Expand All @@ -100,7 +100,7 @@ private RelativeBy(Object rootLocator, List<Map<String, Object>> filters) {
}

this.root = Require.nonNull("Root locator", rootLocator);
this.filters = ImmutableList.copyOf(Require.nonNull("Filters", filters));
this.filters = List.copyOf(Require.nonNull("Filters", filters));
}

public RelativeBy above(WebElement element) {
Expand Down Expand Up @@ -178,11 +178,11 @@ private RelativeBy near(Object locator, int atMostDistanceInPixels) {
return new RelativeBy(
root,
amend(
ImmutableMap.of(
Map.of(
"kind",
"near",
"args",
ImmutableList.of(asAtomLocatorParameter(locator), atMostDistanceInPixels))));
List.of(asAtomLocatorParameter(locator), atMostDistanceInPixels))));
}

@Override
Expand All @@ -200,24 +200,21 @@ private RelativeBy simpleDirection(String direction, Object locator) {
Require.nonNull("Locator", locator);

return new RelativeBy(
root,
amend(
ImmutableMap.of(
"kind", direction, "args", ImmutableList.of(asAtomLocatorParameter(locator)))));
root, amend(Map.of("kind", direction, "args", List.of(asAtomLocatorParameter(locator)))));
}

private List<Map<String, Object>> amend(Map<String, Object> toAdd) {
return ImmutableList.<Map<String, Object>>builder().addAll(filters).add(toAdd).build();
return Stream.concat(filters.stream(), Stream.of(toAdd))
.collect(Collectors.toUnmodifiableList());
}

@Override
public Parameters getRemoteParameters() {
return new Parameters("relative", ImmutableMap.of("root", root, "filters", filters));
return new Parameters("relative", Map.of("root", root, "filters", filters));
}

private Map<String, Object> toJson() {
return ImmutableMap.of(
"using", "relative", "value", ImmutableMap.of("root", root, "filters", filters));
return Map.of("using", "relative", "value", Map.of("root", root, "filters", filters));
}
}

Expand All @@ -244,7 +241,7 @@ private static Object asAtomLocatorParameter(Object object) {
"Expected JSON encoded form of locator to have a 'value' field: " + raw);
}

return ImmutableMap.of((String) raw.get("using"), raw.get("value"));
return Map.of((String) raw.get("using"), raw.get("value"));
}

private static void assertLocatorCanBeSerialized(Object locator) {
Expand Down
Expand Up @@ -17,10 +17,9 @@

package org.openqa.selenium.support.locators;

import com.google.common.io.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;

class RelativeLocatorScript {
Expand All @@ -34,9 +33,11 @@ class RelativeLocatorScript {
"/%s/%s",
RelativeLocator.class.getPackage().getName().replace(".", "/"), "findElements.js");

URL url = RelativeLocator.class.getResource(location);
String rawFunction;
try (InputStream stream = RelativeLocator.class.getResourceAsStream(location)) {
rawFunction = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
}

String rawFunction = Resources.toString(url, StandardCharsets.UTF_8);
FIND_ELEMENTS =
String.format("/* findElements */return (%s).apply(null, arguments);", rawFunction);
} catch (IOException e) {
Expand Down
Expand Up @@ -20,8 +20,8 @@
import static org.openqa.selenium.support.locators.RelativeLocatorScript.FIND_ELEMENTS;

import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableMap;
import java.util.List;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.InvalidArgumentException;
import org.openqa.selenium.JavascriptExecutor;
Expand Down Expand Up @@ -64,8 +64,7 @@ public List<WebElement> findElements(SearchContext context) {

@SuppressWarnings("unchecked")
List<WebElement> elements =
(List<WebElement>)
js.executeScript(FIND_ELEMENTS, ImmutableMap.of("relative", converted));
(List<WebElement>) js.executeScript(FIND_ELEMENTS, Map.of("relative", converted));
return elements;
}

Expand Down
1 change: 0 additions & 1 deletion java/src/org/openqa/selenium/support/ui/BUILD.bazel
Expand Up @@ -11,7 +11,6 @@ java_library(
"//java/test/org/openqa/selenium/support:__subpackages__",
],
deps = [
artifact("com.google.guava:guava"),
],
)

Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/support/ui/FluentWait.java
Expand Up @@ -18,7 +18,6 @@
package org.openqa.selenium.support.ui;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -163,7 +162,7 @@ public <K extends Throwable> FluentWait<T> ignoreAll(Collection<Class<? extends
* @see #ignoreAll(Collection)
*/
public FluentWait<T> ignoring(Class<? extends Throwable> exceptionType) {
return this.ignoreAll(ImmutableList.<Class<? extends Throwable>>of(exceptionType));
return this.ignoreAll(List.<Class<? extends Throwable>>of(exceptionType));
}

/**
Expand All @@ -175,7 +174,7 @@ public FluentWait<T> ignoring(Class<? extends Throwable> exceptionType) {
public FluentWait<T> ignoring(
Class<? extends Throwable> firstType, Class<? extends Throwable> secondType) {

return this.ignoreAll(ImmutableList.of(firstType, secondType));
return this.ignoreAll(List.of(firstType, secondType));
}

/**
Expand Down

0 comments on commit d3a167e

Please sign in to comment.