use Files.newInputStream instead of new FileInputStream and null check (#10847)

This commit is contained in:
zhangzq7 2022-11-02 12:57:46 +08:00 committed by GitHub
parent d7c8d10dbf
commit 1aa1aea43e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 21 deletions

View File

@ -56,15 +56,18 @@ public class ClassFinder {
private void findClassesByFile(String packageName, String resource, Set<String> result) {
File directory = new File(resource);
File[] listFiles = directory.listFiles();
for (File file : listFiles) {
if (file.isDirectory()) {
findClassesByFile(packageName, file.getPath(), result);
} else {
String path = file.getPath();
if (path.endsWith(".class")) {
int packageIndex = path.indexOf(packageName.replace("/", File.separator));
String classPath = path.substring(packageIndex, path.length() - 6);
result.add(classPath.replace(File.separator, "."));
// null check
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory()) {
findClassesByFile(packageName, file.getPath(), result);
} else {
String path = file.getPath();
if (path.endsWith(".class")) {
int packageIndex = path.indexOf(packageName.replace("/", File.separator));
String classPath = path.substring(packageIndex, path.length() - 6);
result.add(classPath.replace(File.separator, "."));
}
}
}
}

View File

@ -56,15 +56,17 @@ public class ClassFinder {
private void findClassesByFile(String packageName, String resource, Set<String> result) {
File directory = new File(resource);
File[] listFiles = directory.listFiles();
for (File file : listFiles) {
if (file.isDirectory()) {
findClassesByFile(packageName, file.getPath(), result);
} else {
String path = file.getPath();
if (path.endsWith(".class")) {
int packageIndex = path.indexOf(packageName.replace("/", File.separator));
String classPath = path.substring(packageIndex, path.length() - 6);
result.add(classPath.replace(File.separator, "."));
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory()) {
findClassesByFile(packageName, file.getPath(), result);
} else {
String path = file.getPath();
if (path.endsWith(".class")) {
int packageIndex = path.indexOf(packageName.replace("/", File.separator));
String classPath = path.substring(packageIndex, path.length() - 6);
result.add(classPath.replace(File.separator, "."));
}
}
}
}

View File

@ -23,7 +23,6 @@ import org.apache.dubbo.test.check.exception.DubboTestException;
import org.apache.dubbo.test.check.registrycenter.context.ZookeeperContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
@ -54,7 +53,8 @@ public class ConfigZookeeperInitializer extends ZookeeperInitializer {
int availableAdminServerPort = NetUtils.getAvailablePort(adminServerPort);
Properties properties = new Properties();
try {
properties.load(new FileInputStream(zooSample));
// use Files.newInputStream instead of new FileInputStream
properties.load(Files.newInputStream(zooSample.toPath()));
properties.setProperty("clientPort", String.valueOf(clientPort));
properties.setProperty("admin.serverPort", String.valueOf(availableAdminServerPort));
Path dataDir = Paths.get(zookeeperConf.getParent().toString(), "data");
@ -87,7 +87,8 @@ public class ConfigZookeeperInitializer extends ZookeeperInitializer {
File log4j = Paths.get(zookeeperConf.toString(), "log4j.properties").toFile();
try {
properties.load(new FileInputStream(log4j));
// use Files.newInputStream instead of new FileInputStream
properties.load(Files.newInputStream(log4j.toPath()));
Path logDir = Paths.get(zookeeperConf.getParent().toString(), "logs");
if (!Files.exists(logDir)) {
try {