Search in sources :

Example 1 with BindingImpl

use of com.google.inject.internal.BindingImpl in project camel by apache.

the class Injectors method tryCloseJitBindings.

private static void tryCloseJitBindings(Closer closer, Injector injector, Class<? extends Annotation> scopeAnnotationToClose, CloseErrors errors) {
    Class<? extends Injector> type = injector.getClass();
    Field field;
    try {
        field = type.getDeclaredField("jitBindings");
        field.setAccessible(true);
        Object bindings = field.get(injector);
        if (bindings != null) {
            if (bindings instanceof Map) {
                Map<Key<?>, BindingImpl<?>> map = (Map<Key<?>, BindingImpl<?>>) bindings;
                Set<Entry<Key<?>, BindingImpl<?>>> entries = map.entrySet();
                for (Entry<Key<?>, BindingImpl<?>> entry : entries) {
                    closeBinding(entry.getKey(), entry.getValue(), scopeAnnotationToClose, closer, errors);
                }
            }
        }
    } catch (NoSuchFieldException e) {
    // ignore - Guice has refactored so we can't access the jit bindings
    // System.out.println("No such field! " + e);
    } catch (IllegalAccessException e) {
    // ignore - Guice has refactored so we can't access the jit bindings
    // System.out.println("Failed to access field: " + field +
    // ". Reason: " + e);
    }
}
Also used : Field(java.lang.reflect.Field) BindingImpl(com.google.inject.internal.BindingImpl) Entry(java.util.Map.Entry) Map(java.util.Map) Key(com.google.inject.Key)

Example 2 with BindingImpl

use of com.google.inject.internal.BindingImpl in project camel by apache.

the class Injectors method getScopeAnnotation.

/**
     * Returns the scope annotation for the given binding or null if there is no
     * scope
     */
public static Class<? extends Annotation> getScopeAnnotation(Binding<?> binding) {
    Class<? extends Annotation> scopeAnnotation = null;
    if (binding instanceof BindingImpl) {
        BindingImpl bindingImpl = (BindingImpl) binding;
        Scoping scoping = bindingImpl.getScoping();
        if (scoping != null) {
            scopeAnnotation = scoping.getScopeAnnotation();
            // TODO not sure why we need this hack???
            if (scopeAnnotation == null) {
                Scope scope = scoping.getScopeInstance();
                if (scope instanceof HasScopeAnnotation) {
                    HasScopeAnnotation hasScopeAnnotation = (HasScopeAnnotation) scope;
                    scopeAnnotation = hasScopeAnnotation.getScopeAnnotation();
                }
                if (scopeAnnotation == null && (scoping == Scoping.EAGER_SINGLETON || scoping == Scoping.SINGLETON_ANNOTATION || scoping == Scoping.SINGLETON_INSTANCE)) {
                    scopeAnnotation = Singleton.class;
                }
            }
        }
    }
    return scopeAnnotation;
}
Also used : BindingImpl(com.google.inject.internal.BindingImpl) HasScopeAnnotation(org.apache.camel.guice.support.HasScopeAnnotation) Scoping(com.google.inject.internal.Scoping) Scope(com.google.inject.Scope)

Aggregations

BindingImpl (com.google.inject.internal.BindingImpl)2 Key (com.google.inject.Key)1 Scope (com.google.inject.Scope)1 Scoping (com.google.inject.internal.Scoping)1 Field (java.lang.reflect.Field)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 HasScopeAnnotation (org.apache.camel.guice.support.HasScopeAnnotation)1