google / google/guice

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

Open
#738 0 comments 0 reactions 0 assignees View on GitHub
imported
Dominant language
Java
Stars
12.7k
Forks
1.7k
Avg merge
11m
Merged PRs (30d)
2

Description

_From [brunojcm](https://code.google.com/u/105555132528693440487/) 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_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.