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

项目: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;
}
项目:che    文件:MachineTokenInterceptorTest.java   
@Test
public void checkAllInterceptedMethodsArePresent() throws Throwable {
  ConstructorBinding<?> interceptedBinding =
      (ConstructorBinding<?>) injector.getBinding(WorkspaceRuntimes.class);

  for (Method method : interceptedBinding.getMethodInterceptors().keySet()) {
    workspaceRuntimes.getClass().getMethod(method.getName(), method.getParameterTypes());
  }
}
项目:deguicifier    文件:Generators.java   
private static String constructorCall(ConstructorBinding<?> binding) {
  StringBuilder builder = new StringBuilder();
  builder.append("new " + canonicalName(binding.getKey().getTypeLiteral()) + "(\n");
  builder.append(argumentList(binding));
  builder.append(")");
  return builder.toString();
}
项目:deguicifier    文件:BindingTypesTest.java   
@Test
public void binds_constructor() {
  given(module = new AbstractModule() {
    @Override
    protected void configure() {
      bind(Implementation.class);
    }
  });
  given(injector = Guice.createInjector(module));
  when(injector.getBinding(Implementation.class));
  thenReturned(instanceOf(ConstructorBinding.class));
}
项目:guice    文件:DefaultNodeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override
public Collection<Node> visit(ConstructorBinding<?> binding) {
  Collection<Member> members = Lists.newArrayList();
  members.add(binding.getConstructor().getMember());
  for (InjectionPoint injectionPoint : binding.getInjectableMembers()) {
    members.add(injectionPoint.getMember());
  }

  return ImmutableList.<Node>of(newImplementationNode(binding, members));
}
项目:guice    文件:TransitiveDependencyVisitorTest.java   
public void testVisitConstructor() {
  Binding<?> binding = getBinding(Key.get(ConstructedClass.class));
  Collection<Key<?>> dependencies = visitor.visit((ConstructorBinding<?>) binding);

  assertDependencies(
      dependencies, Key.get(A.class), Key.get(B.class), Key.get(C.class), Key.get(D.class));
}
项目:guice    文件:ConstructorBindingImpl.java   
@Override
public String toString() {
  return MoreObjects.toStringHelper(ConstructorBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .toString();
}
项目:guice    文件:MethodInterceptionTest.java   
public void testSpiAccessToInterceptors() throws NoSuchMethodException {
  final MethodInterceptor countingInterceptor = new CountingInterceptor();
  final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
  Injector injector =
      Guice.createInjector(
          new AbstractModule() {
            @Override
            protected void configure() {
              bindInterceptor(
                  Matchers.any(), Matchers.returns(only(Foo.class)), countingInterceptor);
              bindInterceptor(
                  Matchers.any(),
                  Matchers.returns(only(Foo.class).or(only(Bar.class))),
                  returnNullInterceptor);
            }
          });

  ConstructorBinding<?> interceptedBinding =
      (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
  Method barMethod = Interceptable.class.getMethod("bar");
  Method fooMethod = Interceptable.class.getMethod("foo");
  assertEquals(
      ImmutableMap.<Method, List<MethodInterceptor>>of(
          fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor),
          barMethod, ImmutableList.of(returnNullInterceptor)),
      interceptedBinding.getMethodInterceptors());

  ConstructorBinding<?> nonInterceptedBinding =
      (ConstructorBinding<?>) injector.getBinding(Foo.class);
  assertEquals(
      ImmutableMap.<Method, List<MethodInterceptor>>of(),
      nonInterceptedBinding.getMethodInterceptors());

  injector.getInstance(Interceptable.class).foo();
  assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
项目:guice-old    文件:DefaultNodeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override public Collection<Node> visit(ConstructorBinding<?> binding) {
  Collection<Member> members = Lists.newArrayList();
  members.add(binding.getConstructor().getMember());
  for (InjectionPoint injectionPoint : binding.getInjectableMembers()) {
    members.add(injectionPoint.getMember());
  }

  return ImmutableList.<Node>of(newImplementationNode(binding, members));
}
项目:guice-old    文件:TransitiveDependencyVisitorTest.java   
public void testVisitConstructor() {
  Binding<?> binding = getBinding(Key.get(ConstructedClass.class));
  Collection<Key<?>> dependencies = visitor.visit((ConstructorBinding<?>) binding);

  assertDependencies(dependencies, Key.get(A.class), Key.get(B.class), Key.get(C.class),
      Key.get(D.class));
}
项目:guice-old    文件:ConstructorBindingImpl.java   
@Override public String toString() {
  return Objects.toStringHelper(ConstructorBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .toString();
}
项目:guice-old    文件:MethodInterceptionTest.java   
public void testSpiAccessToInterceptors() throws NoSuchMethodException {
  final MethodInterceptor countingInterceptor = new CountingInterceptor();
  final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
  Injector injector = Guice.createInjector(new AbstractModule() {
    protected void configure() {
      bindInterceptor(Matchers.any(),Matchers.returns(only(Foo.class)),
          countingInterceptor);
      bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class).or(only(Bar.class))),
          returnNullInterceptor);
    }
  });

  ConstructorBinding<?> interceptedBinding
      = (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
  Method barMethod = Interceptable.class.getMethod("bar");
  Method fooMethod = Interceptable.class.getMethod("foo");
  assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(
      fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor),
      barMethod, ImmutableList.of(returnNullInterceptor)),
      interceptedBinding.getMethodInterceptors());

  ConstructorBinding<?> nonInterceptedBinding
      = (ConstructorBinding<?>) injector.getBinding(Foo.class);
  assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(),
      nonInterceptedBinding.getMethodInterceptors());

  injector.getInstance(Interceptable.class).foo();
  assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
项目:google-guice    文件:DefaultNodeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override public Collection<Node> visit(ConstructorBinding<?> binding) {
  Collection<Member> members = Lists.newArrayList();
  members.add(binding.getConstructor().getMember());
  for (InjectionPoint injectionPoint : binding.getInjectableMembers()) {
    members.add(injectionPoint.getMember());
  }

  return ImmutableList.<Node>of(newImplementationNode(binding, members));
}
项目:google-guice    文件:TransitiveDependencyVisitorTest.java   
public void testVisitConstructor() {
  Binding<?> binding = getBinding(Key.get(ConstructedClass.class));
  Collection<Key<?>> dependencies = visitor.visit((ConstructorBinding<?>) binding);

  assertDependencies(dependencies, Key.get(A.class), Key.get(B.class), Key.get(C.class),
      Key.get(D.class));
}
项目:google-guice    文件:ConstructorBindingImpl.java   
@Override public String toString() {
  return Objects.toStringHelper(ConstructorBinding.class)
      .add("key", getKey())
      .add("source", getSource())
      .add("scope", getScoping())
      .toString();
}
项目:google-guice    文件:MethodInterceptionTest.java   
public void testSpiAccessToInterceptors() throws NoSuchMethodException {
  final MethodInterceptor countingInterceptor = new CountingInterceptor();
  final MethodInterceptor returnNullInterceptor = new ReturnNullInterceptor();
  Injector injector = Guice.createInjector(new AbstractModule() {
    protected void configure() {
      bindInterceptor(Matchers.any(),Matchers.returns(only(Foo.class)),
          countingInterceptor);
      bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class).or(only(Bar.class))),
          returnNullInterceptor);
    }
  });

  ConstructorBinding<?> interceptedBinding
      = (ConstructorBinding<?>) injector.getBinding(Interceptable.class);
  Method barMethod = Interceptable.class.getMethod("bar");
  Method fooMethod = Interceptable.class.getMethod("foo");
  assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(
      fooMethod, ImmutableList.of(countingInterceptor, returnNullInterceptor),
      barMethod, ImmutableList.of(returnNullInterceptor)),
      interceptedBinding.getMethodInterceptors());

  ConstructorBinding<?> nonInterceptedBinding
      = (ConstructorBinding<?>) injector.getBinding(Foo.class);
  assertEquals(ImmutableMap.<Method, List<MethodInterceptor>>of(),
      nonInterceptedBinding.getMethodInterceptors());

  injector.getInstance(Interceptable.class).foo();
  assertEquals("expected counting interceptor to be invoked first", 1, count.get());
}
项目:ProjectAres    文件:BindingTargetTypeResolver.java   
@Override
public Optional<TypeLiteral<?>> visit(ConstructorBinding<?> binding) {
    // Return the owning type of the constructor
    return of(binding.getConstructor().getDeclaringType());
}
项目:ProjectAres    文件:DependencyCollector.java   
@Override
public Object visit(ConstructorBinding<? extends T> constructorBinding) {
    processInjectionPoint(constructorBinding.getConstructor());
    processInjectionPoints(constructorBinding.getInjectableMembers());
    return super.visit(constructorBinding);
}
项目:ProjectAres    文件:Scoper.java   
@Override
public Void visit(ConstructorBinding<? extends T> binding) {
    final InjectionPoint point = binding.getConstructor();
    scope(binding, rebind(binding).toConstructor((Constructor) point.getMember(), point.getDeclaringType()));
    return null;
}
项目:guice    文件:TransitiveDependencyVisitor.java   
@Override
public Collection<Key<?>> visit(ConstructorBinding<?> binding) {
  return visitHasDependencies(binding);
}
项目:guice    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override
public Collection<Edge> visit(ConstructorBinding<?> binding) {
  return newDependencyEdges(NodeId.newTypeId(binding.getKey()), binding);
}
项目:guice    文件:Indexer.java   
@Override
public Indexer.IndexedBinding visit(ConstructorBinding<? extends Object> binding) {
  return new Indexer.IndexedBinding(
      binding, BindingType.CONSTRUCTOR, scope(binding), binding.getConstructor());
}
项目:papaya    文件:ResolvedBinding.java   
@Override
public Set<ResolvedBinding<?>> visit(ConstructorBinding<?> constructorBinding) {
  return getDependencyBindings(constructorBinding);
}
项目:guice-old    文件:TransitiveDependencyVisitor.java   
@Override public Collection<Key<?>> visit(ConstructorBinding<?> binding) {
  return visitHasDependencies(binding);
}
项目:google-guice    文件:TransitiveDependencyVisitor.java   
@Override public Collection<Key<?>> visit(ConstructorBinding<?> binding) {
  return visitHasDependencies(binding);
}
项目:guice-old    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override public Collection<Edge> visit(ConstructorBinding<?> binding) {
  return newDependencyEdges(NodeId.newTypeId(binding.getKey()), binding);
}
项目:google-guice    文件:DefaultEdgeCreator.java   
/**
 * Visitor for {@link ConstructorBinding}s. These are for classes that Guice will instantiate to
 * satisfy injection requests.
 */
@Override public Collection<Edge> visit(ConstructorBinding<?> binding) {
  return newDependencyEdges(NodeId.newTypeId(binding.getKey()), binding);
}