[英]How do I make an annotation, that adds all classes of that type to a list
Ok, so i have a kind of command manager for one of my programs. 好的,所以我为我的一个程序提供了一种命令管理器。 Theres a abstract baceclass called Command which is, really simple 有一个称为Command的抽象baceclass,它非常简单
public abstract class Command {
protected String commandheader;
protected int requiredlevel;
protected Random rand;
public Command(RANK rank,String command)
{
commandheader = command;
requiredlevel = rank.level;
}
}
Then in each of the classes that inherit this, i just so some oop magic. 然后,在每个继承此类的类中,我都说了一些神奇的东西。
public class MyCommand extends Command {
public MyCommand()
{
super(RANK.PLAYER,"blablabla");
}
}
Then i also have a command helper class, which keeps each of these commands in a list so i can easily find if the command, is valid when i pass it in, aswell as get a lsit of all commands that are avalable. 然后,我还有一个命令帮助程序类,该类将这些命令中的每一个都保存在列表中,这样我可以轻松查找该命令在传递时是否有效,以及获得所有可用命令的简短信息。
public class CommandHelper {
public enum RANK{
PLAYER(0);
public int level;
private RANK(int i)
{
level = i;
}
}
static List<Command> commandlist;
private static void initTheCommands()
{
//Add the commands to the list here.
commandlist.add(new MyCommand());
}
//Called by my main class
public static void Init()
{
if(commandlist == null)
{
//Were safe to initalise the stuff brah.
commandlist = new ArrayList<Command>();
initTheCommands();
for(Command cmd : commandlist)
{
System.out.println("Loaded command: " + cmd.commandheader);
}
System.out.println("[INFO] Initalised the command helper");
}
else
{
System.out.println("[INFO] Command list is already populated.");
}
}
}
As of right now, this system works completely fine. 截至目前,该系统完全可以正常工作。 But it has a flaw, for each command i or the other editors add, we have to manually add it to the list, and that seems tedious and can lead to problems as we sync files. 但这有一个缺陷,对于我或其他编辑者添加的每个命令,我们都必须手动将其添加到列表中,这似乎很繁琐,并且在我们同步文件时可能会导致问题。 So i was wondering, is there any way i can add each command to the list without having to manually put it there? 所以我想知道,有什么方法可以将每个命令添加到列表中而不必手动将其添加到列表中? Perhaps annotate my method, or something to just add it to the list? 也许注释我的方法,或者只是将其添加到列表中? I seen something about reflection, but i don't think that's what i want exactly although im not sure about it. 我看到了一些有关反射的内容,但我不确定我到底想要的是什么,尽管我不确定。 Iv never used nor made annotations before so im not sure weather or not this is plausible. iv以前从未使用过或未做过注释,因此不确定天气是否合理。
If thats what you really want to do you can do something like this... 如果那是您真正想要做的,则可以执行以下操作...
Declare your annotation 声明您的注释
@Target (ElementType.TYPE)
@Retention (RetentionPolicy.RUNTIME)
public @interface CommandAnnotation {
}
Annotate your commands 注释您的命令
@CommandAnnotation
public class MyCommand {
Then check for them something like this 然后检查他们像这样
...
import org.reflections.Reflections;
...
public void loadCommands() {
Reflections reflections = new Reflections("com.my.package");
Set<Class<?>> allClasses = reflections.getSubTypesOf(Command.class);
for (Class<?> outerClazz : allClasses) {
CommandAnnotation annotation = outerClazz.getAnnotation(CommandAnnotation.class);
if (annotation == null)
continue;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.