我做Bukkit插件项目已经有一段时间了,我最终决定发布一个正式的版本。因此,在我疯狂地记录我的项目的所有内容时,我决定为那些想要使用它们的人清理一些api。现在,我碰到了一个问题。
基本上,在这个API中,你必须实现一个接口,然后用一个类注册你的实现,这个类管理每个实现,并检查/注册另一个变量与插件内捆绑的PluginMetrics。
下面是我的界面,"name"one_answers"tracker"字段是关注的字段:
public interface IAction {
/**
* Name of the action.
*/
String name = null;
/**
* Stores args passed to the action by the player.
*/
String[] args = null;
/**
* Holds the Metrics tracker object.
*/
Tracker tracker = null;
...
}
并且,这里是方法(和init()方法),我所有的问题都出现了:
public static void registerAction(final Class a) {
try {
store.put((String) a.getField("name").get(a), a);
log.exDebug(String.format("Action %s (%s) was registered.", (String) a.getField("name").get(a), a.getCanonicalName()));
try {
Tracker metric = (Tracker) a.getField("tracker").get(a);
try {
OpenAuth.getMetrics().addCustomData(metric);
log.exDebug(String.format("Registered Metrics data tracker [%s] from %s.", metric.getColumnName(), a.getCanonicalName()));
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering Action data tracker.");
}
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering Action data tracker.");
// e.printStackTrace();
}
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering an Action.");
e.printStackTrace();
}
}
public static void init() {
for (Actions a : Actions.values()) {
try {
registerAction(a.getAction());
} catch (java.lang.Exception e) {
log.info("Exception occurred while initialising Actions enumerator.");
e.printStackTrace();
}
}
for (Object ob : classLoader.load().getClasses()) {
Class c = null;
try {
c = (Class) ob;
} catch (java.lang.Exception e) {
log.info("Exception occurred while casting up external Action.");
e.printStackTrace();
}
try {
registerAction(c);
} catch (java.lang.Exception e) {
log.info("Exception occurred while registering external Action.");
e.printStackTrace();
}
}
}
和一个示例实现:
public class FreezeStick implements IAction {
public static final String name = "freeze";
public static final Tracker tracker = new Tracker("FreezeStick");
private String[] args = null;
private Session attached;
private SessionController sc;
private final LogHandler log = new LogHandler();
private final String permissible = "openauth.action.freeze";
private OAServer server;
private boolean used = false;
protected OAPlayer sender;
protected OAPlayer target;
public FreezeStick(OAServer server, Session attached) {
this.server = server;
this.sc = server.getController().getSessionController();
this.attached = attached;
this.setSender(this.attached.getPlayer());
}
-- etc.. --
每次Tracker metric = (Tracker) a.getDeclaredField("tracker").get(a);
发生时,它是报告为null
。我试过用a.t getfield()而不是a.t getdeclaredfield(),但同样的问题。名称和跟踪器变量在实现中声明时都接收private, static, and final
,并且在名称变量上使用a.getField()自从我第一次写这篇文章以来一直工作得很好。
如果有人能给我指出正确的方向,我将不胜感激:)
还有,如果它能帮助你更好地理解我的问题,这里有一个例外:
2013-01-02 10:48:10 [INFO] [OpenAuth-debug] Action ban (me.maiome.openauth.actions.BanStick) was registered.
2013-01-02 10:48:10 [INFO] [OpenAuth] Exception occurred while adding Action data tracker.
2013-01-02 10:48:10 [SEVERE] java.lang.NullPointerException
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.actions.Actions.registerAction(Actions.java:65)
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.actions.Actions.init(Actions.java:104)
2013-01-02 10:48:10 [SEVERE] at me.maiome.openauth.bukkit.OpenAuth.onEnable(OpenAuth.java:230)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.loadPlugin(CraftServer.java:278)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.enablePlugins(CraftServer.java:260)
2013-01-02 10:48:10 [SEVERE] at org.bukkit.craftbukkit.v1_4_6.CraftServer.<init>(CraftServer.java:214)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.PlayerList.<init>(PlayerList.java:52)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedPlayerList.<init>(SourceFile:11)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.DedicatedServer.init(DedicatedServer.java:104)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.MinecraftServer.run(MinecraftServer.java:399)
2013-01-02 10:48:10 [SEVERE] at net.minecraft.server.v1_4_6.ThreadServerApplication.run(SourceFile:849)
接口不能声明字段,这是实际的代码吗?
您面临的问题是getDeclaredField
方法将获得具有给定名称的字段,但不会搜索超类。为了回答问题的反射部分,我建议您创建一个助手方法,该方法将不断调用Class对象(以及该对象的超类等)上的.getSuperclass()
,直到您到达声明该字段的类。(编辑,以匹配给定的示例:要获取静态字段,您应该使用field.get(null);
。)
Tracker getTracker();
和String getName();
。
如果你使用的是结构化为bean的类对象,那么你应该看看apache-commons中的BeanUtils或spring中的BeanWrapper(名称不是100%确定)。在幕后,它们为你处理低级的反射和自省,所以你所要做的就是写一个简单的方法调用来检索一个值。
看一下registerAction,你只是试着转换传入的类对象,这可能会导致一些头痛的问题。
我认为你可以使用泛型重写,强制一个参数是一个Tracker类对象
public static void registerAction(final Class<Tracker> a) {
您还可以添加一些检查,以查看类是否包含方法或字段,然后再尝试使用它们。
我找到了答案。事实证明,我试图在Metrics实例准备使用之前在Metrics实例上运行#addCustomData()。只需要将Actions.init()往下移动几行:)