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(); } }
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 <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; }
@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()); } }
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(); }
@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)); }
/** * 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)); }
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)); }
@Override public String toString() { return MoreObjects.toStringHelper(ConstructorBinding.class) .add("key", getKey()) .add("source", getSource()) .add("scope", getScoping()) .toString(); }
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()); }
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)); }
@Override public String toString() { return Objects.toStringHelper(ConstructorBinding.class) .add("key", getKey()) .add("source", getSource()) .add("scope", getScoping()) .toString(); }
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()); }
@Override public Optional<TypeLiteral<?>> visit(ConstructorBinding<?> binding) { // Return the owning type of the constructor return of(binding.getConstructor().getDeclaringType()); }
@Override public Object visit(ConstructorBinding<? extends T> constructorBinding) { processInjectionPoint(constructorBinding.getConstructor()); processInjectionPoints(constructorBinding.getInjectableMembers()); return super.visit(constructorBinding); }
@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; }
@Override public Collection<Key<?>> visit(ConstructorBinding<?> binding) { return visitHasDependencies(binding); }
/** * 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); }
@Override public Indexer.IndexedBinding visit(ConstructorBinding<? extends Object> binding) { return new Indexer.IndexedBinding( binding, BindingType.CONSTRUCTOR, scope(binding), binding.getConstructor()); }
@Override public Set<ResolvedBinding<?>> visit(ConstructorBinding<?> constructorBinding) { return getDependencyBindings(constructorBinding); }