diff options
author | Luck <git@lucko.me> | 2021-06-01 21:32:34 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-06-01 21:32:34 +0100 |
commit | c4e8ab0dad45fe33bff89930ad76bac4d2854133 (patch) | |
tree | bb1bc970aa54f450d960fba465d8a6465523a3ee /spark-common/src/main/java/me/lucko/spark | |
parent | 05095b0b30b0efa7f17b3f320e29b5a811329125 (diff) | |
download | spark-c4e8ab0dad45fe33bff89930ad76bac4d2854133.tar.gz spark-c4e8ab0dad45fe33bff89930ad76bac4d2854133.tar.bz2 spark-c4e8ab0dad45fe33bff89930ad76bac4d2854133.zip |
Add class source lookups for Fabric and Forge
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java index 27e3ec6..9f58c7f 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java @@ -29,6 +29,7 @@ import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; +import java.nio.file.Path; import java.nio.file.Paths; import java.security.CodeSource; import java.security.ProtectionDomain; @@ -97,6 +98,18 @@ public interface ClassSourceLookup { } return null; } + + protected String identifyUrl(URL url) throws URISyntaxException { + return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null; + } + + protected String identifyFile(Path path) { + return identifyFileName(path.getFileName().toString()); + } + + protected String identifyFileName(String fileName) { + return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null; + } } /** @@ -117,24 +130,20 @@ public interface ClassSourceLookup { URL url = codeSource.getLocation(); return url == null ? null : identifyUrl(url); } - } - /** - * Attempts to identify a jar file from a URL. - * - * @param url the url - * @return the name of the file - * @throws URISyntaxException thrown by {@link URL#toURI()} - */ - static String identifyUrl(URL url) throws URISyntaxException { - if (url.getProtocol().equals("file")) { - String jarName = Paths.get(url.toURI()).getFileName().toString(); - if (jarName.endsWith(".jar")) { - return jarName.substring(0, jarName.length() - 4); - } + protected String identifyUrl(URL url) throws URISyntaxException { + return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null; + } + + protected String identifyFile(Path path) { + return identifyFileName(path.getFileName().toString()); + } + + protected String identifyFileName(String fileName) { + return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null; } - return null; } + interface Visitor { void visit(ThreadNode node); |