Bugfix: PackageInternalsFinder support file path with white space and Unicode characters (#1921)

This commit is contained in:
漫天的沙 2021-09-23 18:48:16 +08:00 committed by GitHub
parent dee70a88c5
commit 4be365e2ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 1 deletions

View File

@ -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<JavaFileObject> 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;
}
}

View File

@ -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 <br>
* date: 2021/9/23 12:55 下午 <br>
* author: zzq0324 <br>
* version: 1.0 <br>
*/
public class PackageInternalsFinderTest {
@Test
public void testFilePathContainWhitespace() throws IOException {
PackageInternalsFinder finder = new PackageInternalsFinder(this.getClass().getClassLoader());
List<JavaFileObject> 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<JavaFileObject> fileObjectList= finder.find("file/测试目录");
Assert.assertEquals(fileObjectList.size(), 0);
}
}

View File

@ -0,0 +1 @@
This is a test file, it's name contains white space.

View File

@ -0,0 +1 @@
This is a test file, it's name contains chinese character.