Skip to content

Commit 7bbb588

Browse files
committed
fix(gui): don't init already loaded plugins while collecting options (#2727)
1 parent 8629e4c commit 7bbb588

2 files changed

Lines changed: 26 additions & 29 deletions

File tree

jadx-gui/src/main/java/jadx/gui/utils/plugins/CollectPlugins.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package jadx.gui.utils.plugins;
22

33
import java.util.ArrayList;
4+
import java.util.Optional;
45
import java.util.SortedSet;
5-
import java.util.TreeSet;
66

77
import jadx.api.JadxArgs;
88
import jadx.api.JadxDecompiler;
@@ -27,12 +27,13 @@ public CollectPlugins(MainWindow mainWindow) {
2727
}
2828

2929
public CloseablePlugins build() {
30-
SortedSet<PluginContext> allPlugins = new TreeSet<>();
31-
mainWindow.getWrapper().getCurrentDecompiler()
32-
.ifPresent(decompiler -> allPlugins.addAll(decompiler.getPluginManager().getResolvedPluginContexts()));
33-
34-
// collect and init not loaded plugins in new temp context
35-
Runnable closeable = null;
30+
Optional<JadxDecompiler> currentDecompiler = mainWindow.getWrapper().getCurrentDecompiler();
31+
if (currentDecompiler.isPresent()) {
32+
JadxDecompiler decompiler = currentDecompiler.get();
33+
SortedSet<PluginContext> plugins = decompiler.getPluginManager().getResolvedPluginContexts();
34+
return new CloseablePlugins(new ArrayList<>(plugins), null);
35+
}
36+
// collect and init plugins in new temp context
3637
JadxArgs jadxArgs = mainWindow.getSettings().toJadxArgs();
3738
jadxArgs.setFilesGetter(JadxFilesGetter.INSTANCE);
3839
try (JadxDecompiler decompiler = new JadxDecompiler(jadxArgs)) {
@@ -44,18 +45,10 @@ public CloseablePlugins build() {
4445
pluginContext.setAppContext(appContext);
4546
});
4647
pluginManager.load(new JadxExternalPluginsLoader());
47-
SortedSet<PluginContext> missingPlugins = new TreeSet<>();
48-
for (PluginContext context : pluginManager.getAllPluginContexts()) {
49-
if (!allPlugins.contains(context)) {
50-
missingPlugins.add(context);
51-
}
52-
}
53-
if (!missingPlugins.isEmpty()) {
54-
pluginManager.init(missingPlugins);
55-
allPlugins.addAll(missingPlugins);
56-
closeable = () -> pluginManager.unload(missingPlugins);
57-
}
48+
SortedSet<PluginContext> allPlugins = pluginManager.getAllPluginContexts();
49+
pluginManager.init(allPlugins);
50+
Runnable closeable = () -> pluginManager.unload(allPlugins);
51+
return new CloseablePlugins(new ArrayList<>(allPlugins), closeable);
5852
}
59-
return new CloseablePlugins(new ArrayList<>(allPlugins), closeable);
6053
}
6154
}

jadx-plugins-tools/src/main/java/jadx/plugins/tools/JadxExternalPluginsLoader.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class JadxExternalPluginsLoader implements JadxPluginLoader {
3131
public List<JadxPlugin> load() {
3232
close();
3333
long start = System.currentTimeMillis();
34-
Map<Class<? extends JadxPlugin>, JadxPlugin> map = new HashMap<>();
34+
Map<String, JadxPlugin> map = new HashMap<>();
3535
loadFromClsLoader(map, thisClassLoader());
3636
loadInstalledPlugins(map);
3737

@@ -45,7 +45,7 @@ public List<JadxPlugin> load() {
4545
}
4646

4747
public JadxPlugin loadFromJar(Path jar) {
48-
Map<Class<? extends JadxPlugin>, JadxPlugin> map = new HashMap<>();
48+
Map<String, JadxPlugin> map = new HashMap<>();
4949
loadFromJar(map, jar);
5050
int loaded = map.size();
5151
if (loaded == 0) {
@@ -59,22 +59,26 @@ public JadxPlugin loadFromJar(Path jar) {
5959

6060
}
6161

62-
private void loadFromClsLoader(Map<Class<? extends JadxPlugin>, JadxPlugin> map, ClassLoader classLoader) {
63-
ServiceLoader.load(JadxPlugin.class, classLoader)
64-
.stream()
65-
.filter(p -> p.type().getClassLoader() == classLoader)
66-
.filter(p -> !map.containsKey(p.type()))
67-
.forEach(p -> map.put(p.type(), p.get()));
62+
private void loadFromClsLoader(Map<String, JadxPlugin> map, ClassLoader classLoader) {
63+
ServiceLoader<JadxPlugin> serviceLoader = ServiceLoader.load(JadxPlugin.class, classLoader);
64+
for (ServiceLoader.Provider<JadxPlugin> provider : serviceLoader.stream().collect(Collectors.toList())) {
65+
Class<? extends JadxPlugin> pluginClass = provider.type();
66+
String clsName = pluginClass.getName();
67+
if (!map.containsKey(clsName)
68+
&& pluginClass.getClassLoader() == classLoader) {
69+
map.put(clsName, provider.get());
70+
}
71+
}
6872
}
6973

70-
private void loadInstalledPlugins(Map<Class<? extends JadxPlugin>, JadxPlugin> map) {
74+
private void loadInstalledPlugins(Map<String, JadxPlugin> map) {
7175
List<Path> jars = JadxPluginsTools.getInstance().getEnabledPluginJars();
7276
for (Path jar : jars) {
7377
loadFromJar(map, jar);
7478
}
7579
}
7680

77-
private void loadFromJar(Map<Class<? extends JadxPlugin>, JadxPlugin> map, Path jar) {
81+
private void loadFromJar(Map<String, JadxPlugin> map, Path jar) {
7882
try {
7983
File jarFile = jar.toFile();
8084
String clsLoaderName = JADX_PLUGIN_CLASSLOADER_PREFIX + jarFile.getName();

0 commit comments

Comments
 (0)