简体   繁体   English

如何做注释,将所有该类型的类添加到列表中

[英]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.

相关问题 如何找到带有特定注释的所有Java对象(而非类)? - How do I find all Java objects (not classes) with a particular annotation? Java - 如何创建仅适用于类型上下文的注释类型? (PURE 类型注解) - Java - How do I create an annotation type that is only applicable in type contexts? (PURE type annotation) 如何不允许对所有类使用我自己的注释,而不是对实现具体接口的类进行使用 - How can I disallow to use my own annotation for all classes instead of classes implementing concrete interface 我在Bluej中有2个类,并且想要使其中一个类中的字段成为另一类的类型。 我该怎么做? - I have 2 classes in Bluej and want to make a field in one of the classes be of type of the other class. How would I do this? 如何使用它们的超类型对具体类进行通用 @OneToMany 映射? - How do I make a generic @OneToMany mapping of concrete classes using their super type? 如何制作具有对所有方法和类均可用的imageIcon的JLabel? - How do I make a JLabel that has an imageIcon that is available to all methods and classes? 如何制作一个基于测试类列表有条件地运行测试的JUnit测试套件? - How do I make a JUnit Test Suite that runs tests conditionally based on a list of Test Classes? 如何获取eclipse中Java项目中使用的所有类和方法的列表? - How do I get the list of all the classes and methods used in a Java project in eclipse? 注释配置的框架(即Tomcat,Hibernate)如何扫描类路径中的所有类以进行注释? - How do annotation-configured frameworks (ie. Tomcat, Hibernate) scan all classes in the classpath for annotations? 如何获得所有由Cayenne管理的实体类的列表? - How do I get a list of all Cayenne-managed entity classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM