diff --git a/memorycompiler/src/main/java/com/taobao/arthas/compiler/PackageInternalsFinder.java b/memorycompiler/src/main/java/com/taobao/arthas/compiler/PackageInternalsFinder.java index 62b2f7d6..cc5d183e 100644 --- a/memorycompiler/src/main/java/com/taobao/arthas/compiler/PackageInternalsFinder.java +++ b/memorycompiler/src/main/java/com/taobao/arthas/compiler/PackageInternalsFinder.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.net.JarURLConnection; import java.net.URI; import java.net.URL; +import java.net.URLDecoder; import java.util.ArrayList; import java.util.Collection; import java.util.Enumeration; @@ -55,7 +56,7 @@ public class PackageInternalsFinder { } private Collection listUnder(String packageName, URL packageFolderURL) { - File directory = new File(packageFolderURL.getFile()); + File directory = new File(decode(packageFolderURL.getFile())); if (directory.isDirectory()) { // browse local .class files - useful for local execution return processDir(packageName, directory); } else { // browse a jar file @@ -108,4 +109,14 @@ public class PackageInternalsFinder { return result; } + + private String decode(String filePath) { + try { + return URLDecoder.decode(filePath, "utf-8"); + } catch (Exception e) { + // ignore, return original string + } + + return filePath; + } } diff --git a/memorycompiler/src/test/java/com/taobao/arthas/compiler/PackageInternalsFinderTest.java b/memorycompiler/src/test/java/com/taobao/arthas/compiler/PackageInternalsFinderTest.java new file mode 100644 index 00000000..535bf083 --- /dev/null +++ b/memorycompiler/src/test/java/com/taobao/arthas/compiler/PackageInternalsFinderTest.java @@ -0,0 +1,33 @@ +package com.taobao.arthas.compiler; + +import org.junit.Assert; +import org.junit.Test; + +import javax.tools.JavaFileObject; +import java.io.IOException; +import java.util.List; + +/** + * description: PackageInternalsFinderTest
+ * date: 2021/9/23 12:55 下午
+ * author: zzq0324
+ * version: 1.0
+ */ +public class PackageInternalsFinderTest { + + @Test + public void testFilePathContainWhitespace() throws IOException { + PackageInternalsFinder finder = new PackageInternalsFinder(this.getClass().getClassLoader()); + List fileObjectList= finder.find("file/test folder"); + + Assert.assertEquals(fileObjectList.size(), 0); + } + + @Test + public void testFilePathContainChineseCharacter() throws IOException { + PackageInternalsFinder finder = new PackageInternalsFinder(this.getClass().getClassLoader()); + List fileObjectList= finder.find("file/测试目录"); + + Assert.assertEquals(fileObjectList.size(), 0); + } +} diff --git a/memorycompiler/src/test/resources/file/test folder/file.txt b/memorycompiler/src/test/resources/file/test folder/file.txt new file mode 100644 index 00000000..326fdfb7 --- /dev/null +++ b/memorycompiler/src/test/resources/file/test folder/file.txt @@ -0,0 +1 @@ +This is a test file, it's name contains white space. \ No newline at end of file diff --git a/memorycompiler/src/test/resources/file/测试目录/file.txt b/memorycompiler/src/test/resources/file/测试目录/file.txt new file mode 100644 index 00000000..c9f1f720 --- /dev/null +++ b/memorycompiler/src/test/resources/file/测试目录/file.txt @@ -0,0 +1 @@ +This is a test file, it's name contains chinese character. \ No newline at end of file