我如何编写自定义代码可能注释?

发布于 2025-02-01 22:10:56 字数 2140 浏览 4 评论 0原文

我正在用Bukkit框架为Minecraft制作插件。要在此处添加自定义事件,我必须进行这样的活动类:

public class CityCreationCommandEvent extends CancellableCommandEvent {

    private static final HandlerList handlers = new HandlerList();

    private final String cityName;

    public CityCreationCommandEvent(CommandSender sender, String name) {
        super(sender);
        this.cityName = name;
    }

    public String getCityName() {
        return this.cityName;
    }

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }
}

此代码包含样板行,必须为每个新事件打印出来:

    private static final HandlerList handlers = new HandlerList();

    public String getCityName() {
        return this.cityName;
    }

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }

我可以使用Lombok添加Getter和Setter,但是这些行将

    private static final HandlerList handlers = new HandlerList();

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }

留在代码中反正。我想编写自己的注释来做类似的事情:

@Event
public class CityCreationCommandEvent extends CancellableCommandEvent {

    @Getter
    private final String cityName;

    public CityCreationCommandEvent(CommandSender sender, String name) {
        super(sender);
        this.cityName = name;
    }

}

甚至是这样(但我认为很难做到)

@Event
public class CityCreationCommandEvent extends CancellableCommandEvent {

    @Value
    private final String cityName;
}

,或者

@Event
@UnmodifiableValue(type = String.class, name = "cityName")
public class CityCreationCommandEvent extends CancellableCommandEvent {}

我有一个想法是由Javac手动执行此操作,但这很难如果我想使用IDE中生成的方法,则必须为IDE编写自己的插件)。

因此,最好的方法是与Lombok集成。 Lombok是否有API可以做这样的事情?或者,如果没有,我如何使用已经存在的注释来做类似的事情?

I'm making a plugin for Minecraft with Bukkit framework. To add custom event there, I have to do my event class like this:

public class CityCreationCommandEvent extends CancellableCommandEvent {

    private static final HandlerList handlers = new HandlerList();

    private final String cityName;

    public CityCreationCommandEvent(CommandSender sender, String name) {
        super(sender);
        this.cityName = name;
    }

    public String getCityName() {
        return this.cityName;
    }

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }
}

And, this code contains boilerplate lines, that have to be printed for every new event:

    private static final HandlerList handlers = new HandlerList();

    public String getCityName() {
        return this.cityName;
    }

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }

I can add getter and setters using lombok, but these lines

    private static final HandlerList handlers = new HandlerList();

    @NotNull
    public HandlerList getHandlers() {
        return handlers;
    }

    @NotNull
    public static HandlerList getHandlerList() {
        return handlers;
    }

will stay in code anyway. I want to write my own annotation to do something like this:

@Event
public class CityCreationCommandEvent extends CancellableCommandEvent {

    @Getter
    private final String cityName;

    public CityCreationCommandEvent(CommandSender sender, String name) {
        super(sender);
        this.cityName = name;
    }

}

or even this (but I think it will be hard to make)

@Event
public class CityCreationCommandEvent extends CancellableCommandEvent {

    @Value
    private final String cityName;
}

or

@Event
@UnmodifiableValue(type = String.class, name = "cityName")
public class CityCreationCommandEvent extends CancellableCommandEvent {}

I have an idea to do this manually by javac, but it quite difficult (keeping in mind the fact that if I want to use the generated methods in the ide, I will have to write my own plugin for the ide).

So, the best way is to integrate with lombok. Does lombok have an API to do something like this? Or, if not, how I can do something like this using already existing annotations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文