oki,
thanks for this 2 solution.
I have maybe seen an other solution :
using the AssistedInject.
So it make something like that in my code :
@Inject
public SimpleGraphImpl(@Assisted final boolean directed) {
this.directed = directed;
this.nodes = new HashSet<ISimpleNode>();
this.edges = new HashSet<ISimpleEdge>();
}
public interface IGraphFactory {
public ISimpleGraph create(boolean directed);
}
public class SimpleGraphModule extends AbstractModule {
@Override
protected void configure() {
.........
bind(IGraphFactory.class).toProvider(FactoryProvider.newFactory(IGraphFactory.class,
SimpleGraphImpl.class));
...
}
Do you think that it is a goos solution? me I like this idea.
But I have an exception when I do that :
Exception in thread "main" java.lang.IllegalAccessError: tried to
access class com.google.inject.assistedinject.FactoryProvider2 from
class $com.google.inject.assistedinject.FactoryProvider2$
$FastClassByGuice$$9dcdf6d7
at $com.google.inject.assistedinject.FactoryProvider2$
$FastClassByGuice$$9dcdf6d7.invoke(<generated>)
if you think that it is a bad solution. I will do the other solution,
so it is not necessary to look why I have this exception.
Thanks,
jérémie
Thanks
On 7 juil, 01:24, Maaartin-1 <[email protected]> wrote:
> Another (less common) possibility is to use
>
> public class DirectedHolder {
> public DirectedHolder(boolean directed) {
> this.directed = directed;
> }
> public final boolean directed;
>
> }
>
> and
>
> @Inject
> SimpleGraphImpl(DirectedHolder directedHolder) {
> this.directed = directedHolder.directed;
> this.nodes = new HashSet<ISimpleNode>();
> this.edges = new HashSet<ISimpleEdge>();
> }
>
> and
>
> bind(DirectedHolder.class).toInstance(new DirectedHolder(true));
>
> This may be better sometimes, but the standard way is to use binding
> annotations.
>
> On 10-07-06 19:57, Colin Decker wrote:
>
>
>
> > Use a binding annotation
> > (http://code.google.com/p/google-guice/wiki/BindingAnnotations):
>
> > @Inject
> > public SimpleGraphImpl(@Directed boolean directed) { // could use, say,
> > @Named("directed") as well
> > this.directed = directed;
> > this.nodes = new HashSet<ISimpleNode>();
> > this.edges = new HashSet<ISimpleEdge>();
> > }
>
> > Then in your module use bindConstant():
>
> > bindConstant().annotatedWith(Directed.class).to(false); // or
> > Names.named("directed") instead of Directed.class
> > bind(ISimpleGraph.class).to(SimpleGraphImpl.class);
>
> > On Tue, Jul 6, 2010 at 1:17 PM, Jerem's <[email protected]
> > <mailto:[email protected]>> wrote:
>
> > Hello everybody
>
> > I try to use google guice.
> > If the test is good, I will propose to my team to use it.
>
> > to test it I d veloppe a simple structure of graph (graph edge and
> > node).
> > My problem is that a graph is directed or not :
>
> > So my class graph look like that :
>
> > public class SimpleGraphImpl implements ISimpleGraph {
>
> > private final Boolean directed;
>
> > private final Collection<ISimpleNode> nodes;
>
> > private final Collection<ISimpleEdge> edges;
>
> > public SimpleGraphImpl(boolean directed) {
> > this.directed = directed;
> > this.nodes = new HashSet<ISimpleNode>();
> > this.edges = new HashSet<ISimpleEdge>();
> > }
>
> > ....
>
> > }
>
> > I would like to use guice in this classe.
>
> > So I have this module :
> > public class SimpleGraphModule extends AbstractModule {
>
> > �...@override
> > protected void configure() {
>
> > this.bind(ISimpleGraph.class).to(SimpleGraphImpl.class);
> > .....
> > }
>
> > }
>
> > but to be able to do that (if I have understood how guice work), I
> > have to do that :
>
> > @Inject
> > public SimpleGraphImpl(boolean directed) {
> > this.directed = directed;
> > this.nodes = new HashSet<ISimpleNode>();
> > this.edges = new HashSet<ISimpleEdge>();
> > }
>
> > bit it is not possible because boolean has no constructor with
> > @inject.
>
> > this exemple is important because in my team we often work in graph
> > structure.
>
> > do you know how can I resolve this problem?
>
> > (sorry for my bas english)
>
> > J r mie
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "google-guice" group.
> > To post to this group, send email to [email protected]
> > <mailto:[email protected]>.
> > To unsubscribe from this group, send email to
> > [email protected]
> > <mailto:google-guice%[email protected]>.
> > For more options, visit this group at
> > http://groups.google.com/group/google-guice?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "google-guice" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.