From f945b52fa4df55ea6bac422697cf81b09b482667 Mon Sep 17 00:00:00 2001 From: LamGC Date: Sun, 21 Feb 2021 20:31:47 +0800 Subject: [PATCH] =?UTF-8?q?[Change]=20Framework-API=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=20Framework=20=E5=86=85=E5=AD=98=E5=82=A8=20PluginWrapper=20?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E5=BC=8F,=20=E4=BB=A5=E9=98=B2=20Plugin=20?= =?UTF-8?q?=E5=86=85=E7=9A=84=20wrapper=20=E9=81=AD=E5=88=B0=E6=9B=B4?= =?UTF-8?q?=E6=94=B9;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [Change] Framework 添加 _wrapper 字段, 用于保存 PluginWrapper 并防止 Plugin 内的 wrapper 遭到更改(但是依然没办法防止 'getWrapper()' 返回错误的 PluginWrapper); --- .../java/net/lamgc/cgj/bot/framework/Framework.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java index 7ec0aa3..15dc2c1 100644 --- a/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java +++ b/ContentGrabbingJi-framework-api/src/main/java/net/lamgc/cgj/bot/framework/Framework.java @@ -32,6 +32,11 @@ public abstract class Framework extends Plugin { private final File dataFolder; private final FrameworkContext context; + /** + * 这个变量是防止原 {@link Plugin#wrapper} 遭到修改而存在. + *

很迷惑为什么 {@link Plugin#getWrapper()} 加了 {@code final} 关键字而 {@link Plugin#wrapper} 却不添加 {@code final} 关键字. + */ + private final PluginWrapper _wrapper; /** * 由 FrameworkManager 执行的构造方法. @@ -43,6 +48,7 @@ public abstract class Framework extends Plugin { */ public Framework(PluginWrapper wrapper, File dataFolder, FrameworkContext context) { super(wrapper); + this._wrapper = wrapper; this.context = context; if (!(wrapper.getDescriptor() instanceof FrameworkDescriptor)) { throw new IllegalStateException("Invalid description object"); @@ -77,11 +83,12 @@ public abstract class Framework extends Plugin { * @return 返回框架描述对象. */ public final FrameworkDescriptor getDescriptor() { - PluginDescriptor descriptor = getWrapper().getDescriptor(); + PluginDescriptor descriptor = _wrapper.getDescriptor(); if (descriptor instanceof FrameworkDescriptor) { return (FrameworkDescriptor) descriptor; } - throw new ClassCastException("无法转换 Descriptor 的类型, 框架管理器可能遭到修改!"); + throw new ClassCastException("无法转换 Descriptor 的类型, " + + "可能是第三方 FrameworkManager 并未传入 FrameworkDescriptor 或 FrameworkManager 遭到修改!"); } /** @@ -111,7 +118,7 @@ public abstract class Framework extends Plugin { * @return 返回资源输入流, 如果资源不存在时返回 {@code null}. */ protected final InputStream getFrameworkResourceAsStream(String name) { - return getWrapper().getPluginClassLoader().getResourceAsStream(name); + return _wrapper.getPluginClassLoader().getResourceAsStream(name); } }