Guice Assisted Injection with custom Annotations

839 views Asked by At

At the moment I use Assisted Injection with Named Parameters like this:

public interface Factory {
public MyObject create(@Assisted("string1") String string1, @Assisted("string2") String string2);
}

This works great. But I think it is a bit ugly to use Strings as Identifier for my parameters. What I would want to do is the following:

public interface Factory {
public MyObject create(@String1 String string1, @String2 String string2);
}

So essentially I want custom Assisted Annotations. Is there a way to do this?

1

There are 1 answers

0
Andrew McNamee On BEST ANSWER

To my knowledge, that style of custom @Assisted annotations doesn't exist as a feature; the only way to differentiate assisted-inject parameters of the same type is, as you said, to use named @Assisted annotations.

What you propose might be implementable, so you could always file a feature request on the Guice site[1] if you think it'd be worthwhile.

My gut instinct is that there wouldn't be too much added value with this approach, though, since they'd be too easily confused with normal binding annotations, while the implication of the @Assisted annotation is much different. Plus, it does take a fair amount of boilerplate to define an annotation type.

  1. http://code.google.com/p/google-guice/