From 1aa1aea43e87c8d183b21351dc0cf7d87413db2d Mon Sep 17 00:00:00 2001 From: zhangzq7 <116408254+zhangzq7@users.noreply.github.com> Date: Wed, 2 Nov 2022 12:57:46 +0800 Subject: [PATCH] use Files.newInputStream instead of new FileInputStream and null check (#10847) --- .../dubbo/maven/plugin/ClassFinder.java | 21 +++++++++++-------- .../org/apache/dubbo/utils/ClassFinder.java | 20 ++++++++++-------- .../ConfigZookeeperInitializer.java | 7 ++++--- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/dubbo-native-plugin/src/main/java/org/apache/dubbo/maven/plugin/ClassFinder.java b/dubbo-native-plugin/src/main/java/org/apache/dubbo/maven/plugin/ClassFinder.java index c5c350e379..f05a20c2c8 100644 --- a/dubbo-native-plugin/src/main/java/org/apache/dubbo/maven/plugin/ClassFinder.java +++ b/dubbo-native-plugin/src/main/java/org/apache/dubbo/maven/plugin/ClassFinder.java @@ -56,15 +56,18 @@ public class ClassFinder { private void findClassesByFile(String packageName, String resource, Set 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, ".")); + } } } } diff --git a/dubbo-native/src/main/java/org/apache/dubbo/utils/ClassFinder.java b/dubbo-native/src/main/java/org/apache/dubbo/utils/ClassFinder.java index c78d108be8..75ef3277e8 100644 --- a/dubbo-native/src/main/java/org/apache/dubbo/utils/ClassFinder.java +++ b/dubbo-native/src/main/java/org/apache/dubbo/utils/ClassFinder.java @@ -56,15 +56,17 @@ public class ClassFinder { private void findClassesByFile(String packageName, String resource, Set 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, ".")); + } } } } diff --git a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java index cc50bb958a..f703f1e780 100644 --- a/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java +++ b/dubbo-test/dubbo-test-check/src/main/java/org/apache/dubbo/test/check/registrycenter/initializer/ConfigZookeeperInitializer.java @@ -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 {