Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gissuebot opened this issue Jul 7, 2014 · 0 comments
Labels

Comments

@gissuebot
Copy link

From brunojcm on December 15, 2012 20:42:43

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: http://code.google.com/p/google-guice/issues/detail?id=738

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant