diff options
6 files changed, 48 insertions, 48 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java index d67c092..414db57 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlatform.java @@ -95,7 +95,6 @@ public class SparkPlatform { private final ActivityLog activityLog; private final TickHook tickHook; private final TickReporter tickReporter; - private final ClassSourceLookup classSourceLookup; private final TickStatistics tickStatistics; private Map<String, GarbageCollectorStatistics> startupGcStatistics = ImmutableMap.of(); private long serverNormalOperationStartTime; @@ -132,7 +131,6 @@ public class SparkPlatform { this.tickHook = plugin.createTickHook(); this.tickReporter = plugin.createTickReporter(); - this.classSourceLookup = plugin.createClassSourceLookup(); this.tickStatistics = this.tickHook != null ? new TickStatistics() : null; } @@ -212,8 +210,8 @@ public class SparkPlatform { return this.tickReporter; } - public ClassSourceLookup getClassSourceLookup() { - return this.classSourceLookup; + public ClassSourceLookup createClassSourceLookup() { + return this.plugin.createClassSourceLookup(); } public TickStatistics getTickStatistics() { diff --git a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java index d45c7af..2dd07c9 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java +++ b/spark-common/src/main/java/me/lucko/spark/common/command/modules/SamplerModule.java @@ -305,7 +305,7 @@ public class SamplerModule implements CommandModule { } private void handleUpload(SparkPlatform platform, CommandResponseHandler resp, Sampler sampler, ThreadNodeOrder threadOrder, String comment, MergeMode mergeMode, boolean saveToFileFlag) { - SparkProtos.SamplerData output = sampler.toProto(platform.getPlugin().getPlatformInfo(), resp.sender(), threadOrder, comment, mergeMode, platform.getClassSourceLookup()); + SparkProtos.SamplerData output = sampler.toProto(platform.getPlugin().getPlatformInfo(), resp.sender(), threadOrder, comment, mergeMode, platform.createClassSourceLookup()); boolean saveToFile = false; if (saveToFileFlag) { 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 { diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java index fad0bc8..fa59079 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/FabricClassSourceLookup.java @@ -34,7 +34,7 @@ public class FabricClassSourceLookup extends ClassSourceLookup.ByCodeSource { } @Override - protected String identifyFile(Path path) { + public String identifyFile(Path path) { if (!path.startsWith(this.modsDirectory)) { return null; } diff --git a/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityClassSourceLookup.java b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityClassSourceLookup.java index 4989bf4..bcb8176 100644 --- a/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityClassSourceLookup.java +++ b/spark-velocity/src/main/java/me/lucko/spark/velocity/VelocityClassSourceLookup.java @@ -27,6 +27,9 @@ import me.lucko.spark.common.util.ClassSourceLookup; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.HashMap; +import java.util.Map; + public class VelocityClassSourceLookup extends ClassSourceLookup.ByClassLoader { private static final Class<?> PLUGIN_CLASS_LOADER; @@ -38,21 +41,22 @@ public class VelocityClassSourceLookup extends ClassSourceLookup.ByClassLoader { } } - private final PluginManager pluginManager; + private final Map<ClassLoader, String> classLoadersToPlugin; public VelocityClassSourceLookup(PluginManager pluginManager) { - this.pluginManager = pluginManager; + this.classLoadersToPlugin = new HashMap<>(); + for (PluginContainer plugin : pluginManager.getPlugins()) { + plugin.getInstance().ifPresent(instance -> { + String id = plugin.getDescription().getName().orElseGet(() -> plugin.getDescription().getId()); + this.classLoadersToPlugin.put(instance.getClass().getClassLoader(), id); + }); + } } @Override public @Nullable String identify(ClassLoader loader) { if (PLUGIN_CLASS_LOADER.isInstance(loader)) { - for (PluginContainer plugin : this.pluginManager.getPlugins()) { - Object instance = plugin.getInstance().orElse(null); - if (instance != null && instance.getClass().getClassLoader() == loader) { - return plugin.getDescription().getName().orElseGet(() -> plugin.getDescription().getId()); - } - } + return this.classLoadersToPlugin.get(loader); } return null; } diff --git a/spark-velocity4/src/main/java/me/lucko/spark/velocity/Velocity4ClassSourceLookup.java b/spark-velocity4/src/main/java/me/lucko/spark/velocity/Velocity4ClassSourceLookup.java index 28bf3da..c5c22c3 100644 --- a/spark-velocity4/src/main/java/me/lucko/spark/velocity/Velocity4ClassSourceLookup.java +++ b/spark-velocity4/src/main/java/me/lucko/spark/velocity/Velocity4ClassSourceLookup.java @@ -27,6 +27,9 @@ import me.lucko.spark.common.util.ClassSourceLookup; import org.checkerframework.checker.nullness.qual.Nullable; +import java.util.HashMap; +import java.util.Map; + public class Velocity4ClassSourceLookup extends ClassSourceLookup.ByClassLoader { private static final Class<?> PLUGIN_CLASS_LOADER; @@ -38,21 +41,22 @@ public class Velocity4ClassSourceLookup extends ClassSourceLookup.ByClassLoader } } - private final PluginManager pluginManager; + private final Map<ClassLoader, String> classLoadersToPlugin; public Velocity4ClassSourceLookup(PluginManager pluginManager) { - this.pluginManager = pluginManager; + this.classLoadersToPlugin = new HashMap<>(); + for (PluginContainer plugin : pluginManager.plugins()) { + Object instance = plugin.instance(); + if (instance != null) { + this.classLoadersToPlugin.put(instance.getClass().getClassLoader(), plugin.description().name()); + } + } } @Override public @Nullable String identify(ClassLoader loader) { if (PLUGIN_CLASS_LOADER.isInstance(loader)) { - for (PluginContainer plugin : this.pluginManager.plugins()) { - Object instance = plugin.instance(); - if (instance != null && instance.getClass().getClassLoader() == loader) { - return plugin.description().name(); - } - } + return this.classLoadersToPlugin.get(loader); } return null; } |