Java 使用 lombok 为列表添加方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21776729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add method for lists with lombok
提问by KristofMols
I am currently replacing all my standard POJO's to use Lombok for all the boilerplate code.
我目前正在替换我所有的标准 POJO,以将 Lombok 用于所有样板代码。
So far no problems have occurred, but what i'm missing in the lombok implementation is that there are no generated methods for adding one object to a collection.
到目前为止还没有发生任何问题,但是我在 lombok 实现中缺少的是没有生成的方法可以将一个对象添加到集合中。
Generated Code:
生成的代码:
private List<Object> list = new ArrayList<>();
public Object getObject(){..}
public void setObject(List<Object> o){..}
What I want extra:
我想要额外的东西:
public void addObject(Object o) {..}
Anyone know if this is getting there soon or if this is impossible?
有谁知道这是否很快就会实现,或者这是不可能的?
采纳答案by Ondra ?i?ka
1) I couldn't find a ticket for it, and, based on the comment on the other answer, I filed one: https://github.com/rzwitserloot/lombok/issues/1905So let's see :)
1)我找不到票,根据对另一个答案的评论,我提交了一个:https: //github.com/rzwitserloot/lombok/issues/1905所以让我们看看:)
2) For a single collection, it seems that @Delegate
could do the job:
2)对于单个集合,似乎@Delegate
可以完成这项工作:
interface CollectionAdders<E> {
boolean add(E e);
boolean addAll(Collection<? extends E> c);
}
interface ListGetters<E> {
E get(int index);
}
class Foo {
@Delegate(types={CollectionAdders.class, ListGetters.class})
List<String> names = new ArrayList<>();
}
Generates:
产生:
Foo#add(E e)
Foo#addAll(Collection<? extends E> c)
Foo#get(int index)
See this forum post: https://groups.google.com/forum/#!topic/project-lombok/alektPraJ_Q
请参阅此论坛帖子:https: //groups.google.com/forum/#!topic/project-lombok/ alektPraJ_Q
回答by maaartinus
This is surely currently impossible. There's such a proposal, but low priority (or even rejected).
这在目前肯定是不可能的。有这样一个提议,但优先级很低(甚至被拒绝)。
Actually, I can't find it anymore. You may want to try yourself on the issue list.
事实上,我再也找不到了。您可能想在问题列表中尝试一下。
Now, I stumbled upon this threadshowing an interesting workaround limited to a single variable.
现在,我偶然发现了这个线程,它展示了一个仅限于单个变量的有趣解决方法。
Bad news
坏消息
This gets improbably implemented in the near future. There are far too many feature requests to implement and maintain them all (or any non-trivial fraction of them). See this issue comment.
这在不久的将来不可能实现。有太多的功能请求需要实现和维护它们(或它们的任何重要部分)。请参阅此问题评论。