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));
}
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.");
}
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 };
}
}
});
}
})));
}
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);
}
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());
}
}
};
}
Aggregations