AssistedInject fails with message "Return method not assignable to blah"

4 views
Skip to first unread message

Juan

unread,
Mar 14, 2008, 1:05:42 PM3/14/08
to google-guice
Hi,

Using AssistedInject If I declare a factory like this gives me the
following message:

Return method not assignable to blah


interface ClazzFactory {
Clazz create(String clazz);
}

If I declare it like this it works.....

interface ClazzFactoryThatWorks {
Object create(String clazz);
}

What is the problem?



--- Complete unit test ---

package guice;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;

import assistedinject.Assisted;
import assistedinject.AssistedInject;
import assistedinject.FactoryProvider;

public class LearningAssistedInject {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testCreateByFactoryWithSuperSucessed() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ClazzFactoryThatWorks.class).toProvider(
FactoryProvider.newFactory(ClazzFactoryThatWorks.class,
Clazz.class));
}
});
ClazzFactoryThatWorks factory = injector
.getInstance(ClazzFactoryThatWorks.class);
Clazz clazz = (Clazz) factory.create("teste");
assertEquals("teste", clazz.getClazz());
}

@Test
public void testCreateByFactoryWithClazzFails() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(ClazzFactory.class).toProvider(
FactoryProvider.newFactory(ClazzFactory.class,
Clazz.class));
}
});
ClazzFactory factory = injector.getInstance(ClazzFactory.class);
Clazz clazz = (Clazz) factory.create("teste");
assertEquals("teste", clazz.getClazz());
}


public static class Clazz {

private String clazz;

@AssistedInject
public Clazz(@Assisted String clazz) {
this.clazz = clazz;
}

public String getClazz() {
return clazz;
}
}

interface ClazzFactory {
Clazz create(String clazz);
}

interface ClazzFactoryThatWorks {
Object create(String clazz);
}

}

limpb...@gmail.com

unread,
Mar 14, 2008, 4:14:09 PM3/14/08
to google-guice

This is a bug in assistedinject!

The fix is to grab the latest assistedinject source from
Subversion - unfortunately that code is not written to
work with the 1.0 version of Guice.

If you'd like to use AssistedInject current with Guice 1.0,
then the only change you'll need to do is to include a copy
of the Objects.class file from build/classes/com/google/guice/internal
-- it's moved since 1.0 and I believe assistedinject is using
the new version.

Otherwise, just use the latest Guice code. As far as I know
it is almost completely backwards compatible. The only
behaviour changes have to do with handling of unchecked
exceptions.

Cheers,
Jesse

Juan

unread,
Mar 15, 2008, 9:27:12 AM3/15/08
to google-guice
Jesse,

Thanks!

Is this situation added in the unit tests?

Juan

limpb...@gmail.com

unread,
Mar 15, 2008, 2:40:37 PM3/15/08
to google-guice
Yup, it's test covered.
Reply all
Reply to author
Forward
0 new messages