diff options
author | Luck <git@lucko.me> | 2021-11-14 19:10:46 +0000 |
---|---|---|
committer | Luck <git@lucko.me> | 2021-11-14 19:10:46 +0000 |
commit | 49fc0ba47fac077a2fe965c44fd382a1ab02089c (patch) | |
tree | 2261bfdb4fd50b56a51fc1386eb4ac77d2fee939 /spark-velocity4/src/main/java/me/lucko | |
parent | eed8b8d38260fb16f114ab856e486904ae6dc937 (diff) | |
download | spark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.tar.gz spark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.tar.bz2 spark-49fc0ba47fac077a2fe965c44fd382a1ab02089c.zip |
Improve Velocity ClassSourceLookup
Diffstat (limited to 'spark-velocity4/src/main/java/me/lucko')
-rw-r--r-- | spark-velocity4/src/main/java/me/lucko/spark/velocity/Velocity4ClassSourceLookup.java | 20 |
1 files changed, 12 insertions, 8 deletions
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; } |