pombreda / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 1 forks source link

Better message on assisted injection when there is no matching arguments on the constructor #738

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:

On the current trunk version (3.1.0-SNAPSHOT), using assisted injection, when 
you create a method in the factory interface and none of the constructors match 
the parameters on this method, the error message is the following:

com.google.guice.assistedprivateconstructor.SomeClass has @AssistedInject 
constructors, but none of them match the parameters in method 
com.google.guice.assistedprivateconstructor.SomeClassFactory.create().  Unable 
to create AssistedInject factory.

But, in most cases, there is a constructor matching the factory method, but the 
author only forgot to put the @Assisted annotation on the right parameters.
I think the error message should suggest this, like:

com.google.guice.assistedprivateconstructor.SomeClass has @AssistedInject 
constructors, but none of them match the parameters in method 
com.google.guice.assistedprivateconstructor.SomeClassFactory.create().  Unable 
to create AssistedInject factory. Check if have the @Assisted annotation on the 
right parameters.

That would make this silly mistake much more easy to realize and fix!

I used the following code to generate the message:

class SomeClass {
    @AssistedInject
    public SomeClass(String a) {
        //do nothing
    }
}

interface SomeClassFactory {
    SomeClass create(String a);
}

public class Main {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new AbstractModule() {
            @Override
            protected void configure() {
                install(new FactoryModuleBuilder()
                    .build(SomeClassFactory.class));
            }
        });

        SomeClassFactory factory = injector.getInstance(SomeClassFactory.class);
        SomeClass created = factory.create("a1");
        System.out.println(created);
    }
}

Original issue reported on code.google.com by bruno...@gmail.com on 16 Dec 2012 at 1:42