dagger.internal.codegen.binding.BindingNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dagger-compiler Show documentation
Show all versions of dagger-compiler Show documentation
A fast dependency injector for Android and Java.
The newest version!
/*
* Copyright (C) 2018 The Dagger Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dagger.internal.codegen.binding;
import static dagger.internal.codegen.binding.BindingType.PRODUCTION;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import dagger.internal.codegen.model.BindingKind;
import dagger.internal.codegen.model.ComponentPath;
import dagger.internal.codegen.model.DaggerElement;
import dagger.internal.codegen.model.DaggerTypeElement;
import dagger.internal.codegen.model.DependencyRequest;
import dagger.internal.codegen.model.Key;
import dagger.internal.codegen.model.Scope;
import java.util.Optional;
import javax.inject.Inject;
/**
* An implementation of {@link dagger.internal.codegen.model.Binding} that also exposes {@link
* BindingDeclaration}s associated with the binding.
*/
// TODO(dpb): Consider a supertype of dagger.internal.codegen.model.Binding that
// dagger.internal.codegen.binding.Binding
// could also implement.
@AutoValue
public abstract class BindingNode implements dagger.internal.codegen.model.Binding {
private BindingDeclarationFormatter bindingDeclarationFormatter;
public abstract Binding delegate();
public abstract ImmutableSet multibindingDeclarations();
public abstract ImmutableSet optionalBindingDeclarations();
public abstract ImmutableSet subcomponentDeclarations();
/**
* The elements (other than the binding's {@link #bindingElement()}) that are associated with the
* binding.
*
*
* - {@linkplain BindsOptionalOf optional binding} declarations
*
- {@linkplain Module#subcomponents() module subcomponent} declarations
*
- {@linkplain Multibinds multibinding} declarations
*
*/
public final Iterable associatedDeclarations() {
return Iterables.concat(
multibindingDeclarations(), optionalBindingDeclarations(), subcomponentDeclarations());
}
@Override
public Key key() {
return delegate().key();
}
@Override
public ImmutableSet dependencies() {
return delegate().dependencies();
}
@Override
public Optional bindingElement() {
return delegate().bindingElement().map(DaggerElement::from);
}
@Override
public Optional contributingModule() {
return delegate().contributingModule().map(DaggerTypeElement::from);
}
@Override
public boolean requiresModuleInstance() {
return delegate().requiresModuleInstance();
}
@Override
public Optional scope() {
return delegate().scope();
}
@Override
public boolean isNullable() {
return delegate().isNullable();
}
@Override
public boolean isProduction() {
return delegate().bindingType().equals(PRODUCTION);
}
@Override
public BindingKind kind() {
return delegate().kind();
}
@Override
public final String toString() {
return bindingDeclarationFormatter.format(delegate());
}
static final class Factory {
private final BindingDeclarationFormatter bindingDeclarationFormatter;
@Inject
Factory(BindingDeclarationFormatter bindingDeclarationFormatter) {
this.bindingDeclarationFormatter = bindingDeclarationFormatter;
}
public BindingNode forContributionBindings(
ComponentPath component,
ContributionBinding delegate,
Iterable multibindingDeclarations,
Iterable optionalBindingDeclarations,
Iterable subcomponentDeclarations) {
return create(
component,
delegate,
ImmutableSet.copyOf(multibindingDeclarations),
ImmutableSet.copyOf(optionalBindingDeclarations),
ImmutableSet.copyOf(subcomponentDeclarations));
}
public BindingNode forMembersInjectionBinding(
ComponentPath component, MembersInjectionBinding delegate) {
return create(component, delegate, ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of());
}
private BindingNode create(
ComponentPath component,
Binding delegate,
ImmutableSet multibindingDeclarations,
ImmutableSet optionalBindingDeclarations,
ImmutableSet subcomponentDeclarations) {
BindingNode node =
new AutoValue_BindingNode(
component,
delegate,
multibindingDeclarations,
optionalBindingDeclarations,
subcomponentDeclarations);
node.bindingDeclarationFormatter = bindingDeclarationFormatter;
return node;
}
}
}