课程信息

时间:2013-01-13 09:56:39

标签: java class annotations

我正在为一个更大的项目编写一个系统,不同的类在一个CommandHandler中注册命令。命令是带有要执行的代码的类 我的问题:CommandHandler需要一些关于类,名称,权限和用法的信息。

我已经尝试使用@interfaces,但这总是让我 null 。我应该以其他方式这样做还是可以解决这个问题?

代码:CommandHandler中的寄存器

 public void register(Class<? extends Command> c) {
    CommandInfo info = c.getAnnotation(CommandInfo.class);
    if (info == null) return;

    try {
        commands.put(info.pattern(), c.newInstance());
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

@CommandInfo

@Retention(RetentionPolicy.RUNTIME)
public @interface CommandInfo
 {
/**
 * The actual name of the command. Not really used anywhere.
 */
public String name();

/**
 * A regex pattern that allows minor oddities and alternatives to the
 * command name.
 */
public String pattern();

/**
 * The usage message, i.e. how the command should be used.
 */
public String usage();

/**
 * A description of what the command does.
 */
public String desc();

/**
 * The permission required to execute this command.
 */
public String permission();
}

一个命令:

public class SetPortPoint implements Command
{
    @CommandInfo(
        name = "setportpoint",
        pattern = "setportpoint|spp",
        usage = "/maa setportpoint <arena> <wavenumber>",
        desc = "set a Port point for a Arena at a given Wave",
        permission = "mobarenaaddon.porter.setportpoint"
    )   
    public boolean execute(){
        //The Code to do
    }
}

1 个答案:

答案 0 :(得分:0)

您正在请求类的注释,但您请求的内容是放在方法上的。将注释放在类本身上或枚举其方法(getMethods())并分别查询每个注释。当然,你所做的取决于你想要的。

另请注意,注释不会继承到子类。