Search in sources :

Example 46 with Key

use of com.google.inject.Key in project druid by druid-io.

the class JsonConfigProvider method bindInstance.

@SuppressWarnings("unchecked")
public static <T> void bindInstance(Binder binder, Key<T> bindKey, T instance) {
    binder.bind(bindKey).toInstance(instance);
    final ParameterizedType supType = Types.newParameterizedType(Supplier.class, bindKey.getTypeLiteral().getType());
    final Key supplierKey;
    if (bindKey.getAnnotationType() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotationType());
    } else if (bindKey.getAnnotation() != null) {
        supplierKey = Key.get(supType, bindKey.getAnnotation());
    } else {
        supplierKey = Key.get(supType);
    }
    binder.bind(supplierKey).toInstance(Suppliers.<T>ofInstance(instance));
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Key(com.google.inject.Key)

Example 47 with Key

use of com.google.inject.Key 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 48 with Key

use of com.google.inject.Key in project druid by druid-io.

the class ResourceFilterTestHelper method getRequestPaths.

// Feeds in an array of [ PathName, MethodName, ResourceFilter , Injector]
public static Collection<Object[]> getRequestPaths(final Class clazz, final Iterable<Class<?>> mockableInjections, final Iterable<Key<?>> mockableKeys, final Iterable<?> injectedObjs) {
    final Injector injector = Guice.createInjector(new Module() {

        @Override
        public void configure(Binder binder) {
            for (Class clazz : mockableInjections) {
                binder.bind(clazz).toInstance(EasyMock.createNiceMock(clazz));
            }
            for (Object obj : injectedObjs) {
                binder.bind((Class) obj.getClass()).toInstance(obj);
            }
            for (Key<?> key : mockableKeys) {
                binder.bind((Key<Object>) key).toInstance(EasyMock.createNiceMock(key.getTypeLiteral().getRawType()));
            }
            binder.bind(AuthConfig.class).toInstance(new AuthConfig(true));
        }
    });
    //Ignore the first "/"
    final String basepath = ((Path) clazz.getAnnotation(Path.class)).value().substring(1);
    final List<Class<? extends ResourceFilter>> baseResourceFilters = clazz.getAnnotation(ResourceFilters.class) == null ? Collections.<Class<? extends ResourceFilter>>emptyList() : ImmutableList.copyOf(((ResourceFilters) clazz.getAnnotation(ResourceFilters.class)).value());
    return ImmutableList.copyOf(Iterables.concat(// Step 3 - Merge all the Objects arrays for each endpoints
    Iterables.transform(//  - Resource Filter instance for the endpoint
    Iterables.filter(// ResourceFilters applied to them
    ImmutableList.copyOf(clazz.getDeclaredMethods()), new Predicate<Method>() {

        @Override
        public boolean apply(Method input) {
            return input.getAnnotation(GET.class) != null || input.getAnnotation(POST.class) != null || input.getAnnotation(DELETE.class) != null && (input.getAnnotation(ResourceFilters.class) != null || !baseResourceFilters.isEmpty());
        }
    }), new Function<Method, Collection<Object[]>>() {

        @Override
        public Collection<Object[]> apply(final Method method) {
            final List<Class<? extends ResourceFilter>> resourceFilters = method.getAnnotation(ResourceFilters.class) == null ? baseResourceFilters : ImmutableList.copyOf(method.getAnnotation(ResourceFilters.class).value());
            return Collections2.transform(resourceFilters, new Function<Class<? extends ResourceFilter>, Object[]>() {

                @Override
                public Object[] apply(Class<? extends ResourceFilter> input) {
                    if (method.getAnnotation(Path.class) != null) {
                        return new Object[] { String.format("%s%s", basepath, method.getAnnotation(Path.class).value()), input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    } else {
                        return new Object[] { basepath, input.getAnnotation(GET.class) == null ? (method.getAnnotation(DELETE.class) == null ? "POST" : "DELETE") : "GET", injector.getInstance(input), injector };
                    }
                }
            });
        }
    })));
}
Also used : Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) AuthConfig(io.druid.server.security.AuthConfig) Method(java.lang.reflect.Method) Predicate(com.google.common.base.Predicate) Binder(com.google.inject.Binder) ResourceFilters(com.sun.jersey.spi.container.ResourceFilters) Function(com.google.common.base.Function) ResourceFilter(com.sun.jersey.spi.container.ResourceFilter) DELETE(javax.ws.rs.DELETE) Injector(com.google.inject.Injector) GET(javax.ws.rs.GET) Collection(java.util.Collection) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Module(com.google.inject.Module) Key(com.google.inject.Key)

Example 49 with Key

use of com.google.inject.Key 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 50 with Key

use of com.google.inject.Key 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)

Aggregations

Key (com.google.inject.Key)106 AbstractModule (com.google.inject.AbstractModule)55 Injector (com.google.inject.Injector)52 Binding (com.google.inject.Binding)36 Module (com.google.inject.Module)20 Provider (com.google.inject.Provider)18 Element (com.google.inject.spi.Element)16 Map (java.util.Map)16 HasDependencies (com.google.inject.spi.HasDependencies)14 InstanceBinding (com.google.inject.spi.InstanceBinding)13 List (java.util.List)13 TypeLiteral (com.google.inject.TypeLiteral)12 LinkedKeyBinding (com.google.inject.spi.LinkedKeyBinding)10 ProviderInstanceBinding (com.google.inject.spi.ProviderInstanceBinding)9 PrivateModule (com.google.inject.PrivateModule)8 DefaultBindingTargetVisitor (com.google.inject.spi.DefaultBindingTargetVisitor)8 Dependency (com.google.inject.spi.Dependency)8 ProviderKeyBinding (com.google.inject.spi.ProviderKeyBinding)8 ImmutableList (com.google.common.collect.ImmutableList)7 InjectionPoint (com.google.inject.spi.InjectionPoint)7