Search in sources :

Example 46 with Binding

use of com.google.inject.Binding in project neo4j-mobile-android by neo4j-contrib.

the class DBInspectorApplication method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    Module defaultModule = RoboGuice.newDefaultRoboModule(this);
    Module dbInspectorModule = new DBInspectorModule();
    Module combinedModule = Modules.combine(defaultModule, dbInspectorModule);
    Injector injector = RoboGuice.setBaseApplicationInjector(this, RoboGuice.DEFAULT_STAGE, combinedModule);
    Map<Key<?>, Binding<?>> bindings = injector.getAllBindings();
    for (Key<?> key : bindings.keySet()) {
        Binding<?> value = bindings.get(key);
        Ln.d("binding key '" + key + "', value '" + value + "'");
    }
    Ln.i("Application initialized.");
}
Also used : Binding(com.google.inject.Binding) Injector(com.google.inject.Injector) Module(com.google.inject.Module) Key(com.google.inject.Key)

Example 47 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PrivateInternals_DynamicTypes method dynamicItemsOf.

public static Map<TypeLiteral<?>, DynamicItem<?>> dynamicItemsOf(Injector src) {
    Map<TypeLiteral<?>, DynamicItem<?>> m = new HashMap<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        TypeLiteral<?> type = e.getKey().getTypeLiteral();
        if (type.getRawType() == DynamicItem.class) {
            ParameterizedType p = (ParameterizedType) type.getType();
            m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicItem<?>) e.getValue().getProvider().get());
        }
    }
    if (m.isEmpty()) {
        return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
}
Also used : Binding(com.google.inject.Binding) ParameterizedType(java.lang.reflect.ParameterizedType) TypeLiteral(com.google.inject.TypeLiteral) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Example 48 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PluginGuiceEnvironment method reattachSet.

private void reattachSet(ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles, Map<TypeLiteral<?>, DynamicSet<?>> sets, @Nullable Injector src, Plugin newPlugin) {
    if (src == null || sets == null || sets.isEmpty()) {
        return;
    }
    for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) {
        @SuppressWarnings("unchecked") TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
        @SuppressWarnings("unchecked") DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();
        // Index all old handles that match this DynamicSet<T> keyed by
        // annotations. Ignore the unique annotations, thereby favoring
        // the @Named annotations or some other non-unique naming.
        Map<Annotation, ReloadableRegistrationHandle<?>> am = new HashMap<>();
        List<ReloadableRegistrationHandle<?>> old = oldHandles.get(type);
        Iterator<ReloadableRegistrationHandle<?>> oi = old.iterator();
        while (oi.hasNext()) {
            ReloadableRegistrationHandle<?> h = oi.next();
            Annotation a = h.getKey().getAnnotation();
            if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
                am.put(a, h);
                oi.remove();
            }
        }
        // Replace old handles with new bindings, favoring cases where there
        // is an exact match on an @Named annotation. If there is no match
        // pick any handle and replace it. We generally expect only one
        // handle of each DynamicSet type when using unique annotations, but
        // possibly multiple ones if @Named was used. Plugin authors that want
        // atomic replacement across reloads should use @Named annotations with
        // stable names that do not change across plugin versions to ensure the
        // handles are swapped correctly.
        oi = old.iterator();
        for (Binding<?> binding : bindings(src, type)) {
            @SuppressWarnings("unchecked") Binding<Object> b = (Binding<Object>) binding;
            Key<Object> key = b.getKey();
            if (key.getAnnotation() == null) {
                continue;
            }
            @SuppressWarnings("unchecked") ReloadableRegistrationHandle<Object> h1 = (ReloadableRegistrationHandle<Object>) am.remove(key.getAnnotation());
            if (h1 != null) {
                replace(newPlugin, h1, b);
            } else if (oi.hasNext()) {
                @SuppressWarnings("unchecked") ReloadableRegistrationHandle<Object> h2 = (ReloadableRegistrationHandle<Object>) oi.next();
                oi.remove();
                replace(newPlugin, h2, b);
            } else {
                newPlugin.add(set.add(b.getKey(), b.getProvider()));
            }
        }
    }
}
Also used : Binding(com.google.inject.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DynamicSet(com.google.gerrit.extensions.registration.DynamicSet) Annotation(java.lang.annotation.Annotation) TypeLiteral(com.google.inject.TypeLiteral) ReloadableRegistrationHandle(com.google.gerrit.extensions.registration.ReloadableRegistrationHandle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Example 49 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PluginGuiceEnvironment method copy.

private Module copy(Injector src) {
    Set<TypeLiteral<?>> dynamicTypes = new HashSet<>();
    Set<TypeLiteral<?>> dynamicItemTypes = new HashSet<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        TypeLiteral<?> type = e.getKey().getTypeLiteral();
        if (type.getRawType() == DynamicItem.class) {
            ParameterizedType t = (ParameterizedType) type.getType();
            dynamicItemTypes.add(TypeLiteral.get(t.getActualTypeArguments()[0]));
        } else if (type.getRawType() == DynamicSet.class || type.getRawType() == DynamicMap.class) {
            ParameterizedType t = (ParameterizedType) type.getType();
            dynamicTypes.add(TypeLiteral.get(t.getActualTypeArguments()[0]));
        }
    }
    final Map<Key<?>, Binding<?>> bindings = new LinkedHashMap<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        if (dynamicTypes.contains(e.getKey().getTypeLiteral()) && e.getKey().getAnnotation() != null) {
            // exported to plugins.
            continue;
        } else if (dynamicItemTypes.contains(e.getKey().getTypeLiteral())) {
            continue;
        } else if (shouldCopy(e.getKey())) {
            bindings.put(e.getKey(), e.getValue());
        }
    }
    bindings.remove(Key.get(Injector.class));
    bindings.remove(Key.get(java.util.logging.Logger.class));
    @Nullable final Binding<HttpServletRequest> requestBinding = src.getExistingBinding(Key.get(HttpServletRequest.class));
    @Nullable final Binding<HttpServletResponse> responseBinding = src.getExistingBinding(Key.get(HttpServletResponse.class));
    return new AbstractModule() {

        @SuppressWarnings("unchecked")
        @Override
        protected void configure() {
            for (Map.Entry<Key<?>, Binding<?>> e : bindings.entrySet()) {
                Key<Object> k = (Key<Object>) e.getKey();
                Binding<Object> b = (Binding<Object>) e.getValue();
                bind(k).toProvider(b.getProvider());
            }
            if (requestBinding != null) {
                bind(HttpServletRequest.class).annotatedWith(RootRelative.class).toProvider(requestBinding.getProvider());
            }
            if (responseBinding != null) {
                bind(HttpServletResponse.class).annotatedWith(RootRelative.class).toProvider(responseBinding.getProvider());
            }
        }
    };
}
Also used : Binding(com.google.inject.Binding) HttpServletResponse(javax.servlet.http.HttpServletResponse) LinkedHashMap(java.util.LinkedHashMap) AbstractModule(com.google.inject.AbstractModule) ParameterizedType(java.lang.reflect.ParameterizedType) HttpServletRequest(javax.servlet.http.HttpServletRequest) TypeLiteral(com.google.inject.TypeLiteral) Injector(com.google.inject.Injector) RootRelative(com.google.gerrit.extensions.annotations.RootRelative) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) Key(com.google.inject.Key) Nullable(com.google.gerrit.common.Nullable) HashSet(java.util.HashSet)

Example 50 with Binding

use of com.google.inject.Binding in project gerrit by GerritCodeReview.

the class PluginGuiceEnvironment method reattachMap.

private void reattachMap(ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles, Map<TypeLiteral<?>, DynamicMap<?>> maps, @Nullable Injector src, Plugin newPlugin) {
    if (src == null || maps == null || maps.isEmpty()) {
        return;
    }
    for (Map.Entry<TypeLiteral<?>, DynamicMap<?>> e : maps.entrySet()) {
        @SuppressWarnings("unchecked") TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
        @SuppressWarnings("unchecked") PrivateInternals_DynamicMapImpl<Object> map = (PrivateInternals_DynamicMapImpl<Object>) e.getValue();
        Map<Annotation, ReloadableRegistrationHandle<?>> am = new HashMap<>();
        for (ReloadableRegistrationHandle<?> h : oldHandles.get(type)) {
            Annotation a = h.getKey().getAnnotation();
            if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
                am.put(a, h);
            }
        }
        for (Binding<?> binding : bindings(src, e.getKey())) {
            @SuppressWarnings("unchecked") Binding<Object> b = (Binding<Object>) binding;
            Key<Object> key = b.getKey();
            if (key.getAnnotation() == null) {
                continue;
            }
            @SuppressWarnings("unchecked") ReloadableRegistrationHandle<Object> h = (ReloadableRegistrationHandle<Object>) am.remove(key.getAnnotation());
            if (h != null) {
                replace(newPlugin, h, b);
                oldHandles.remove(type, h);
            } else {
                newPlugin.add(map.put(newPlugin.getName(), b.getKey(), b.getProvider()));
            }
        }
    }
}
Also used : Binding(com.google.inject.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Annotation(java.lang.annotation.Annotation) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) TypeLiteral(com.google.inject.TypeLiteral) ReloadableRegistrationHandle(com.google.gerrit.extensions.registration.ReloadableRegistrationHandle) PrivateInternals_DynamicMapImpl(com.google.gerrit.extensions.registration.PrivateInternals_DynamicMapImpl) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Aggregations

Binding (com.google.inject.Binding)92 Injector (com.google.inject.Injector)58 Key (com.google.inject.Key)36 AbstractModule (com.google.inject.AbstractModule)33 InstanceBinding (com.google.inject.spi.InstanceBinding)23 Map (java.util.Map)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Module (com.google.inject.Module)18 Element (com.google.inject.spi.Element)18 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)17 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)16 HttpServlet (javax.servlet.http.HttpServlet)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)12 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)12 ServletContext (javax.servlet.ServletContext)12 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 TypeLiteral (com.google.inject.TypeLiteral)10 MapBinderBinding (com.google.inject.multibindings.MapBinderBinding)10