From c4e8ab0dad45fe33bff89930ad76bac4d2854133 Mon Sep 17 00:00:00 2001 From: Luck Date: Tue, 1 Jun 2021 21:32:34 +0100 Subject: Add class source lookups for Fabric and Forge --- .../lucko/spark/common/util/ClassSourceLookup.java | 39 +++++++++++++--------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'spark-common/src/main/java/me/lucko/spark') 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); -- cgit