Java 类com.google.inject.spi.LinkedKeyBinding 实例源码

项目:deguicifier    文件:Generators.java   
private static String instantiation(Binding<?> binding) {
  if (binding instanceof InstanceBinding<?>) {
    return instanceLiteral((InstanceBinding<?>) binding);
  } else if (binding instanceof ProviderKeyBinding<?>) {
    return providerCall(((ProviderKeyBinding<?>) binding).getProviderKey()) + ".get()";
  } else if (binding instanceof LinkedKeyBinding<?>) {
    return providerCall(((LinkedKeyBinding<?>) binding).getLinkedKey());
  } else if (binding instanceof ConstructorBinding<?>) {
    return constructorCall((ConstructorBinding<?>) binding);
  } else if (binding instanceof ProviderBinding<?>) {
    return provider((ProviderBinding<?>) binding);
  } else if (binding instanceof ProviderInstanceBinding<?>) {
    return instantiation((ProviderInstanceBinding<?>) binding);
  } else {
    throw new DeguicifierException();
  }
}
项目:ProjectAres    文件:Bindings.java   
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();
}
项目:Equella    文件:GuicePlugin.java   
@Override
public <T> Collection<T> getBeansOfType(Class<T> clazz)
{
    Injector injector = ensureInjector();
    List<T> beans = new ArrayList<T>();
    Map<Key<?>, Binding<?>> bindings = injector.getBindings();
    for( Binding<?> binding : bindings.values() )
    {
        Key<?> actualKey = null;
        if( binding instanceof UntargettedBinding || binding instanceof ConstructorBinding )
        {
            actualKey = binding.getKey();
        }
        else if( binding instanceof LinkedKeyBinding )
        {
            actualKey = ((LinkedKeyBinding<?>) binding).getLinkedKey();
        }
        else if( binding instanceof ProviderBinding )
        {
            actualKey = ((ProviderBinding<?>) binding).getProvidedKey();
        }
        if( actualKey != null && clazz.isAssignableFrom(actualKey.getTypeLiteral().getRawType()) )
        {
            beans.add(clazz.cast(binding.getProvider().get()));
        }
    }
    return beans;
}
项目:deguicifier    文件:BindingTypesTest.java   
@Test
public void binds_linked_key() {
  given(module = new AbstractModule() {
    @Override
    protected void configure() {
      bind(Interface.class).to(Implementation.class);
    }
  });
  given(injector = Guice.createInjector(module));
  when(injector.getBinding(Interface.class));
  thenReturned(instanceOf(LinkedKeyBinding.class));
}
项目:guice    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link LinkedKeyBinding}. This is the standard {@link Binding} you get from
 * binding an interface class to an implementation class. We draw a {@link BindingEdge} from the
 * interface node to the node of the implementing class.
 */
@Override
public Collection<Edge> visit(LinkedKeyBinding<?> binding) {
  return ImmutableList.<Edge>of(
      new BindingEdge(
          NodeId.newTypeId(binding.getKey()),
          NodeId.newTypeId(binding.getLinkedKey()),
          BindingEdge.Type.NORMAL));
}
项目:guice    文件:TransitiveDependencyVisitorTest.java   
public void testVisitLinkedKey() {
  Binding<?> binding = getBinding(Key.get(Interface.class), new LinkedKeyModule());
  Collection<Key<?>> dependencies = visitor.visit((LinkedKeyBinding<?>) binding);

  // Dependency should be to the class this interface is bound to.
  assertDependencies(dependencies, Key.get(ConstructedClass.class));
}
项目:guice    文件:LinkedBindingImpl.java   
@Override
public String toString() {
  return MoreObjects.toStringHelper(LinkedKeyBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .add("target", targetKey)
      .toString();
}
项目:guice    文件:SpiUtils.java   
private static boolean matches(Binding<?> item, BindResult<?> result) {
  switch (result.type) {
    case INSTANCE:
      if (item instanceof InstanceBinding
          && ((InstanceBinding) item).getInstance().equals(result.instance)) {
        return true;
      }
      break;
    case LINKED:
      if (item instanceof LinkedKeyBinding
          && ((LinkedKeyBinding) item).getLinkedKey().equals(result.key)) {
        return true;
      }
      break;
    case PROVIDER_INSTANCE:
      if (item instanceof ProviderInstanceBinding
          && Objects.equal(
              ((ProviderInstanceBinding) item).getUserSuppliedProvider().get(),
              result.instance)) {
        return true;
      }
      break;
    case PROVIDER_KEY:
      if (item instanceof ProviderKeyBinding
          && ((ProviderKeyBinding) item).getProviderKey().equals(result.key)) {
        return true;
      }
      break;
  }
  return false;
}
项目:guice-old    文件:TransitiveDependencyVisitorTest.java   
public void testVisitLinkedKey() {
  Binding<?> binding = getBinding(Key.get(Interface.class), new LinkedKeyModule());
  Collection<Key<?>> dependencies = visitor.visit((LinkedKeyBinding<?>) binding);

  // Dependency should be to the class this interface is bound to.
  assertDependencies(dependencies, Key.get(ConstructedClass.class));
}
项目:guice-old    文件:SpiUtils.java   
private static boolean matches(Binding<?> item, BindResult<?> result) {
  switch (result.type) {
  case INSTANCE:
    if (item instanceof InstanceBinding
        && ((InstanceBinding) item).getInstance().equals(result.instance)) {
      return true;
    }
    break;
  case LINKED:
    if (item instanceof LinkedKeyBinding
        && ((LinkedKeyBinding) item).getLinkedKey().equals(result.key)) {
      return true;
    }
    break;
  case PROVIDER_INSTANCE:
    if (item instanceof ProviderInstanceBinding
        && Objects.equal(((ProviderInstanceBinding) item).getUserSuppliedProvider().get(),
                         result.instance)) {
      return true;
    }
    break;
  case PROVIDER_KEY:
    if (item instanceof ProviderKeyBinding
        && ((ProviderKeyBinding) item).getProviderKey().equals(result.key)) {
      return true;
    }
    break;
  }
  return false;
}
项目:guice-old    文件:LinkedBindingImpl.java   
@Override public String toString() {
  return Objects.toStringHelper(LinkedKeyBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .add("target", targetKey)
      .toString();
}
项目:google-guice    文件:TransitiveDependencyVisitorTest.java   
public void testVisitLinkedKey() {
  Binding<?> binding = getBinding(Key.get(Interface.class), new LinkedKeyModule());
  Collection<Key<?>> dependencies = visitor.visit((LinkedKeyBinding<?>) binding);

  // Dependency should be to the class this interface is bound to.
  assertDependencies(dependencies, Key.get(ConstructedClass.class));
}
项目:google-guice    文件:LinkedBindingImpl.java   
@Override public String toString() {
  return Objects.toStringHelper(LinkedKeyBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .add("target", targetKey)
      .toString();
}
项目:ProjectAres    文件:BindingTargetTypeResolver.java   
@Override
public Optional<TypeLiteral<?>> visit(LinkedKeyBinding<?> binding) {
    // Delegate to the binding for the target type
    return injector.getBinding(binding.getLinkedKey()).acceptTargetVisitor(this);
}
项目:ProjectAres    文件:DependencyCollector.java   
@Override
public Object visit(LinkedKeyBinding<? extends T> linkedKeyBinding) {
    requireKey(linkedKeyBinding.getLinkedKey());
    return super.visit(linkedKeyBinding);
}
项目:ProjectAres    文件:Scoper.java   
@Override
public Void visit(LinkedKeyBinding<? extends T> binding) {
    scope(binding, rebind(binding).to(binding.getLinkedKey()));
    return null;
}
项目:Equella    文件:DependencyAnalyzer.java   
@Override
public Void visit(LinkedKeyBinding<? extends Object> linkedKeyBinding)
{
    analyzeImplementation(linkedKeyBinding.getLinkedKey().getTypeLiteral(), false);
    return null;
}
项目:google-gin    文件:GuiceBindingVisitor.java   
public Void visit(LinkedKeyBinding<? extends T> linkedKeyBinding) {
  Context context = Context.forElement(linkedKeyBinding);
  bindingsCollection.addBinding(targetKey,
      bindingFactory.getBindClassBinding(linkedKeyBinding.getLinkedKey(), targetKey, context));
  return null;
}
项目:guice    文件:TransitiveDependencyVisitor.java   
@Override
public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) {
  return ImmutableSet.<Key<?>>of(binding.getLinkedKey());
}
项目:guice    文件:Indexer.java   
@Override
public Indexer.IndexedBinding visit(LinkedKeyBinding<? extends Object> binding) {
  return new Indexer.IndexedBinding(
      binding, BindingType.LINKED_KEY, scope(binding), binding.getLinkedKey());
}
项目:papaya    文件:ResolvedBinding.java   
@Override
public ResolvedBinding<T> visit(LinkedKeyBinding<? extends T> linkedKeyBinding) {
  return traverse(linkedKeyBinding.getLinkedKey());
}
项目:guice-old    文件:TransitiveDependencyVisitor.java   
@Override public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) {
  return ImmutableSet.<Key<?>>of(binding.getLinkedKey());
}
项目:guice-old    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link LinkedKeyBinding}. This is the standard {@link Binding} you get from
 * binding an interface class to an implementation class. We  draw a {@link BindingEdge} from
 * the interface node to the node of the implementing class.
 */
@Override public Collection<Edge> visit(LinkedKeyBinding<?> binding) {
  return ImmutableList.<Edge>of(new BindingEdge(NodeId.newTypeId(binding.getKey()),
      NodeId.newTypeId(binding.getLinkedKey()),
      BindingEdge.Type.NORMAL));
}
项目:google-guice    文件:TransitiveDependencyVisitor.java   
@Override public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) {
  return ImmutableSet.<Key<?>>of(binding.getLinkedKey());
}
项目:google-guice    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link LinkedKeyBinding}. This is the standard {@link Binding} you get from
 * binding an interface class to an implementation class. We  draw a {@link BindingEdge} from
 * the interface node to the node of the implementing class.
 */
@Override public Collection<Edge> visit(LinkedKeyBinding<?> binding) {
  return ImmutableList.<Edge>of(new BindingEdge(NodeId.newTypeId(binding.getKey()),
      NodeId.newTypeId(binding.getLinkedKey()),
      BindingEdge.Type.NORMAL));
}
项目:google-guice    文件:SpiUtils.java   
@SuppressWarnings("unchecked")
private static <T> void setInjectorTest(Key<T> setKey, TypeLiteral<?> elementType,
    Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings,
    BindResult... results) {
  Injector injector = Guice.createInjector(modules);
  Visitor<T> visitor = new Visitor<T>();
  Binding<T> binding = injector.getBinding(setKey);
  MultibinderBinding<T> multibinder = (MultibinderBinding<T>)binding.acceptTargetVisitor(visitor);
  assertNotNull(multibinder);
  assertEquals(elementType, multibinder.getElementTypeLiteral());
  assertEquals(allowDuplicates, multibinder.permitsDuplicates());
  List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements());
  List<BindResult> bindResults = Lists.newArrayList(results);
  assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(),
      bindResults.size(), elements.size());

  for(BindResult result : bindResults) {
    Binding found = null;
    for(Binding item : elements) {
      switch (result.type) {
      case INSTANCE:
        if (item instanceof InstanceBinding
            && ((InstanceBinding) item).getInstance().equals(result.instance)) {
          found = item;
        }
        break;
      case LINKED:
        if (item instanceof LinkedKeyBinding
            && ((LinkedKeyBinding) item).getKey().equals(result.key)) {
          found = item;
        }
        break;
      case PROVIDER_INSTANCE:
        if (item instanceof ProviderInstanceBinding
            && ((ProviderInstanceBinding) item).getProviderInstance().get().equals(
                result.instance)) {
          found = item;
        }
        break;
      }
    }
    if(found == null) {
      fail("Could not find element: " + result + " in remaining elements: " + elements);
    } else {
      elements.remove(found);
    }
  }

  if(!elements.isEmpty()) {
    fail("Found all elements of: " + bindResults + ", but more were left over: " + elements);
  }

  Set<Binding> setOfElements = new HashSet<Binding>(multibinder.getElements()); 

  List<Object> otherMultibinders = Lists.newArrayList();
  List<Binding> otherContains = Lists.newArrayList();
  for(Binding b : injector.getAllBindings().values()) {
    boolean contains = multibinder.containsElement(b);
    Object visited = b.acceptTargetVisitor(visitor);
    if(visited != null) {
      if(visited.equals(multibinder)) {
        assertTrue(contains);
      } else {
        otherMultibinders.add(visited);
      }
    } else if(setOfElements.contains(b)) {
      assertTrue(contains);
    } else if(contains) {
      otherContains.add(b);
    }
  }

  if(allowDuplicates) {
    assertEquals("contained more than it should: " + otherContains, 1, otherContains.size());
  } else {
    assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty());
  }
  assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings,
      otherMultibinders.size());

}