I've found this method useful for determining the ultimate scope of a
binding:

   /**
     * Returns the scope of the binding, or the scope of the linked binding
if
     * it was linked.
     */
    public static Scope getLinkedScope(Binding<?> binding) {
        BindingScopingVisitor<Scope> scoper = new
BindingScopingVisitor<Scope>() {
            public Scope visitNoScoping() {
                return Scopes.NO_SCOPE;
            }

            public Scope visitScopeAnnotation(Class<? extends Annotation>
scopeAnnotation) {
                throw new IllegalStateException("no annotations allowed
here");
            }

            public Scope visitScope(Scope scope) {
                return scope;
            }

            public Scope visitEagerSingleton() {
                return Scopes.SINGLETON;
            }
        };

        do {
            Scope scope = binding.acceptScopingVisitor(scoper);

            if (scope != Scopes.NO_SCOPE) {
                return scope;
            }

            if (binding instanceof LinkedBindingImpl) {
                LinkedBindingImpl<?> linkedBinding = (LinkedBindingImpl)
binding;
                Injector injector = linkedBinding.getInjector();
                if (injector != null) {
                    binding =
injector.getBinding(linkedBinding.getLinkedKey());
                    continue;
                }
            }

            return Scopes.NO_SCOPE;
        } while (true);
    }

Obviously modeled after Scopes.isSingleton, except it returns the Scope
instead.  This is useful for us because we're using introspection on
bindings to determine if it's safe to call binding.getInstance arbitrarily
(we only want to do it if it's a singleton binding, and we've internally
defined more than just Scopes.SINGLETON to be effectively a singleton).

Another useful change would be to allow Scopes.isSingleton to take
additional Scope objects that can also mean "I am a singleton".

Sam

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to