Skip to content

Commit

Permalink
refactor: Use Java 9+ APIs instead of outdated/3rd-party APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
valfirst committed Oct 18, 2023
1 parent 72c33e0 commit 1f16d97
Show file tree
Hide file tree
Showing 80 changed files with 387 additions and 432 deletions.
3 changes: 1 addition & 2 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.internal.CapabilityHelpers;
import io.appium.java_client.internal.ReflectionHelpers;
import io.appium.java_client.internal.SessionHelpers;
Expand Down Expand Up @@ -153,7 +152,7 @@ public AppiumDriver(URL remoteSessionAddress, String platformName, String automa
super();
ReflectionHelpers.setPrivateFieldValue(
RemoteWebDriver.class, this, "capabilities", new ImmutableCapabilities(
ImmutableMap.of(
Map.of(
PLATFORM_NAME, platformName,
APPIUM_PREFIX + AUTOMATION_NAME_OPTION, automationName
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.Response;

Expand All @@ -40,7 +39,7 @@ public Object execute(String commandName, Map<String, ?> parameters) {
Response response;

if (parameters == null || parameters.isEmpty()) {
response = driver.execute(commandName, ImmutableMap.of());
response = driver.execute(commandName, Map.of());
} else {
response = driver.execute(commandName, parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.remote.Response;

import javax.annotation.Nullable;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Map;

Expand Down Expand Up @@ -66,7 +64,7 @@ public static <T> T executeScript(ExecutesMethod executesMethod, String scriptNa
public static <T> T executeScript(
ExecutesMethod executesMethod, String scriptName, @Nullable Map<String, Object> args
) {
return execute(executesMethod, new AbstractMap.SimpleEntry<>(EXECUTE_SCRIPT, ImmutableMap.of(
return execute(executesMethod, Map.entry(EXECUTE_SCRIPT, Map.of(
"script", scriptName,
"args", (args == null || args.isEmpty()) ? Collections.emptyList() : Collections.singletonList(args)
)));
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/io/appium/java_client/ErrorCodesMobile.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.ErrorCodes;

Expand All @@ -32,9 +31,7 @@ public class ErrorCodesMobile extends ErrorCodes {

public static final int NO_SUCH_CONTEXT = 35;

private static Map<Integer, String> statusToState =
ImmutableMap.<Integer, String>builder().put(NO_SUCH_CONTEXT, "No such context found")
.build();
private static Map<Integer, String> statusToState = Map.of(NO_SUCH_CONTEXT, "No such context found");

/**
* Returns the exception type that corresponds to the given {@code statusCode}. All unrecognized
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/ExecuteCDPCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.MobileCommand.EXECUTE_GOOGLE_CDP_COMMAND;
import static java.util.Objects.requireNonNull;

public interface ExecuteCDPCommand extends ExecutesMethod {

Expand All @@ -40,7 +40,7 @@ public interface ExecuteCDPCommand extends ExecutesMethod {
*/
default Map<String, Object> executeCdpCommand(String command, @Nullable Map<String, Object> params) {
Map<String, Object> data = new HashMap<>();
data.put("cmd", checkNotNull(command));
data.put("cmd", requireNonNull(command));
data.put("params", params == null ? Collections.emptyMap() : params);
Response response = execute(EXECUTE_GOOGLE_CDP_COMMAND, data);
//noinspection unchecked
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/ExecutesDriverScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.MobileCommand.EXECUTE_DRIVER_SCRIPT;
import static java.util.Objects.requireNonNull;

public interface ExecutesDriverScript extends ExecutesMethod {

Expand All @@ -46,7 +46,7 @@ public interface ExecutesDriverScript extends ExecutesMethod {
*/
default ScriptValue executeDriverScript(String script, @Nullable ScriptOptions options) {
Map<String, Object> data = new HashMap<>();
data.put("script", checkNotNull(script));
data.put("script", requireNonNull(script));
if (options != null) {
data.putAll(options.build());
}
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/io/appium/java_client/HasAppStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.UnsupportedCommandException;

import java.util.AbstractMap;
import java.util.Map;

import static io.appium.java_client.MobileCommand.GET_STRINGS;
import static io.appium.java_client.MobileCommand.prepareArguments;

public interface HasAppStrings extends ExecutesMethod, CanRememberExtensionPresence {
/**
Expand Down Expand Up @@ -52,14 +49,14 @@ default Map<String, String> getAppStringMap() {
default Map<String, String> getAppStringMap(String language) {
final String extName = "mobile: getAppStrings";
try {
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, Map.of(
"language", language
));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return CommandExecutionHelper.execute(
markExtensionAbsence(extName),
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language))
Map.entry(GET_STRINGS, Map.of("language", language))
);
}
}
Expand All @@ -75,18 +72,17 @@ default Map<String, String> getAppStringMap(String language) {
*/
default Map<String, String> getAppStringMap(String language, String stringFile) {
final String extName = "mobile: getAppStrings";
Map<String, Object> args = Map.of(
"language", language,
"stringFile", stringFile
);
try {
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
"language", language,
"stringFile", stringFile
));
return CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, args);
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
String[] parameters = new String[]{"language", "stringFile"};
Object[] values = new Object[]{language, stringFile};
return CommandExecutionHelper.execute(
markExtensionAbsence(extName),
new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))
Map.entry(GET_STRINGS, args)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/HasBrowserCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.CapabilityType;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Strings.isNullOrEmpty;
import static java.util.Objects.requireNonNull;

public interface HasBrowserCheck extends ExecutesMethod, HasCapabilities {
/**
Expand All @@ -20,7 +20,7 @@ default boolean isBrowser() {
CapabilityType.BROWSER_NAME, String.class);
if (!isNullOrEmpty(browserName)) {
try {
return checkNotNull(
return requireNonNull(
CommandExecutionHelper.executeScript(this, "return !!window.navigator;")
);
} catch (WebDriverException ign) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/appium/java_client/HasDeviceTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import java.util.Map;

public interface HasDeviceTime extends ExecutesMethod {

Expand All @@ -32,7 +32,7 @@ public interface HasDeviceTime extends ExecutesMethod {
*/
default String getDeviceTime(String format) {
return CommandExecutionHelper.executeScript(
this, "mobile: getDeviceTime", ImmutableMap.of("format", format)
this, "mobile: getDeviceTime", Map.of("format", format)
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/appium/java_client/HasOnScreenKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import org.openqa.selenium.UnsupportedCommandException;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.MobileCommand.isKeyboardShownCommand;
import static java.util.Objects.requireNonNull;

public interface HasOnScreenKeyboard extends ExecutesMethod, CanRememberExtensionPresence {

Expand All @@ -16,10 +16,10 @@ public interface HasOnScreenKeyboard extends ExecutesMethod, CanRememberExtensio
default boolean isKeyboardShown() {
final String extName = "mobile: isKeyboardShown";
try {
return checkNotNull(CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName));
return requireNonNull(CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
return checkNotNull(
return requireNonNull(
CommandExecutionHelper.execute(markExtensionAbsence(extName), isKeyboardShownCommand())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.UnsupportedCommandException;

import java.util.List;
import java.util.Map;

import static io.appium.java_client.MobileCommand.hideKeyboardCommand;

public interface HidesKeyboardWithKeyName extends HidesKeyboard {
Expand All @@ -37,8 +38,8 @@ public interface HidesKeyboardWithKeyName extends HidesKeyboard {
default void hideKeyboard(String keyName) {
final String extName = "mobile: hideKeyboard";
try {
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
"keys", ImmutableList.of(keyName)
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, Map.of(
"keys", List.of(keyName)
));
} catch (UnsupportedCommandException e) {
// TODO: Remove the fallback
Expand Down
Loading

0 comments on commit 1f16d97

Please sign in to comment.