It's possible to use generic field info on @Delegate?

316 views
Skip to first unread message

joseaio

unread,
Aug 6, 2012, 10:31:53 AM8/6/12
to project...@googlegroups.com
One example,

   @Delegate
(types=Collection.class)
   private final Collection<String> collection = new ArrayList<String>();

On parse/compilation time access to declaration of "collection" field to gets their type
(Collection<String>) and use (generic) to generate methods with this generic

  public void add(String s)
  public void add(Collection<String> s)
  ...

It's possible?
 

Reinier Zwitserloot

unread,
Aug 11, 2012, 12:22:31 PM8/11/12
to project...@googlegroups.com
Possible-ish.

Two notes:

A) In this case, you don't need it; if there's no 'types=' then the type of the field is taken, and this is Collection<String>, which is exactly what you want. For this snippet, just go with "@Delegate" without any further parameters.

B) The delegate type does not have to match the field type. You can just make a private inner interface with each method you want delegated. For example:

private interface StringCollection implements Collection<String>() {}

@Delegate(types=StringCollection.class) List<String> list = new ArrayList<String>();

Reply all
Reply to author
Forward
0 new messages