Skip to content

Commit

Permalink
Implement TypedReleasableReferenceManagers.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=132562351
  • Loading branch information
netdpb authored and ronshapiro committed Sep 12, 2016
1 parent ccacb94 commit 800c746
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static dagger.internal.codegen.AbstractComponentWriter.InitializationState.INITIALIZED;
import static dagger.internal.codegen.AbstractComponentWriter.InitializationState.UNINITIALIZED;
import static dagger.internal.codegen.AnnotationSpecs.SUPPRESS_WARNINGS_UNCHECKED;
import static dagger.internal.codegen.BindingKey.contribution;
import static dagger.internal.codegen.CodeBlocks.makeParametersCodeBlock;
import static dagger.internal.codegen.ContributionBinding.FactoryCreationStrategy.ENUM_INSTANCE;
import static dagger.internal.codegen.ErrorMessages.CANNOT_RETURN_NULL_FROM_NON_NULLABLE_COMPONENT_METHOD;
Expand Down Expand Up @@ -488,6 +489,10 @@ private MethodSpec.Builder addBuilderMethodFromSpec(ExecutableElement method) {
*/
protected abstract void addFactoryMethods();

private boolean graphHasContributionBinding(Key key) {
return graph.resolvedBindings().containsKey(contribution(key));
}

private void addFrameworkFields() {
for (ResolvedBindings resolvedBindings : graph.resolvedBindings().values()) {
addField(resolvedBindings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ protected Iterable<? extends ProcessingStep> initSteps() {
new AnnotationCreatorGenerator(filer, elements);
UnwrappedMapKeyGenerator unwrappedMapKeyGenerator =
new UnwrappedMapKeyGenerator(filer, elements);

ComponentHierarchyValidator componentHierarchyValidator = new ComponentHierarchyValidator();
BindingGraphValidator bindingGraphValidator =
new BindingGraphValidator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ KindAndType ofType(TypeMirror type) {

abstract Kind kind();
abstract Key key();

BindingKey bindingKey() {
switch (kind()) {
case INSTANCE:
Expand Down
30 changes: 11 additions & 19 deletions compiler/src/main/java/dagger/internal/codegen/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,26 +297,16 @@ static final class Factory {
this.elements = checkNotNull(elements);
}

private DeclaredType setOf(TypeMirror elementType) {
return types.getDeclaredType(
elements.getTypeElement(Set.class.getCanonicalName()), elementType);
}

private DeclaredType mapOf(TypeMirror keyType, TypeMirror valueType) {
return types.getDeclaredType(
elements.getTypeElement(Map.class.getCanonicalName()), keyType, valueType);
}

private TypeElement getProviderElement() {
return elements.getTypeElement(Provider.class.getCanonicalName());
private TypeElement getClassElement(Class<?> cls) {
return elements.getTypeElement(cls.getCanonicalName());
}

private TypeElement getProducerElement() {
return elements.getTypeElement(Producer.class.getCanonicalName());
private DeclaredType setOf(TypeMirror elementType) {
return types.getDeclaredType(getClassElement(Set.class), elementType);
}

private TypeElement getClassElement(Class<?> cls) {
return elements.getTypeElement(cls.getName());
private DeclaredType mapOf(TypeMirror keyType, TypeMirror valueType) {
return types.getDeclaredType(getClassElement(Map.class), keyType, valueType);
}

Key forComponentMethod(ExecutableElement componentMethod) {
Expand Down Expand Up @@ -356,11 +346,13 @@ Key forSubcomponentBuilder(TypeMirror builderType) {
}

Key forProvidesMethod(ExecutableElement method, TypeElement contributingModule) {
return forBindingMethod(method, contributingModule, Optional.of(getProviderElement()));
return forBindingMethod(
method, contributingModule, Optional.of(getClassElement(Provider.class)));
}

Key forProducesMethod(ExecutableElement method, TypeElement contributingModule) {
return forBindingMethod(method, contributingModule, Optional.of(getProducerElement()));
return forBindingMethod(
method, contributingModule, Optional.of(getClassElement(Producer.class)));
}

/** Returns the key bound by a {@link Binds} method. */
Expand All @@ -386,7 +378,7 @@ private Key forBindingMethod(
ContributionType contributionType = ContributionType.fromBindingMethod(method);
TypeMirror returnType = normalize(types, methodType.getReturnType());
if (frameworkType.isPresent()
&& frameworkType.get().equals(getProducerElement())
&& frameworkType.get().equals(getClassElement(Producer.class))
&& MoreTypes.isTypeOf(ListenableFuture.class, returnType)) {
returnType = Iterables.getOnlyElement(MoreTypes.asDeclared(returnType).getTypeArguments());
}
Expand Down
88 changes: 40 additions & 48 deletions compiler/src/main/java/dagger/internal/codegen/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package dagger.internal.codegen;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.auto.common.MoreElements.isAnnotationPresent;
import static com.google.common.base.Preconditions.checkArgument;
import static dagger.internal.codegen.ErrorMessages.stripCommonTypePrefixes;
import static dagger.internal.codegen.InjectionAnnotations.getScopes;

import com.google.auto.common.AnnotationMirrors;
import com.google.auto.common.MoreTypes;
import com.google.auto.value.AutoValue;
import com.google.common.base.Equivalence;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.FluentIterable;
Expand All @@ -36,28 +39,45 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
* A representation of the scope (or lack of it) associated with a component, providing method
* or injection location.
*/
final class Scope {
/** A javax.inject.Scope. */
@AutoValue
abstract class Scope {

/** The underlying {@link AnnotationMirror} that represents the scope annotation. */
abstract Equivalence.Wrapper<AnnotationMirror> scopeAnnotation();

/**
* The underlying {@link AnnotationMirror} that represents the scope annotation.
* Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type.
*/
private final AnnotationMirror annotationMirror;
static Scope scope(AnnotationMirror scopeAnnotation) {
checkArgument(
isAnnotationPresent(
scopeAnnotation.getAnnotationType().asElement(), javax.inject.Scope.class));
return new AutoValue_Scope(AnnotationMirrors.equivalence().wrap(scopeAnnotation));
}

private Scope(AnnotationMirror annotationMirror) {
this.annotationMirror = checkNotNull(annotationMirror);
/**
* Creates a {@link Scope} object from the {@link javax.inject.Scope}-annotated annotation type.
*/
static Scope scope(TypeElement scopeType) {
return scope(SimpleAnnotationMirror.of(scopeType));
}

/** Returns all of the associated scoped annotations from the source code element. */
private static Scope scope(Elements elements, Class<? extends Annotation> scopeAnnotationClass) {
return scope(elements.getTypeElement(scopeAnnotationClass.getCanonicalName()));
}

/** Returns all of the associated scopes for a source code element. */
static ImmutableSet<Scope> scopesOf(Element element) {
return FluentIterable.from(getScopes(element)).
transform(new Function<AnnotationMirror, Scope>() {
@Override public Scope apply(AnnotationMirror annotationMirror) {
return new Scope(annotationMirror);
}
}).toSet();
return FluentIterable.from(getScopes(element))
.transform(
new Function<AnnotationMirror, Scope>() {
@Override
public Scope apply(AnnotationMirror annotationMirror) {
return scope(annotationMirror);
}
})
.toSet();
}

/**
Expand All @@ -69,7 +89,7 @@ static Optional<Scope> uniqueScopeOf(Element element) {
if (scopeAnnotations.isEmpty()) {
return Optional.absent();
}
return Optional.of(new Scope(Iterables.getOnlyElement(scopeAnnotations)));
return Optional.of(scope(Iterables.getOnlyElement(scopeAnnotations)));
}

/**
Expand All @@ -93,12 +113,6 @@ static Scope reusableScope(Elements elements) {
return scope(elements, Reusable.class);
}

private static Scope scope(Elements elements, Class<? extends Annotation> scopeAnnotationClass) {
return new Scope(
SimpleAnnotationMirror.of(
elements.getTypeElement(scopeAnnotationClass.getCanonicalName())));
}

/**
* Returns the readable source representation (name with @ prefix) of the annotation type.
*
Expand All @@ -123,36 +137,14 @@ public String getQualifiedName() {
* The scope annotation element.
*/
public TypeElement scopeAnnotationElement() {
return MoreTypes.asTypeElement(annotationMirror.getAnnotationType());
}

/**
* Scopes are equal if the underlying {@link AnnotationMirror} are equivalent according to
* {@link AnnotationMirrors#equivalence()}.
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
} else if (obj instanceof Scope) {
Scope that = (Scope) obj;
return AnnotationMirrors.equivalence()
.equivalent(this.annotationMirror, that.annotationMirror);
} else {
return false;
}
}

@Override
public int hashCode() {
return AnnotationMirrors.equivalence().hash(annotationMirror);
return MoreTypes.asTypeElement(scopeAnnotation().get().getAnnotationType());
}

/**
* Returns a debug representation of the scope.
*/
@Override
public String toString() {
return annotationMirror.toString();
return scopeAnnotation().get().toString();
}
}
7 changes: 5 additions & 2 deletions compiler/src/main/java/dagger/internal/codegen/SetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package dagger.internal.codegen;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;

import com.google.auto.common.MoreTypes;
import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -77,7 +76,11 @@ TypeMirror unwrappedElementType(Class<?> wrappingClass) {
wrappingClass.getTypeParameters().length == 1,
"%s must have exactly one type parameter",
wrappingClass);
checkState(elementsAreTypeOf(wrappingClass));
checkArgument(
elementsAreTypeOf(wrappingClass),
"expected elements to be %s, but this type is %s",
wrappingClass,
declaredSetType());
return MoreTypes.asDeclared(elementType()).getTypeArguments().get(0);
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/src/main/java/dagger/internal/codegen/TypeNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ final class TypeNames {
static final ClassName PRODUCERS = ClassName.get(Producers.class);
static final ClassName PROVIDER = ClassName.get(Provider.class);
static final ClassName PROVIDER_OF_LAZY = ClassName.get(ProviderOfLazy.class);

static final ClassName SET = ClassName.get(Set.class);
static final ClassName SET_FACTORY = ClassName.get(SetFactory.class);
static final ClassName SET_OF_PRODUCED_PRODUCER = ClassName.get(SetOfProducedProducer.class);
static final ClassName SET_PRODUCER = ClassName.get(SetProducer.class);
static final ClassName SINGLE_CHECK = ClassName.get(SingleCheck.class);
static final ClassName STRING = ClassName.get(String.class);

static final ClassName UNSUPPORTED_OPERATION_EXCEPTION =
ClassName.get(UnsupportedOperationException.class);

Expand Down

0 comments on commit 800c746

Please sign in to comment.