mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Fix] Core, Framework-API 将 Framework 中 'initial()' 的调用转移到 FrameworkFactory 中, 以解决类初始化问题;
[Change] Framework 移除 'initial()' 的调用, 以修复类初始化过程中的一些隐式问题(如因父类尚未初始化完成, 导致子类成员变量尚未初始化, 出现 NPE 的问题); [Change] FrameworkFactory 将 Framework 的初始化转移到此, 以修复类初始化过程中的一些隐式问题(如上);
This commit is contained in:
parent
02bbab8e6d
commit
a4340ab575
@ -65,19 +65,29 @@ final class FrameworkFactory implements PluginFactory {
|
|||||||
|
|
||||||
// 如果成功获取类, 就需要对其检查, 以确保类符合框架主类的要求.
|
// 如果成功获取类, 就需要对其检查, 以确保类符合框架主类的要求.
|
||||||
int modifiers = pluginClass.getModifiers();
|
int modifiers = pluginClass.getModifiers();
|
||||||
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)
|
if (Modifier.isAbstract(modifiers) || Modifier.isInterface(modifiers)) {
|
||||||
|| (!Framework.class.isAssignableFrom(pluginClass))) {
|
log.error("The framework class '{}' is not valid: Classes are abstract, or interfaces", pluginClassName);
|
||||||
log.error("The framework class '{}' is not valid", pluginClassName);
|
return null;
|
||||||
|
} else if (!Framework.class.isAssignableFrom(pluginClass)) {
|
||||||
|
log.error("The framework class '{}' is not valid: Class is not a subclass of the Framework",
|
||||||
|
pluginClassName);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// <init>(PluginWrapper, DataFolder)
|
// <init>(PluginWrapper, DataFolder)
|
||||||
Constructor<?> constructor = pluginClass
|
Class<? extends Framework> frameworkClass = pluginClass.asSubclass(Framework.class);
|
||||||
|
Constructor<?> constructor = frameworkClass
|
||||||
.getConstructor(PluginWrapper.class, File.class, FrameworkContext.class);
|
.getConstructor(PluginWrapper.class, File.class, FrameworkContext.class);
|
||||||
return (Framework) constructor.newInstance(pluginWrapper,
|
Framework instance = (Framework) constructor.newInstance(pluginWrapper,
|
||||||
new File(dataRootFolder, pluginWrapper.getPluginId()),
|
new File(dataRootFolder, pluginWrapper.getPluginId()),
|
||||||
new DefaultFrameworkContext(eventExecutor, cacheStoreBuilder));
|
new DefaultFrameworkContext(eventExecutor, cacheStoreBuilder));
|
||||||
|
try {
|
||||||
|
instance.initial();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
throw new IllegalStateException("An exception occurred while initializing the framework", e);
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e.getMessage(), e);
|
log.error(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ package net.lamgc.cgj.bot.framework;
|
|||||||
|
|
||||||
import org.pf4j.Plugin;
|
import org.pf4j.Plugin;
|
||||||
import org.pf4j.PluginDescriptor;
|
import org.pf4j.PluginDescriptor;
|
||||||
import org.pf4j.PluginState;
|
|
||||||
import org.pf4j.PluginWrapper;
|
import org.pf4j.PluginWrapper;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -46,18 +45,12 @@ public abstract class Framework extends Plugin {
|
|||||||
throw new IllegalStateException("Invalid description object");
|
throw new IllegalStateException("Invalid description object");
|
||||||
}
|
}
|
||||||
this.dataFolder = dataFolder;
|
this.dataFolder = dataFolder;
|
||||||
try {
|
|
||||||
initial();
|
|
||||||
} catch (Throwable e) {
|
|
||||||
wrapper.setFailedException(e);
|
|
||||||
wrapper.setPluginState(PluginState.FAILED);
|
|
||||||
log.error("An exception occurred while initializing the framework", e);
|
|
||||||
throw new IllegalStateException("An exception occurred while initializing the framework", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行初始化操作.
|
* 执行初始化操作.
|
||||||
|
* <p> 警告: 请不要在初始化过程中调用 {@link org.pf4j.PluginManager}
|
||||||
|
* 的任何插件管理方法, 这将会导致加载错误.
|
||||||
*/
|
*/
|
||||||
protected abstract void initial();
|
protected abstract void initial();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user