vipx / google-guice

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

Let factory create an object depends on annotation defined at left hand side? #798

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:

Is it possible to let factory create an object depends on annotation defined at 
left hand side?

At the moment I have 2 annotation types, A and B.

@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface A {}

@Retention(RetentionPolicy.RUNTIME)
@BindingAnnotation
public @interface B {}

And interface and its implementation:

public interface ResultSet {}
public class ResultSetA implements ResultSet {}
public class ResultSetA implements ResultSet {}

I have a factory.

public interface ResultSetFactory {

    @A
    public ResultSet createResultSetA(String sql);

    @B
    public ResultSet createResultSetB(String sql);
}

And I setup the factory like this:

install(new FactoryModuleBuilder()
                .implement(ResultSet.class, A.class, ResultSetA.class)
                .implement(ResultSet.class, B.class, ResultSetB.class)
                .build(ResultSetFactory.class));

However, I don't want to create methods everytime I have a new ResultSet(*).
So I want to change the factory to be:

public interface ResultSetFactory {

    public ResultSet create(String sql);
}

And use the new factory like this:

@A ResultSet resultSetA = factory.create(sql);
@B ResultSet resultSetB = factory.create(sql);
...
...

So, my question is, is there any way to do like this?

Original issue reported on code.google.com by chatree.srichart@gmail.com on 4 Mar 2014 at 4:37