Skip to content

Commit

Permalink
#62: Fix #61, Plugin now always use SimplePluginClassLoader
Browse files Browse the repository at this point in the history
--------
Reviewed-by: ZX夏夜之风 <[email protected]>
Tested-by: huanmeng_qwq <[email protected]>
  • Loading branch information
huanmeng-qwq authored Dec 3, 2023
1 parent 91f5961 commit ee8f456
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
27 changes: 19 additions & 8 deletions src/main/java/snw/kookbc/impl/plugin/SimplePluginClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import snw.jkook.Core;
import snw.jkook.config.serialization.ConfigurationSerializable;
import snw.jkook.config.serialization.ConfigurationSerialization;
import snw.jkook.plugin.Plugin;
import snw.jkook.plugin.PluginClassLoader;
import snw.jkook.plugin.PluginDescription;
Expand Down Expand Up @@ -95,9 +97,8 @@ protected Class<? extends Plugin> lookForMainClass(String mainClassName, File fi
if (this.findLoadedClass(mainClassName) != null) {
throw new IllegalArgumentException("The main class defined in plugin.yml has already been defined in the VM.");
} else {
if (parentClassLoader == null) {
super.addURL(file.toURI().toURL());
} else {
super.addURL(file.toURI().toURL());
if (parentClassLoader != null) {
parentClassLoader.addURL(file.toURI().toURL());
}
Class<?> loadClass = this.loadClass(mainClassName, true);
Expand All @@ -112,10 +113,7 @@ protected Class<? extends Plugin> lookForMainClass(String mainClassName, File fi

@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (parentClassLoader == null) {
return super.loadClass(name, resolve);
}
return parentClassLoader.loadClass(name, resolve);
return super.loadClass(name, resolve);
}

@Override
Expand All @@ -128,7 +126,7 @@ public final Class<?> findClass0(String name, boolean dontCallOther) throws Clas
return cache.get(name);
}
try {
Class<?> result = parentClassLoader == null ? super.findClass(name) : parentClassLoader.findClass(name);
Class<?> result = super.findClass(name);
if (result != null) {
cache.put(name, result);
return result;
Expand All @@ -148,6 +146,13 @@ public final Class<?> findClass0(String name, boolean dontCallOther) throws Clas
}

protected Class<?> loadFromOther(String name) throws ClassNotFoundException {
try {
Class<?> clazz = parentClassLoader.findClass(name);
if (clazz != null) {
return clazz;
}
} catch (ClassNotFoundException ignored) {
}
for (SimplePluginClassLoader classLoader : INSTANCES) {
if (classLoader == null) {
// Suggested by ChatGPT:
Expand All @@ -174,6 +179,12 @@ protected Class<?> loadFromOther(String name) throws ClassNotFoundException {
@Override
public void close() throws IOException {
INSTANCES.remove(this);
for (Class<?> clazz : cache.values()) {
if (ConfigurationSerializable.class.isAssignableFrom(clazz)) {
//noinspection unchecked
ConfigurationSerialization.unregisterClass((Class<? extends ConfigurationSerializable>) clazz);
}
}
super.close();
}
}
14 changes: 7 additions & 7 deletions src/main/java/snw/kookbc/impl/plugin/SimplePluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ public void disablePlugin(Plugin plugin) {
} catch (Throwable e) {
plugin.getLogger().error("Exception occurred when we are disabling this plugin", e);
}
// if (plugin.getClass().getClassLoader() instanceof SimplePluginClassLoader) {
// try {
// ((SimplePluginClassLoader) plugin.getClass().getClassLoader()).close();
// } catch (IOException e) {
// logger.error("Unexpected IOException while we're attempting to close the PluginClassLoader.", e);
// }
// }
if (plugin.getClass().getClassLoader() instanceof SimplePluginClassLoader) {
try {
((SimplePluginClassLoader) plugin.getClass().getClassLoader()).close();
} catch (IOException e) {
logger.error("Unexpected IOException while we're attempting to close the PluginClassLoader.", e);
}
}
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/snw/kookbc/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static Launcher instance() {
return launcher;
}

public void onSetup() {
public final void onSetup() {
if (instance() != this) {
return;
}
Expand Down

0 comments on commit ee8f456

Please sign in to comment.