com.google.inject.spi.LinkedKeyBinding Java Examples

The following examples show how to use com.google.inject.spi.LinkedKeyBinding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModulesSupport.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
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;
}
 
Example #2
Source File: ModulesSupport.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
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);
}
 
Example #3
Source File: ModulesSupport.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: Bindings.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
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();
}
 
Example #5
Source File: BindingTargetTypeResolver.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Optional<TypeLiteral<?>> visit(LinkedKeyBinding<?> binding) {
    // Delegate to the binding for the target type
    return injector.getBinding(binding.getLinkedKey()).acceptTargetVisitor(this);
}
 
Example #6
Source File: DependencyCollector.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Object visit(LinkedKeyBinding<? extends T> linkedKeyBinding) {
    requireKey(linkedKeyBinding.getLinkedKey());
    return super.visit(linkedKeyBinding);
}
 
Example #7
Source File: Scoper.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Void visit(LinkedKeyBinding<? extends T> binding) {
    scope(binding, rebind(binding).to(binding.getLinkedKey()));
    return null;
}
 
Example #8
Source File: AdapterInjector.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public TypeToken<?> visit(LinkedKeyBinding<? extends Object> binding) {
	Binding<?> linkedKeyBinding = injector
			.getBinding(binding.getLinkedKey());
	return linkedKeyBinding.acceptTargetVisitor(this);
}
 
Example #9
Source File: AdapterInjector.java    From gef with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public MapBinderBinding<?> visit(
		LinkedKeyBinding<? extends Object> binding) {
	return null;
}