On Saturday, November 17, 2012 4:52:24 AM UTC+1, Reinier Zwitserloot wrote:
Is there a way we can do this with a 'proxy' - a field / method / type that you make JUST so you can shove the annotations on there, so that you can then reference that and say: That stuff? I guess it would be far too much boilerplate to create a dummy method just as a place to dump annotations.
Probably yes, unless the annotation is extremely complicated. This thread started with `@Inject`, which can be specified simply like
@AllArgsConstructor(annotateWith="Inject")
class C {
...
}
and the placeholder idea could look like
@AllArgsConstructor
class C {
@Inject
private lombok.ConstructorAnnotationPlaceholder placeholder;
...
}
i.e. simply terrible (moreover, it doesn't work for annotations which are allowed on constructors only).
The second most important Guice annotation is probably `@Named("someName")`. You may want to generate
class FileHelper {
@Inject
FileHelper(@Named("from") File from, @Named("to") File to) {
this.from = from;
}
private final File from;
private final File to;
...
}
The most natural way is
@AllArgsConstructor(annotateWith="Inject", moveAnnotations={"Named"})
class FileHelper {
@Named("from") private final File from;
@Named("to") private final File to;
}
which would work as `@Named` is allowed on fields too. If it wasn't, something like
@AllArgsConstructor(annotateWith="Inject")
class FileHelper {
@AnnotateConstructorArgumentWith(value=Named.class, args="\"from\"") private final File from;
@AnnotateConstructorArgumentWith(value=Named.class, args="\"to\"") private final File to;
}
would be acceptable (1). As a template, you'd need something like
@AllArgsConstructor(annotateWith="Inject")
class FileHelper {
private lombok.ConstructorArgumentAnnotationPlaceholder placeholder(@Named("from") File from, @Named("to") File to) {}
private final File from;
private final File to;
}
which is way worse than writing the constructor manually. I can imagine that the placeholder idea can work well somewhere, but I'm afraid I've just demonstrated it to be unusable in these two important cases.
___
(1) I was assuming a simplified notation in case there's only the "value" argument; otherwise something like
args="value=\"from\""
or in the general case
args={"min=1", "max=10", "name=\"abc\""}
would be needed. Not very nice.