Question

I am currently replacing all my standard POJO's to use Lombok for all the boilerplate code.

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.

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?

Était-ce utile?

La solution

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/1905 So let's see :)

2) For a single collection, it seems that @Delegate could do the job:

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

Autres conseils

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 thread showing 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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top