aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/util
diff options
context:
space:
mode:
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/ClassSourceLookup.java46
1 files changed, 20 insertions, 26 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 9f58c7f..42a04f7 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
@@ -83,9 +83,27 @@ public interface ClassSourceLookup {
}
/**
+ * A {@link ClassSourceLookup} which identifies classes based on URL.
+ */
+ interface ByUrl extends ClassSourceLookup {
+
+ default String identifyUrl(URL url) throws URISyntaxException {
+ return url.getProtocol().equals("file") ? identifyFile(Paths.get(url.toURI())) : null;
+ }
+
+ default String identifyFile(Path path) {
+ return identifyFileName(path.getFileName().toString());
+ }
+
+ default String identifyFileName(String fileName) {
+ return fileName.endsWith(".jar") ? fileName.substring(0, fileName.length() - 4) : null;
+ }
+ }
+
+ /**
* A {@link ClassSourceLookup} which identifies classes based on the first URL in a {@link URLClassLoader}.
*/
- class ByFirstUrlSource extends ByClassLoader {
+ class ByFirstUrlSource extends ByClassLoader implements ByUrl {
@Override
public @Nullable String identify(ClassLoader loader) throws IOException, URISyntaxException {
if (loader instanceof URLClassLoader) {
@@ -98,24 +116,12 @@ 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;
- }
}
/**
* A {@link ClassSourceLookup} which identifies classes based on their {@link ProtectionDomain#getCodeSource()}.
*/
- class ByCodeSource implements ClassSourceLookup {
+ class ByCodeSource implements ClassSourceLookup, ByUrl {
@Override
public @Nullable String identify(Class<?> clazz) throws URISyntaxException {
ProtectionDomain protectionDomain = clazz.getProtectionDomain();
@@ -130,18 +136,6 @@ public interface ClassSourceLookup {
URL url = codeSource.getLocation();
return url == null ? null : identifyUrl(url);
}
-
- 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;
- }
}
interface Visitor {