Misconfigured assisted inject duplicates bindings

20 views
Skip to first unread message

Geoff Groos

unread,
Dec 15, 2015, 3:14:55 AM12/15/15
to google-guice
Hey guys,

I was allowed to execute this code without a provision error:

public interface Factory {
   
PowershellFileWriter create(@Assisted Queryable<VariableSymbol> variables,
                               
@Assisted @NotModified ToolTemplate toolTemplate,
                               
@Assisted Path outputPath);

}
@Inject
public PowershellFileWriter(FileSystem fileSystem,
                           
ResourceEnvironment.Factory envFactory,
                           
@Assisted Queryable<VariableSymbol> variables,
                           
@Assisted Path scriptPath,
                           
@Assisted Path outputPath) {

   
//...
}

when it is pretty clearly in error:
- no use of ToolTemplate in the constructor
- 2 assisted args of the same type without naming either

The corrected version looks like this:

    public interface Factory {
       
PowershellFileWriter create(Queryable<VariableSymbol> variables,
                                   
@Assisted("script") Path scriptPath,
                                   
@Assisted("out") Path outputPath);

   
}
   
@Inject
   
public PowershellFileWriter(FileSystem fileSystem,
                               
ResourceEnvironment.Factory envFactory,
                               
@Assisted Queryable<VariableSymbol> variables,
                               
@Assisted("script") Path scriptPath,
                               
@Assisted("out") Path outputPath) {

why wasn't I given a resolution exception when I went to use this factory, or a configuration exception when I bound up this factory? Isn't validation done on the Factory interface when we use the FactoryModuleBuilder?

Sam Berlin

unread,
Dec 15, 2015, 7:25:10 AM12/15/15
to google-guice

This is a "feature" (albeit, not the best one) of AssistedInject -- the @Inject constructors don't need to consume all the assisted param. This helps when you want to bind the implementation to a stub that does less.

If you want the error checking, I believe you can annotate the cxtor @AssistedInject instead of @Inject, and it will be a bit stricter.

sam


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/52237a42-109d-41e9-abb4-545dbbfc577a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages