1

私はより大きなプロジェクトのために、異なるクラスが1つの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();
}

そして1つのコマンド:

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
    }
}
4

1 に答える 1

0

クラスでアノテーションをリクエストしていますが、リクエストしたものはメソッドに配置されます。クラス自体にアノテーションを付けるか、そのメソッド(getMethods())を列挙して、それぞれのアノテーションを個別にクエリします。もちろん、あなたが何をするかはあなたが望むものに依存します。

また、アノテーションはサブクラスに継承されないことに注意してください。

于 2013-01-13T11:33:50.043 に答える