Skip to content

Commit 470ae76

Browse files
AndreyBozhkoAndrey Bozhko
and
Andrey Bozhko
authored
Add Paths#get to forbidden APIs (#3289)
* add Paths.get to forbidden apis * replace Paths.get with Path.of Co-authored-by: Andrey Bozhko <[email protected]>
1 parent 86f2afd commit 470ae76

File tree

71 files changed

+173
-218
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+173
-218
lines changed

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/GradlePropertiesGenerator.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.nio.charset.StandardCharsets;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23-
import java.nio.file.Paths;
2423
import java.nio.file.StandardOpenOption;
2524
import java.util.Map;
2625

@@ -37,7 +36,7 @@ public static void main(String[] args) {
3736
}
3837

3938
try {
40-
new GradlePropertiesGenerator().run(Paths.get(args[0]), Paths.get(args[1]));
39+
new GradlePropertiesGenerator().run(Path.of(args[0]), Path.of(args[1]));
4140
} catch (Exception e) {
4241
System.err.println("ERROR: " + e.getMessage());
4342
System.exit(3);

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/ProfileResults.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package org.apache.lucene.gradle;
1919

2020
import java.io.IOException;
21-
import java.nio.file.Paths;
21+
import java.nio.file.Path;
2222
import java.util.AbstractMap.SimpleEntry;
2323
import java.util.ArrayList;
2424
import java.util.Arrays;
@@ -168,7 +168,7 @@ public static void printReport(
168168
long sumValues = 0;
169169
String framePadding = " ".repeat(COLUMN_SIZE * 2);
170170
for (String file : files) {
171-
try (RecordingFile recording = new RecordingFile(Paths.get(file))) {
171+
try (RecordingFile recording = new RecordingFile(Path.of(file))) {
172172
while (recording.hasMoreEvents()) {
173173
RecordedEvent event = recording.readEvent();
174174
if (!isInteresting(mode, event)) {

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/WrapperDownloader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.nio.charset.StandardCharsets;
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
29-
import java.nio.file.Paths;
3029
import java.security.MessageDigest;
3130
import java.security.NoSuchAlgorithmException;
3231
import java.util.Locale;
@@ -46,7 +45,7 @@ public static void main(String[] args) {
4645

4746
try {
4847
checkVersion();
49-
new WrapperDownloader().run(Paths.get(args[0]));
48+
new WrapperDownloader().run(Path.of(args[0]));
5049
} catch (Exception e) {
5150
System.err.println("ERROR: " + e.getMessage());
5251
System.exit(3);

gradle/testing/randomization.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ allprojects {
218218
systemProperty 'java.security.manager', "default"
219219

220220
def gradleUserHome = project.gradle.getGradleUserHomeDir()
221-
systemProperty 'gradle.lib.dir', Paths.get(project.class.location.toURI()).parent.toAbsolutePath().toString().replace('\\', '/')
222-
systemProperty 'gradle.worker.jar', Paths.get("${gradleUserHome}/caches/${gradle.gradleVersion}/workerMain/gradle-worker.jar").toAbsolutePath().toString()
221+
systemProperty 'gradle.lib.dir', Path.of(project.class.location.toURI()).parent.toAbsolutePath().toString().replace('\\', '/')
222+
systemProperty 'gradle.worker.jar', Path.of("${gradleUserHome}/caches/${gradle.gradleVersion}/workerMain/gradle-worker.jar").toAbsolutePath().toString()
223223
systemProperty 'gradle.user.home', gradleUserHome.toPath().toAbsolutePath().toString()
224224
}
225225

gradle/validation/forbidden-apis/defaults.all.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,7 @@ java.util.Collections#sort(java.util.List, java.util.Comparator)
9393
java.net.URL#<init>(**)
9494

9595
@defaultMessage Use Locale.Builder instead.
96-
java.util.Locale#<init>(**)
96+
java.util.Locale#<init>(**)
97+
98+
@defaultMessage Use Path.of(...) methods instead.
99+
java.nio.file.Paths#get(**)

solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.nio.file.FileSystems;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32-
import java.nio.file.Paths;
3332
import java.util.ArrayList;
3433
import java.util.List;
3534
import java.util.SplittableRandom;
@@ -122,7 +121,7 @@ public void tearDown(BenchmarkParams benchmarkParams) throws Exception {
122121

123122
// dump Solr metrics
124123
Path metricsResults =
125-
Paths.get(
124+
Path.of(
126125
workDir,
127126
"metrics-results",
128127
benchmarkParams.id(),
@@ -203,7 +202,7 @@ public void doSetup(BenchmarkParams benchmarkParams, BaseBenchState baseBenchSta
203202

204203
workDir = System.getProperty("workBaseDir", "build/work");
205204

206-
Path currentRelativePath = Paths.get("");
205+
Path currentRelativePath = Path.of("");
207206
String s = currentRelativePath.toAbsolutePath().toString();
208207
log("current relative path is: " + s);
209208
log("work path is: " + workDir);
@@ -226,13 +225,13 @@ public void doSetup(BenchmarkParams benchmarkParams, BaseBenchState baseBenchSta
226225
String baseDirSysProp = System.getProperty("miniClusterBaseDir");
227226
if (baseDirSysProp != null) {
228227
deleteMiniCluster = false;
229-
miniClusterBaseDir = Paths.get(baseDirSysProp);
228+
miniClusterBaseDir = Path.of(baseDirSysProp);
230229
if (Files.exists(miniClusterBaseDir)) {
231230
createCollectionAndIndex = false;
232231
allowClusterReuse = true;
233232
}
234233
} else {
235-
miniClusterBaseDir = Paths.get(workDir, "mini-cluster");
234+
miniClusterBaseDir = Path.of(workDir, "mini-cluster");
236235
}
237236

238237
System.setProperty("metricsEnabled", String.valueOf(metricsEnabled));
@@ -584,6 +583,6 @@ public static Path getFile(String name) {
584583
"Cannot find resource in classpath or in file-system (relative to CWD): "
585584
+ name
586585
+ " CWD="
587-
+ Paths.get("").toAbsolutePath());
586+
+ Path.of("").toAbsolutePath());
588587
}
589588
}

solr/core/src/java/org/apache/solr/cli/AssertTool.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.invoke.MethodHandles;
2121
import java.nio.file.Files;
2222
import java.nio.file.Path;
23-
import java.nio.file.Paths;
2423
import java.nio.file.attribute.FileOwnerAttributeView;
2524
import java.util.concurrent.TimeUnit;
2625
import org.apache.commons.cli.CommandLine;
@@ -344,8 +343,8 @@ public int assertSolrNotRunningInCloudMode(String url, String credentials) throw
344343
}
345344

346345
public static int sameUser(String directory) throws Exception {
347-
if (Files.exists(Paths.get(directory))) {
348-
String userForDir = userForDir(Paths.get(directory));
346+
if (Files.exists(Path.of(directory))) {
347+
String userForDir = userForDir(Path.of(directory));
349348
if (!currentUser().equals(userForDir)) {
350349
return exitOrException("Must run as user " + userForDir + ". We are " + currentUser());
351350
}
@@ -356,14 +355,14 @@ public static int sameUser(String directory) throws Exception {
356355
}
357356

358357
public static int assertFileExists(String directory) throws Exception {
359-
if (!Files.exists(Paths.get(directory))) {
358+
if (!Files.exists(Path.of(directory))) {
360359
return exitOrException("Directory " + directory + " does not exist.");
361360
}
362361
return 0;
363362
}
364363

365364
public static int assertFileNotExists(String directory) throws Exception {
366-
if (Files.exists(Paths.get(directory))) {
365+
if (Files.exists(Path.of(directory))) {
367366
return exitOrException("Directory " + directory + " should not exist.");
368367
}
369368
return 0;

solr/core/src/java/org/apache/solr/cli/CLIUtils.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.net.URI;
2727
import java.net.URISyntaxException;
2828
import java.nio.file.Path;
29-
import java.nio.file.Paths;
3029
import java.util.Arrays;
3130
import java.util.Collections;
3231
import java.util.List;
@@ -343,8 +342,7 @@ public static boolean isCloudMode(SolrClient solrClient) throws SolrServerExcept
343342
}
344343

345344
public static Path getConfigSetsDir(Path solrInstallDir) {
346-
Path configSetsPath = Paths.get("server/solr/configsets/");
347-
return solrInstallDir.resolve(configSetsPath);
345+
return solrInstallDir.resolve("server/solr/configsets");
348346
}
349347

350348
public static boolean isWindows() {

solr/core/src/java/org/apache/solr/cli/ConfigSetDownloadTool.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.lang.invoke.MethodHandles;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22-
import java.nio.file.Paths;
2322
import org.apache.commons.cli.CommandLine;
2423
import org.apache.commons.cli.Option;
2524
import org.apache.commons.cli.Options;
@@ -81,11 +80,11 @@ public void runImpl(CommandLine cli) throws Exception {
8180

8281
echoIfVerbose("\nConnecting to ZooKeeper at " + zkHost + " ...");
8382
try (SolrZkClient zkClient = CLIUtils.getSolrZkClient(cli, zkHost)) {
84-
Path configSetPath = Paths.get(confDir);
83+
Path configSetPath = Path.of(confDir);
8584
// we try to be nice about having the "conf" in the directory, and we create it if it's not
8685
// there.
8786
if (!configSetPath.endsWith("/conf")) {
88-
configSetPath = Paths.get(configSetPath.toString(), "conf");
87+
configSetPath = configSetPath.resolve("conf");
8988
}
9089
Files.createDirectories(configSetPath);
9190
echo(

solr/core/src/java/org/apache/solr/cli/ConfigSetUploadTool.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.lang.invoke.MethodHandles;
2020
import java.nio.file.Path;
21-
import java.nio.file.Paths;
2221
import org.apache.commons.cli.CommandLine;
2322
import org.apache.commons.cli.Option;
2423
import org.apache.commons.cli.Options;
@@ -79,7 +78,7 @@ public void runImpl(CommandLine cli) throws Exception {
7978
String zkHost = CLIUtils.getZkHost(cli);
8079

8180
final String solrInstallDir = System.getProperty("solr.install.dir");
82-
Path solrInstallDirPath = Paths.get(solrInstallDir);
81+
Path solrInstallDirPath = Path.of(solrInstallDir);
8382

8483
String confName = cli.getOptionValue(CONF_NAME_OPTION);
8584
String confDir = cli.getOptionValue(CONF_DIR_OPTION);

solr/core/src/java/org/apache/solr/cli/CreateTool.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.io.IOException;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22-
import java.nio.file.Paths;
2322
import java.nio.file.StandardCopyOption;
2423
import java.util.Locale;
2524
import java.util.Set;
@@ -151,8 +150,8 @@ protected void createCore(CommandLine cli, SolrClient solrClient) throws Excepti
151150
cli.getOptionValue(CONF_DIR_OPTION, DefaultValues.DEFAULT_CONFIG_SET);
152151

153152
// we allow them to pass a directory instead of a configset name
154-
Path configsetDir = Paths.get(confDirName);
155-
Path solrInstallDirPath = Paths.get(solrInstallDir);
153+
Path configsetDir = Path.of(confDirName);
154+
Path solrInstallDirPath = Path.of(solrInstallDir);
156155

157156
if (!Files.isDirectory(configsetDir)) {
158157
ensureConfDirExists(solrInstallDirPath, configsetDir);
@@ -176,7 +175,7 @@ protected void createCore(CommandLine cli, SolrClient solrClient) throws Excepti
176175
+ "' already exists!\nChecked core existence using Core API command");
177176
}
178177

179-
Path coreInstanceDir = Paths.get(coreRootDirectory, coreName);
178+
Path coreInstanceDir = Path.of(coreRootDirectory, coreName);
180179
Path confDir = getFullConfDir(solrInstallDirPath, configsetDir).resolve("conf");
181180
if (!Files.isDirectory(coreInstanceDir)) {
182181
Files.createDirectories(coreInstanceDir);
@@ -232,8 +231,8 @@ protected void createCollection(CloudSolrClient cloudSolrClient, CommandLine cli
232231
final String solrInstallDir = System.getProperty("solr.install.dir");
233232
String confName = cli.getOptionValue(CONF_NAME_OPTION);
234233
String confDir = cli.getOptionValue(CONF_DIR_OPTION, DefaultValues.DEFAULT_CONFIG_SET);
235-
Path solrInstallDirPath = Paths.get(solrInstallDir);
236-
Path confDirPath = Paths.get(confDir);
234+
Path solrInstallDirPath = Path.of(solrInstallDir);
235+
Path confDirPath = Path.of(confDir);
237236
ensureConfDirExists(solrInstallDirPath, confDirPath);
238237
printDefaultConfigsetWarningIfNecessary(cli);
239238

solr/core/src/java/org/apache/solr/cli/SolrProcessManager.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.nio.charset.StandardCharsets;
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
28-
import java.nio.file.Paths;
2928
import java.util.ArrayList;
3029
import java.util.Arrays;
3130
import java.util.Collection;
@@ -71,7 +70,7 @@ public SolrProcessManager() {
7170
pidProcessMap.values().stream().collect(Collectors.toUnmodifiableMap(p -> p.port, p -> p));
7271
String solrInstallDir = EnvUtils.getProperty(SOLR_INSTALL_DIR_ATTRIBUTE);
7372
pidDir =
74-
Paths.get(
73+
Path.of(
7574
EnvUtils.getProperty(
7675
"solr.pid.dir",
7776
solrInstallDir != null

solr/core/src/java/org/apache/solr/cli/StreamTool.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.nio.charset.StandardCharsets;
2929
import java.nio.file.Files;
3030
import java.nio.file.Path;
31-
import java.nio.file.Paths;
3231
import java.util.ArrayList;
3332
import java.util.Arrays;
3433
import java.util.HashMap;
@@ -437,7 +436,7 @@ protected List<CrawlFile> validateAndSetFilepathsInSandbox() {
437436

438437
final List<CrawlFile> crawlSeeds = new ArrayList<>();
439438
for (String crawlRootStr : commaDelimitedFilepaths.split(",")) {
440-
Path crawlRootPath = Paths.get(crawlRootStr).normalize();
439+
Path crawlRootPath = Path.of(crawlRootStr).normalize();
441440

442441
if (!Files.exists(crawlRootPath)) {
443442
throw new SolrException(

solr/core/src/java/org/apache/solr/cli/ZkCpTool.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.invoke.MethodHandles;
2222
import java.lang.reflect.InvocationTargetException;
2323
import java.nio.file.Path;
24-
import java.nio.file.Paths;
2524
import java.util.Locale;
2625
import java.util.Properties;
2726
import java.util.concurrent.TimeUnit;
@@ -165,7 +164,7 @@ public void runImpl(CommandLine cli) throws Exception {
165164
// solr start -DminStateByteLenForCompression=0 -c, this logic will not
166165
// know about the -DminStateByteLenForCompression and only return the
167166
// version set in the solr.xml. So you must edit solr.xml directly.
168-
Path solrHomePath = Paths.get(solrHome);
167+
Path solrHomePath = Path.of(solrHome);
169168
Properties props = new Properties();
170169
props.put(SolrXmlConfig.ZK_HOST, zkHost);
171170
NodeConfig nodeConfig = NodeConfig.loadNodeConfig(solrHomePath, props);

solr/core/src/java/org/apache/solr/core/CachingDirectoryFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.nio.file.DirectoryStream;
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
24-
import java.nio.file.Paths;
2524
import java.util.ArrayList;
2625
import java.util.Collection;
2726
import java.util.HashMap;
@@ -480,7 +479,7 @@ public void init(NamedList<?> args) {
480479
// override global config
481480
if (args.get(SolrXmlConfig.SOLR_DATA_HOME) != null) {
482481
dataHomePath =
483-
Paths.get((String) args.get(SolrXmlConfig.SOLR_DATA_HOME)).toAbsolutePath().normalize();
482+
Path.of((String) args.get(SolrXmlConfig.SOLR_DATA_HOME)).toAbsolutePath().normalize();
484483
}
485484
if (dataHomePath != null) {
486485
log.info("{} = {}", SolrXmlConfig.SOLR_DATA_HOME, dataHomePath);

solr/core/src/java/org/apache/solr/core/CoreContainer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import java.io.IOException;
3636
import java.lang.invoke.MethodHandles;
3737
import java.nio.file.Path;
38-
import java.nio.file.Paths;
3938
import java.text.SimpleDateFormat;
4039
import java.util.Arrays;
4140
import java.util.Collections;
@@ -1597,7 +1596,7 @@ public SolrCore create(
15971596

15981597
// Validate paths are relative to known locations to avoid path traversal
15991598
assertPathAllowed(cd.getInstanceDir());
1600-
assertPathAllowed(Paths.get(cd.getDataDir()));
1599+
assertPathAllowed(Path.of(cd.getDataDir()));
16011600

16021601
boolean preExistingZkEntry = false;
16031602
try {

solr/core/src/java/org/apache/solr/core/NodeConfig.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.net.URL;
2222
import java.nio.file.Files;
2323
import java.nio.file.Path;
24-
import java.nio.file.Paths;
2524
import java.util.Arrays;
2625
import java.util.Collections;
2726
import java.util.LinkedHashSet;
@@ -307,7 +306,7 @@ public static Path getSolrInstallDir() {
307306
log.debug("solr.install.dir property not initialized.");
308307
return null;
309308
}
310-
return Paths.get(prop);
309+
return Path.of(prop);
311310
}
312311

313312
/**

solr/core/src/java/org/apache/solr/core/SolrPaths.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.lang.invoke.MethodHandles;
2121
import java.nio.file.FileSystems;
2222
import java.nio.file.Path;
23-
import java.nio.file.Paths;
2423
import java.util.Collections;
2524
import java.util.HashSet;
2625
import java.util.Set;
@@ -34,7 +33,7 @@ public final class SolrPaths {
3433
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
3534

3635
/** Special path which means to accept all paths. */
37-
public static final Path ALL_PATH = Paths.get("_ALL_");
36+
public static final Path ALL_PATH = Path.of("_ALL_");
3837

3938
/** Special singleton path set containing only {@link #ALL_PATH}. */
4039
private static final Set<Path> ALL_PATHS = Collections.singleton(ALL_PATH);
@@ -116,7 +115,7 @@ public AllowPathBuilder addPath(String path) {
116115
if (path.equals(WILDCARD_PATH)) {
117116
paths = ALL_PATHS;
118117
} else {
119-
addPath(Paths.get(path));
118+
addPath(Path.of(path));
120119
}
121120
return this;
122121
}

0 commit comments

Comments
 (0)