From ed8eac595c26719571683cff7f3c9de2cfc28406 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 18 Jul 2024 18:13:46 +0100 Subject: [PATCH] Refactor ClassFinder into interface --- .../me/lucko/spark/common/SparkPlatform.java | 5 ++ .../me/lucko/spark/common/SparkPlugin.java | 15 ++++++ .../common/command/modules/SamplerModule.java | 2 +- .../spark/common/sampler/AbstractSampler.java | 6 ++- .../common/sampler/async/AsyncSampler.java | 2 +- .../common/sampler/java/JavaSampler.java | 2 +- .../sampler/source/ClassSourceLookup.java | 12 +++-- .../common/util/MethodDisambiguator.java | 10 +++- .../common/util/classfinder/ClassFinder.java | 46 +++++++++++++++++++ .../util/classfinder/CombinedClassFinder.java | 44 ++++++++++++++++++ .../util/classfinder/FallbackClassFinder.java | 40 ++++++++++++++++ .../InstrumentationClassFinder.java} | 32 +++++-------- .../spark/fabric/FabricClassSourceLookup.java | 13 +++--- .../fabric/plugin/FabricSparkPlugin.java | 2 +- 14 files changed, 191 insertions(+), 40 deletions(-) create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/classfinder/ClassFinder.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/classfinder/CombinedClassFinder.java create mode 100644 spark-common/src/main/java/me/lucko/spark/common/util/classfinder/FallbackClassFinder.java rename spark-common/src/main/java/me/lucko/spark/common/util/{ClassFinder.java => classfinder/InstrumentationClassFinder.java} (70%) 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 21f92103..733510dc 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 @@ -55,6 +55,7 @@ import me.lucko.spark.common.util.Configuration; import me.lucko.spark.common.util.SparkStaticLogger; import me.lucko.spark.common.util.TemporaryFiles; +import me.lucko.spark.common.util.classfinder.ClassFinder; import me.lucko.spark.common.ws.TrustedKeyStore; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.event.ClickEvent; @@ -285,6 +286,10 @@ public ClassSourceLookup createClassSourceLookup() { return this.plugin.createClassSourceLookup(); } + public ClassFinder createClassFinder() { + return this.plugin.createClassFinder(); + } + public TickStatistics getTickStatistics() { return this.tickStatistics; } diff --git a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java index a3bdceb2..bf745dfd 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java +++ b/spark-common/src/main/java/me/lucko/spark/common/SparkPlugin.java @@ -33,6 +33,9 @@ import me.lucko.spark.common.sampler.source.SourceMetadata; import me.lucko.spark.common.tick.TickHook; import me.lucko.spark.common.tick.TickReporter; +import me.lucko.spark.common.util.classfinder.ClassFinder; +import me.lucko.spark.common.util.classfinder.FallbackClassFinder; +import me.lucko.spark.common.util.classfinder.InstrumentationClassFinder; import java.nio.file.Path; import java.util.Collection; @@ -149,6 +152,18 @@ default ClassSourceLookup createClassSourceLookup() { return ClassSourceLookup.NO_OP; } + /** + * Creates a class finder for the platform. + * + * @return the class finder + */ + default ClassFinder createClassFinder() { + return ClassFinder.combining( + new InstrumentationClassFinder(this), + FallbackClassFinder.INSTANCE + ); + } + /** * Gets a list of known sources (plugins/mods) on the platform. * 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 9ce66dc6..9e2647a0 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 @@ -507,7 +507,7 @@ private Sampler.ExportProps getExportProps(SparkPlatform platform, CommandRespon .creator(resp.senderData()) .comment(Iterables.getFirst(arguments.stringFlag("comment"), null)) .mergeMode(() -> { - MethodDisambiguator methodDisambiguator = new MethodDisambiguator(); + MethodDisambiguator methodDisambiguator = new MethodDisambiguator(platform.createClassFinder()); return arguments.boolFlag("separate-parent-calls") ? MergeMode.separateParentCalls(methodDisambiguator) : MergeMode.sameMethod(methodDisambiguator); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/AbstractSampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/AbstractSampler.java index 445702ef..8a9c05f3 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/AbstractSampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/AbstractSampler.java @@ -32,6 +32,7 @@ import me.lucko.spark.common.sampler.source.SourceMetadata; import me.lucko.spark.common.sampler.window.ProtoTimeEncoder; import me.lucko.spark.common.sampler.window.WindowStatisticsCollector; +import me.lucko.spark.common.util.classfinder.ClassFinder; import me.lucko.spark.common.ws.ViewerSocket; import me.lucko.spark.proto.SparkProtos; import me.lucko.spark.proto.SparkSamplerProtos.SamplerData; @@ -44,6 +45,7 @@ import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.function.Supplier; /** * Base implementation class for {@link Sampler}s. @@ -230,11 +232,11 @@ protected void writeMetadataToProto(SamplerData.Builder proto, SparkPlatform pla proto.setMetadata(metadata); } - protected void writeDataToProto(SamplerData.Builder proto, DataAggregator dataAggregator, MergeMode mergeMode, ClassSourceLookup classSourceLookup) { + protected void writeDataToProto(SamplerData.Builder proto, DataAggregator dataAggregator, MergeMode mergeMode, ClassSourceLookup classSourceLookup, Supplier classFinderSupplier) { List data = dataAggregator.exportData(); data.sort(Comparator.comparing(ThreadNode::getThreadLabel)); - ClassSourceLookup.Visitor classSourceVisitor = ClassSourceLookup.createVisitor(classSourceLookup); + ClassSourceLookup.Visitor classSourceVisitor = ClassSourceLookup.createVisitor(classSourceLookup, classFinderSupplier); ProtoTimeEncoder timeEncoder = new ProtoTimeEncoder(getMode().valueTransformer(), data); int[] timeWindows = timeEncoder.getKeys(); diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/async/AsyncSampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/async/AsyncSampler.java index e3470b4b..5350558e 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/async/AsyncSampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/async/AsyncSampler.java @@ -221,7 +221,7 @@ public SamplerData toProto(SparkPlatform platform, ExportProps exportProps) { proto.setChannelInfo(exportProps.channelInfo()); } writeMetadataToProto(proto, platform, exportProps.creator(), exportProps.comment(), this.dataAggregator); - writeDataToProto(proto, this.dataAggregator, exportProps.mergeMode().get(), exportProps.classSourceLookup().get()); + writeDataToProto(proto, this.dataAggregator, exportProps.mergeMode().get(), exportProps.classSourceLookup().get(), platform::createClassFinder); return proto.build(); } diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/java/JavaSampler.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/java/JavaSampler.java index bae7e190..e3ae73a0 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/java/JavaSampler.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/java/JavaSampler.java @@ -193,7 +193,7 @@ public SamplerData toProto(SparkPlatform platform, ExportProps exportProps) { proto.setChannelInfo(exportProps.channelInfo()); } writeMetadataToProto(proto, platform, exportProps.creator(), exportProps.comment(), this.dataAggregator); - writeDataToProto(proto, this.dataAggregator, exportProps.mergeMode().get(), exportProps.classSourceLookup().get()); + writeDataToProto(proto, this.dataAggregator, exportProps.mergeMode().get(), exportProps.classSourceLookup().get(), platform::createClassFinder); return proto.build(); } diff --git a/spark-common/src/main/java/me/lucko/spark/common/sampler/source/ClassSourceLookup.java b/spark-common/src/main/java/me/lucko/spark/common/sampler/source/ClassSourceLookup.java index a62f8d14..c856123a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/sampler/source/ClassSourceLookup.java +++ b/spark-common/src/main/java/me/lucko/spark/common/sampler/source/ClassSourceLookup.java @@ -23,7 +23,7 @@ import me.lucko.spark.common.SparkPlatform; import me.lucko.spark.common.sampler.node.StackTraceNode; import me.lucko.spark.common.sampler.node.ThreadNode; -import me.lucko.spark.common.util.ClassFinder; +import me.lucko.spark.common.util.classfinder.ClassFinder; import org.checkerframework.checker.nullness.qual.Nullable; import java.io.IOException; @@ -42,6 +42,7 @@ import java.util.Objects; import java.util.Queue; import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; /** @@ -203,11 +204,11 @@ interface Visitor { Map getLineSourceMapping(); } - static Visitor createVisitor(ClassSourceLookup lookup) { + static Visitor createVisitor(ClassSourceLookup lookup, Supplier classFinderSupplier) { if (lookup == ClassSourceLookup.NO_OP) { return NoOpVisitor.INSTANCE; // don't bother! } - return new VisitorImpl(lookup); + return new VisitorImpl(lookup, classFinderSupplier.get()); } enum NoOpVisitor implements Visitor { @@ -254,14 +255,15 @@ public Map getLineSourceMapping() { */ class VisitorImpl implements Visitor { private final ClassSourceLookup lookup; - private final ClassFinder classFinder = new ClassFinder(); + private final ClassFinder classFinder; private final SourcesMap classSources = new SourcesMap<>(Function.identity()); private final SourcesMap methodSources = new SourcesMap<>(MethodCall::toString); private final SourcesMap lineSources = new SourcesMap<>(MethodCallByLine::toString); - VisitorImpl(ClassSourceLookup lookup) { + VisitorImpl(ClassSourceLookup lookup, ClassFinder classFinder) { this.lookup = lookup; + this.classFinder = classFinder; } @Override diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java b/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java index 2b2e3c78..3b0ddc3a 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/MethodDisambiguator.java @@ -24,6 +24,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ListMultimap; import me.lucko.spark.common.sampler.node.StackTraceNode; +import me.lucko.spark.common.util.classfinder.ClassFinder; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Label; @@ -43,8 +44,13 @@ * to a method (method name + method description). */ public final class MethodDisambiguator { - private final Map cache = new ConcurrentHashMap<>(); - private final ClassFinder classFinder = new ClassFinder(); + private final ClassFinder classFinder; + private final Map cache; + + public MethodDisambiguator(ClassFinder classFinder) { + this.classFinder = classFinder; + this.cache = new ConcurrentHashMap<>(); + } public Optional disambiguate(StackTraceNode element) { String desc = element.getMethodDescription(); diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/ClassFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/ClassFinder.java new file mode 100644 index 00000000..1ee75c66 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/ClassFinder.java @@ -0,0 +1,46 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.classfinder; + +import com.google.common.collect.ImmutableList; +import org.checkerframework.checker.nullness.qual.Nullable; + +public interface ClassFinder { + + /** + * Creates a ClassFinder that combines the results of multiple other finders. + * + * @param finders the other class finders + * @return the combined class finder + */ + static ClassFinder combining(ClassFinder... finders) { + return new CombinedClassFinder(ImmutableList.copyOf(finders)); + } + + /** + * Attempts to find a class by name. + * + * @param className the name of the class + * @return the class, if found + */ + @Nullable Class findClass(String className); + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/CombinedClassFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/CombinedClassFinder.java new file mode 100644 index 00000000..ed63f36c --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/CombinedClassFinder.java @@ -0,0 +1,44 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.classfinder; + +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.util.List; + +class CombinedClassFinder implements ClassFinder { + private final List finders; + + CombinedClassFinder(List finders) { + this.finders = finders; + } + + @Override + public @Nullable Class findClass(String className) { + for (ClassFinder finder : this.finders) { + Class clazz = finder.findClass(className); + if (clazz != null) { + return clazz; + } + } + return null; + } +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/FallbackClassFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/FallbackClassFinder.java new file mode 100644 index 00000000..dd3c9f00 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/FallbackClassFinder.java @@ -0,0 +1,40 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.lucko.spark.common.util.classfinder; + +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Uses {@link Class#forName(String)} to find a class reference for given class names. + */ +public enum FallbackClassFinder implements ClassFinder { + INSTANCE; + + @Override + public @Nullable Class findClass(String className) { + try { + return Class.forName(className); + } catch (Throwable e) { + return null; + } + } + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/ClassFinder.java b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/InstrumentationClassFinder.java similarity index 70% rename from spark-common/src/main/java/me/lucko/spark/common/util/ClassFinder.java rename to spark-common/src/main/java/me/lucko/spark/common/util/classfinder/InstrumentationClassFinder.java index cead9383..5f06d649 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/ClassFinder.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/classfinder/InstrumentationClassFinder.java @@ -18,8 +18,10 @@ * along with this program. If not, see . */ -package me.lucko.spark.common.util; +package me.lucko.spark.common.util.classfinder; +import me.lucko.spark.common.SparkPlugin; +import me.lucko.spark.common.util.JavaVersion; import net.bytebuddy.agent.ByteBuddyAgent; import org.checkerframework.checker.nullness.qual.Nullable; @@ -33,18 +35,18 @@ * *

This is necessary as we don't always have access to the classloader for a given class.

*/ -public class ClassFinder { +public class InstrumentationClassFinder implements ClassFinder { private static boolean warned = false; - private static Instrumentation loadInstrumentation() { + private static Instrumentation loadInstrumentation(SparkPlugin plugin) { Instrumentation instrumentation = null; try { instrumentation = ByteBuddyAgent.install(); if (!warned && JavaVersion.getJavaVersion() >= 21) { warned = true; - SparkStaticLogger.log(Level.INFO, "If you see a warning above that says \"WARNING: A Java agent has been loaded dynamically\", it can be safely ignored."); - SparkStaticLogger.log(Level.INFO, "See here for more information: https://spark.lucko.me/docs/misc/Java-agent-warning"); + plugin.log(Level.INFO, "If you see a warning above that says \"WARNING: A Java agent has been loaded dynamically\", it can be safely ignored."); + plugin.log(Level.INFO, "See here for more information: https://spark.lucko.me/docs/misc/Java-agent-warning"); } } catch (Exception e) { // ignored @@ -54,8 +56,8 @@ private static Instrumentation loadInstrumentation() { private final Map> classes = new HashMap<>(); - public ClassFinder() { - Instrumentation instrumentation = loadInstrumentation(); + public InstrumentationClassFinder(SparkPlugin plugin) { + Instrumentation instrumentation = loadInstrumentation(plugin); if (instrumentation == null) { return; } @@ -66,21 +68,9 @@ public ClassFinder() { } } + @Override public @Nullable Class findClass(String className) { - // try instrumentation - Class clazz = this.classes.get(className); - if (clazz != null) { - return clazz; - } - - // try Class.forName - try { - return Class.forName(className); - } catch (Throwable e) { - // ignore - } - - return null; + return this.classes.get(className); } } 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 bd5385bd..c79c2b04 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 @@ -22,7 +22,7 @@ import com.google.common.collect.ImmutableMap; import me.lucko.spark.common.sampler.source.ClassSourceLookup; -import me.lucko.spark.common.util.ClassFinder; +import me.lucko.spark.common.util.classfinder.ClassFinder; import me.lucko.spark.fabric.smap.MixinUtils; import me.lucko.spark.fabric.smap.SourceMap; import me.lucko.spark.fabric.smap.SourceMapProvider; @@ -41,14 +41,15 @@ import java.util.Map; public class FabricClassSourceLookup extends ClassSourceLookup.ByCodeSource { - - private final ClassFinder classFinder = new ClassFinder(); - private final SourceMapProvider smapProvider = new SourceMapProvider(); - + private final ClassFinder classFinder; + private final SourceMapProvider smapProvider; private final Path modsDirectory; private final Map pathToModMap; - public FabricClassSourceLookup() { + public FabricClassSourceLookup(ClassFinder classFinder) { + this.classFinder = classFinder; + this.smapProvider = new SourceMapProvider(); + FabricLoader loader = FabricLoader.getInstance(); this.modsDirectory = loader.getGameDir().resolve("mods").toAbsolutePath().normalize(); this.pathToModMap = constructPathToModIdMap(loader.getAllMods()); diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java index d1262226..1569bf80 100644 --- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java +++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricSparkPlugin.java @@ -109,7 +109,7 @@ public void log(Level level, String msg) { @Override public ClassSourceLookup createClassSourceLookup() { - return new FabricClassSourceLookup(); + return new FabricClassSourceLookup(createClassFinder()); } @Override