我如何编写自定义代码可能注释?
我正在用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论