下面列出了怎么用com.google.inject.spi.LinkedKeyBinding的API类实例代码及写法,或者点击链接到github查看源代码。
private static boolean checkBindingRemoveRequired(final ConfigurationContext context,
final Binding binding,
final List<Class<?>> extensions,
final Multimap<Key, LinkedKeyBinding> linkedBindings) {
final Key key = binding.getKey();
if (isPossibleExtension(key)) {
context.stat().count(Stat.AnalyzedBindingsCount, 1);
final Class type = key.getTypeLiteral().getRawType();
if (ExtensionsSupport.registerExtensionBinding(context, type,
binding, BindingUtils.getTopDeclarationModule(binding))) {
LOGGER.debug("Extension detected from guice binding: {}", type.getSimpleName());
extensions.add(type);
return !context.isExtensionEnabled(type);
}
}
// note if linked binding recognized as extension by its key - it would not be counted (not needed)
if (binding instanceof LinkedKeyBinding) {
// remember all linked bindings (do not recognize on first path to avoid linked binding check before
// real binding)
final LinkedKeyBinding linkedBind = (LinkedKeyBinding) binding;
linkedBindings.put(linkedBind.getLinkedKey(), linkedBind);
}
return false;
}
private static List<LinkedKeyBinding> findLinkedBindingsToRemove(final ConfigurationContext context,
final List<Class<?>> extensions,
final Multimap<Key, LinkedKeyBinding> links) {
// try to recognize extensions in links
for (Map.Entry<Key, LinkedKeyBinding> entry : links.entries()) {
final Key key = entry.getKey();
final Class type = key.getTypeLiteral().getRawType();
final LinkedKeyBinding binding = entry.getValue();
if (!isPossibleExtension(key)) {
continue;
}
// try to detect extension in linked type (binding already analyzed so no need to count)
if (!extensions.contains(type) && ExtensionsSupport.registerExtensionBinding(context, type,
binding, BindingUtils.getTopDeclarationModule(binding))) {
LOGGER.debug("Extension detected from guice link binding: {}", type.getSimpleName());
extensions.add(type);
}
}
// find disabled bindings (already removed)
// for extensions recognized from links above imagine as we removed some (not existing) bindings
final List<Key> removedExtensions = extensions.stream()
.filter(it -> !context.isExtensionEnabled(it))
.map(Key::get)
.collect(Collectors.toList());
return removeChains(removedExtensions, links);
}
/**
* Pass in removed binding keys. Need to find all links ending on removed type and remove.
* Next, repeat with just removed types (to clean up entire chains because with it context may not start).
* For example: {@code bind(Interface1).to(Interface2); bind(Interface2).to(Extension)}
* Extension detected as extension, but if its disabled then link (Interface2 -> Extension) must be removed
* but without it Interface1 -> Interface2 remains and fail context becuase its just interfaces
* that's why entire chains must be removed.
*
* @param removed removed keys (to clean links leading to this keys)
* @param bindings all linked bindings (actually without links with recognized extension in left part)
* @return list of bindings to remove
*/
private static List<LinkedKeyBinding> removeChains(final List<Key> removed,
final Multimap<Key, LinkedKeyBinding> bindings) {
final List<Key> newlyRemoved = new ArrayList<>();
final List<LinkedKeyBinding> res = new ArrayList<>();
for (Key removedKey : removed) {
// remove all links ending on removed key
for (LinkedKeyBinding bnd : bindings.get(removedKey)) {
res.add(bnd);
newlyRemoved.add(bnd.getKey());
}
}
// continue removing chains
if (!newlyRemoved.isEmpty()) {
res.addAll(removeChains(newlyRemoved, bindings));
}
return res;
}
public static <T> Optional<TypeLiteral<? extends T>> targetType(Injector injector, Binding<T> binding) {
if(binding instanceof UntargettedBinding) {
return Optional.of(binding.getKey().getTypeLiteral());
} else if(binding instanceof ConstructorBinding) {
return Optional.of((TypeLiteral<? extends T>) ((ConstructorBinding) binding).getConstructor().getDeclaringType());
} else if(binding instanceof InstanceBinding) {
return Optional.of(TypeLiteral.get((Class<T>) ((InstanceBinding) binding).getInstance().getClass()));
} else if(binding instanceof LinkedKeyBinding) {
return targetType(injector, injector.getBinding(((LinkedKeyBinding) binding).getLinkedKey()));
} else if(binding instanceof ExposedBinding) {
return targetType(((ExposedBinding) binding).getPrivateElements().getInjector(), binding.getKey());
}
return Optional.empty();
}
@Override
public Optional<TypeLiteral<?>> visit(LinkedKeyBinding<?> binding) {
// Delegate to the binding for the target type
return injector.getBinding(binding.getLinkedKey()).acceptTargetVisitor(this);
}
@Override
public Object visit(LinkedKeyBinding<? extends T> linkedKeyBinding) {
requireKey(linkedKeyBinding.getLinkedKey());
return super.visit(linkedKeyBinding);
}
@Override
public Void visit(LinkedKeyBinding<? extends T> binding) {
scope(binding, rebind(binding).to(binding.getLinkedKey()));
return null;
}
@Override
public TypeToken<?> visit(LinkedKeyBinding<? extends Object> binding) {
Binding<?> linkedKeyBinding = injector
.getBinding(binding.getLinkedKey());
return linkedKeyBinding.acceptTargetVisitor(this);
}
@Override
public MapBinderBinding<?> visit(
LinkedKeyBinding<? extends Object> binding) {
return null;
}