Skip to content

Commit 0c0fbdb

Browse files
authored
Merge branch 'trunk' into feat-14291/jspecify-nullable-annotation_ff_driver_service
2 parents 4d742a5 + 5d1ee30 commit 0c0fbdb

File tree

10 files changed

+59
-55
lines changed

10 files changed

+59
-55
lines changed

java/src/org/openqa/selenium/By.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public String toString() {
296296

297297
public static class ByClassName extends PreW3CLocator {
298298

299+
private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*");
299300
private final String className;
300301

301302
public ByClassName(String className) {
@@ -305,7 +306,7 @@ public ByClassName(String className) {
305306
.nonNull("Cannot find elements when the class name expression is null."),
306307
".%s");
307308

308-
if (className.matches(".*\\s.*")) {
309+
if (AT_LEAST_ONE_WHITESPACE.matcher(className).matches()) {
309310
throw new InvalidSelectorException("Compound class names not permitted");
310311
}
311312

java/src/org/openqa/selenium/PersistentCapabilities.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717

1818
package org.openqa.selenium;
1919

20-
import java.util.Collections;
2120
import java.util.Map;
2221
import java.util.Set;
2322
import java.util.function.Function;
24-
import java.util.stream.Collector;
2523
import java.util.stream.Collectors;
2624
import java.util.stream.Stream;
2725
import org.openqa.selenium.internal.Require;
@@ -58,7 +56,7 @@ public PersistentCapabilities setCapability(String name, Object value) {
5856
@Override
5957
public Map<String, Object> asMap() {
6058
return getCapabilityNames().stream()
61-
.collect(toUnmodifiableMap(Function.identity(), this::getCapability));
59+
.collect(Collectors.toUnmodifiableMap(Function.identity(), this::getCapability));
6260
}
6361

6462
@Override
@@ -81,19 +79,7 @@ public Capabilities merge(Capabilities other) {
8179
public Set<String> getCapabilityNames() {
8280
return Stream.concat(
8381
caps.getCapabilityNames().stream(), overrides.getCapabilityNames().stream())
84-
.collect(toUnmodifiableSet());
85-
}
86-
87-
// Needed, since we're dependent on Java 8 as a minimum version
88-
private <T, K, U> Collector<T, ?, Map<K, U>> toUnmodifiableMap(
89-
Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper) {
90-
return Collectors.collectingAndThen(
91-
Collectors.toMap(keyMapper, valueMapper), Collections::unmodifiableMap);
92-
}
93-
94-
// Needed, since we're dependent on Java 8 as a minimum version
95-
private <T> Collector<T, ?, Set<T>> toUnmodifiableSet() {
96-
return Collectors.collectingAndThen(Collectors.toSet(), Collections::unmodifiableSet);
82+
.collect(Collectors.toUnmodifiableSet());
9783
}
9884

9985
@Override

java/src/org/openqa/selenium/Platform.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ public String toString() {
369369
}
370370
};
371371

372+
private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+).*");
372373
private static @Nullable Platform current;
373374
private final String[] partOfOsName;
374375
private int minorVersion = 0;
@@ -389,21 +390,20 @@ public static Platform getCurrent() {
389390

390391
String version = System.getProperty("os.version", "0.0.0");
391392
int major = 0;
392-
int min = 0;
393+
int minor = 0;
393394

394-
Pattern pattern = Pattern.compile("^(\\d+)\\.(\\d+).*");
395-
Matcher matcher = pattern.matcher(version);
395+
final Matcher matcher = VERSION_PATTERN.matcher(version);
396396
if (matcher.matches()) {
397397
try {
398398
major = Integer.parseInt(matcher.group(1));
399-
min = Integer.parseInt(matcher.group(2));
399+
minor = Integer.parseInt(matcher.group(2));
400400
} catch (NumberFormatException e) {
401401
// These things happen
402402
}
403403
}
404404

405405
current.majorVersion = major;
406-
current.minorVersion = min;
406+
current.minorVersion = minor;
407407
}
408408
return current;
409409
}

java/src/org/openqa/selenium/Point.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import org.jspecify.annotations.NullMarked;
2222
import org.jspecify.annotations.Nullable;
2323

24-
/** A copy of java.awt.Point, to remove dependency on awt. */
24+
/** Represents a point in a two-dimensional space with x and y coordinates. */
2525
@NullMarked
2626
public class Point {
27-
public int x;
28-
public int y;
27+
public final int x;
28+
public final int y;
2929

3030
public Point(int x, int y) {
3131
this.x = x;

java/src/org/openqa/selenium/chrome/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ java_export(
2020
"//java/src/org/openqa/selenium/json",
2121
"//java/src/org/openqa/selenium/manager",
2222
"//java/src/org/openqa/selenium/remote",
23+
"@maven//:org_jspecify_jspecify",
2324
],
2425
)

java/src/org/openqa/selenium/chrome/ChromeDriverService.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232
import java.util.Locale;
3333
import java.util.Map;
34+
import org.jspecify.annotations.Nullable;
3435
import org.openqa.selenium.Capabilities;
3536
import org.openqa.selenium.WebDriverException;
3637
import org.openqa.selenium.chromium.ChromiumDriverLogLevel;
@@ -102,11 +103,11 @@ public class ChromeDriverService extends DriverService {
102103
* @throws IOException If an I/O error occurs.
103104
*/
104105
public ChromeDriverService(
105-
File executable,
106+
@Nullable File executable,
106107
int port,
107-
Duration timeout,
108-
List<String> args,
109-
Map<String, String> environment)
108+
@Nullable Duration timeout,
109+
@Nullable List<String> args,
110+
@Nullable Map<String, String> environment)
110111
throws IOException {
111112
super(
112113
executable,
@@ -151,13 +152,13 @@ public static ChromeDriverService createDefaultService() {
151152
public static class Builder
152153
extends DriverService.Builder<ChromeDriverService, ChromeDriverService.Builder> {
153154

154-
private Boolean disableBuildCheck;
155-
private Boolean readableTimestamp;
156-
private Boolean appendLog;
157-
private Boolean verbose;
158-
private Boolean silent;
159-
private String allowedListIps;
160-
private ChromiumDriverLogLevel logLevel;
155+
private @Nullable Boolean disableBuildCheck;
156+
private @Nullable Boolean readableTimestamp;
157+
private @Nullable Boolean appendLog;
158+
private @Nullable Boolean verbose;
159+
private @Nullable Boolean silent;
160+
private @Nullable String allowedListIps;
161+
private @Nullable ChromiumDriverLogLevel logLevel;
161162

162163
@Override
163164
public int score(Capabilities capabilities) {
@@ -202,7 +203,7 @@ public Builder withBuildCheckDisabled(boolean noBuildCheck) {
202203
* @param logLevel {@link ChromiumDriverLogLevel} for desired log level output.
203204
* @return A self reference.
204205
*/
205-
public Builder withLogLevel(ChromiumDriverLogLevel logLevel) {
206+
public Builder withLogLevel(@Nullable ChromiumDriverLogLevel logLevel) {
206207
this.logLevel = logLevel;
207208
this.silent = false;
208209
this.verbose = false;
@@ -244,7 +245,7 @@ public Builder withVerbose(boolean verbose) {
244245
* @param allowedListIps Comma-separated list of remote IPv4 addresses.
245246
* @return A self reference.
246247
*/
247-
public Builder withAllowedListIps(String allowedListIps) {
248+
public Builder withAllowedListIps(@Nullable String allowedListIps) {
248249
this.allowedListIps = allowedListIps;
249250
return this;
250251
}
@@ -255,7 +256,7 @@ public Builder withAllowedListIps(String allowedListIps) {
255256
* @param readableTimestamp Whether the timestamp of the log is readable.
256257
* @return A self reference.
257258
*/
258-
public Builder withReadableTimestamp(Boolean readableTimestamp) {
259+
public Builder withReadableTimestamp(@Nullable Boolean readableTimestamp) {
259260
this.readableTimestamp = readableTimestamp;
260261
return this;
261262
}
@@ -321,7 +322,11 @@ protected List<String> createArgs() {
321322

322323
@Override
323324
protected ChromeDriverService createDriverService(
324-
File exe, int port, Duration timeout, List<String> args, Map<String, String> environment) {
325+
@Nullable File exe,
326+
int port,
327+
@Nullable Duration timeout,
328+
@Nullable List<String> args,
329+
@Nullable Map<String, String> environment) {
325330
try {
326331
return new ChromeDriverService(exe, port, timeout, args, environment);
327332
} catch (IOException e) {

java/src/org/openqa/selenium/grid/commands/InfoCommand.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.nio.charset.StandardCharsets;
3232
import java.util.Collections;
3333
import java.util.Set;
34+
import java.util.regex.Pattern;
3435
import org.openqa.selenium.cli.CliCommand;
3536
import org.openqa.selenium.cli.WrappedPrintWriter;
3637
import org.openqa.selenium.grid.config.Role;
@@ -39,6 +40,8 @@
3940
@AutoService(CliCommand.class)
4041
public class InfoCommand implements CliCommand {
4142

43+
private static final Pattern CODE_OR_LIST_PATTERN = Pattern.compile("^\\s*(\\*|\\d+\\.).*");
44+
4245
public String getName() {
4346
return "info";
4447
}
@@ -109,7 +112,7 @@ public Executable configure(PrintStream out, PrintStream err, String... args) {
109112
break;
110113
}
111114

112-
String path = getClass().getPackage().getName().replaceAll("\\.", "/") + "/" + toDisplay;
115+
String path = getClass().getPackage().getName().replace('.', '/') + "/" + toDisplay;
113116
String content;
114117
try {
115118
content = readContent(path);
@@ -143,11 +146,11 @@ private String readContent(String path) throws IOException {
143146
} else if ("```".equals(line)) {
144147
inCode = !inCode;
145148
} else {
146-
if (line.startsWith("=")) {
149+
if (line.charAt(0) == '=') {
147150
formattedText.append("\n");
148151
}
149152
formattedText.append(line);
150-
if (inCode || line.matches("^\\s*\\*.*") || line.matches("^\\s*\\d+\\..*")) {
153+
if (inCode || CODE_OR_LIST_PATTERN.matcher(line).matches()) {
151154
formattedText.append("\n");
152155
} else {
153156
formattedText.append(" ");

java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public class DockerOptions {
6868
private static final String DEFAULT_DOCKER_NETWORK = "bridge";
6969
private static final Logger LOG = Logger.getLogger(DockerOptions.class.getName());
7070
private static final Json JSON = new Json();
71+
private static final Pattern LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS =
72+
Pattern.compile("^([\\w/-]+):([\\w/-]+)$");
73+
private static final Pattern LINUX_DEVICE_MAPPING_WITH_PERMISSIONS =
74+
Pattern.compile("^([\\w/-]+):([\\w/-]+):(\\w+)$");
7175
private final Config config;
7276

7377
public DockerOptions(Config config) {
@@ -209,23 +213,17 @@ public Map<Capabilities, Collection<SessionFactory>> getDockerSessionFactories(
209213
}
210214

211215
protected List<Device> getDevicesMapping() {
212-
Pattern linuxDeviceMappingWithDefaultPermissionsPattern =
213-
Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+)$");
214-
Pattern linuxDeviceMappingWithPermissionsPattern =
215-
Pattern.compile("^([\\w\\/-]+):([\\w\\/-]+):([\\w]+)$");
216-
217216
List<String> devices =
218217
config.getAll(DOCKER_SECTION, "devices").orElseGet(Collections::emptyList);
219218

220219
List<Device> deviceMapping = new ArrayList<>();
221220
for (String device : devices) {
222221
String deviceMappingDefined = device.trim();
223-
Matcher matcher =
224-
linuxDeviceMappingWithDefaultPermissionsPattern.matcher(deviceMappingDefined);
222+
Matcher matcher = LINUX_DEVICE_MAPPING_WITH_DEFAULT_PERMISSIONS.matcher(deviceMappingDefined);
225223

226224
if (matcher.matches()) {
227225
deviceMapping.add(device(matcher.group(1), matcher.group(2), null));
228-
} else if ((matcher = linuxDeviceMappingWithPermissionsPattern.matcher(deviceMappingDefined))
226+
} else if ((matcher = LINUX_DEVICE_MAPPING_WITH_PERMISSIONS.matcher(deviceMappingDefined))
229227
.matches()) {
230228
deviceMapping.add(device(matcher.group(1), matcher.group(2), matcher.group(3)));
231229
}

java/src/org/openqa/selenium/net/Urls.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.net.URL;
2626
import java.net.URLEncoder;
2727
import java.nio.charset.StandardCharsets;
28-
import java.util.regex.Pattern;
2928
import org.openqa.selenium.internal.Require;
3029

3130
public class Urls {
@@ -88,7 +87,7 @@ public static URI from(String rawUri) {
8887
return createHttpUri(rawUri);
8988
}
9089

91-
if (Pattern.matches("\\d+", rawUri.substring(0, colonIndex))) {
90+
if (isAllDigits(rawUri.substring(0, colonIndex))) {
9291
return createHttpUri(rawUri);
9392
}
9493
}
@@ -101,6 +100,16 @@ public static URI from(String rawUri) {
101100
}
102101
}
103102

103+
private static boolean isAllDigits(final String input) {
104+
for (int i = 0; i < input.length(); i++) {
105+
if (!Character.isDigit(input.charAt(i))) {
106+
return false;
107+
}
108+
}
109+
110+
return !input.isEmpty();
111+
}
112+
104113
private static URI createHttpUri(String rawHost) {
105114
int slashIndex = rawHost.indexOf('/');
106115
String host = slashIndex == -1 ? rawHost : rawHost.substring(0, slashIndex);

java/src/org/openqa/selenium/remote/codec/w3c/W3CHttpCommandCodec.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
100100

101101
private static final ConcurrentHashMap<String, String> ATOM_SCRIPTS = new ConcurrentHashMap<>();
102102
private static final Pattern CSS_ESCAPE =
103-
Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-\\/\\[\\]\\(\\)])");
103+
Pattern.compile("([\\s'\"\\\\#.:;,!?+<>=~*^$|%&@`{}\\-/\\[\\]()])");
104+
private static final Pattern AT_LEAST_ONE_WHITESPACE = Pattern.compile(".*\\s.*");
104105

105106
public W3CHttpCommandCodec() {
106107
String sessionId = "/session/:sessionId";
@@ -181,7 +182,7 @@ public W3CHttpCommandCodec() {
181182
String stringValue = (String) value;
182183
switch (using) {
183184
case "class name":
184-
if (stringValue.matches(".*\\s.*")) {
185+
if (AT_LEAST_ONE_WHITESPACE.matcher(stringValue).matches()) {
185186
throw new InvalidSelectorException("Compound class names not permitted");
186187
}
187188
return amendLocatorToCssSelector(parameters, "." + cssEscape(stringValue));

0 commit comments

Comments
 (0)