Nathan Merrill Nathan Merrill - 1 year ago 51
Java Question

How to add default methods to Guice interface

I've got the following interface:

public interface FooFactory {
Foo create();

default Foo createWithData(Data data){
Foo foo = create();
foo.addData(data);
return foo;
}
}


I'm installing it as follows:

install(new FactoryModuleBuilder().build(FooFactory.class));


However, the problem is that Guice overwrites my default method with its own factory method. The Foo I get back is the same Foo from
create()
(no data has been set). The default method never gets called.

Now, this is simply a convenience method, but is it possible for me to tell Guice not to overwrite my function?

Answer Source

the guice-assistedinject module has a bug for skipping the java-8 default method at line L252. and I found there is no tests to testing this feature.

a default method is neither a bridge method nor a synthetic method at all in java complier. However, its comments says that will skips the default methods. and the code should be as:

if (isDefault(method)){
  ...
}

you need to write your own Provider instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download