Skip to content

Commit

Permalink
Internal refactor
Browse files Browse the repository at this point in the history
RELNOTES=N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=320418548
  • Loading branch information
bcorso authored and kevinb9n committed Jul 9, 2020
1 parent c5de4ab commit f3ff00a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,15 @@ private MethodSpec providesMethod(BindValueElement bindValue) {
.addStatement("return test.$L", bindValue.variableElement().getSimpleName());

ClassName annotationClassName = bindValue.annotationName();
if (annotationClassName.equals(ClassNames.BIND_VALUE_INTO_MAP)
) {
if (BindValueMetadata.BIND_VALUE_INTO_MAP_ANNOTATIONS.contains(annotationClassName)) {
builder.addAnnotation(IntoMap.class);
// It is safe to call get() on the Optional<AnnotationMirror> returned by mapKey()
// because a @BindValueIntoMap is required to have one and is checked in
// BindValueMetadata.BindValueElement.create().
builder.addAnnotation(AnnotationSpec.get(bindValue.mapKey().get()));
} else if (annotationClassName.equals(ClassNames.BIND_VALUE_INTO_SET)
) {
} else if (BindValueMetadata.BIND_VALUE_INTO_SET_ANNOTATIONS.contains(annotationClassName)) {
builder.addAnnotation(IntoSet.class);
} else if (annotationClassName.equals(ClassNames.BIND_ELEMENTS_INTO_SET)
) {
} else if (BindValueMetadata.BIND_ELEMENTS_INTO_SET_ANNOTATIONS.contains(annotationClassName)) {
builder.addAnnotation(ElementsIntoSet.class);
}
bindValue.qualifier().ifPresent(q -> builder.addAnnotation(AnnotationSpec.get(q)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@
*/
@AutoValue
abstract class BindValueMetadata {
static final ImmutableSet<ClassName> BIND_VALUE_ANNOTATIONS =
ImmutableSet.of(
ClassNames.ANDROID_BIND_VALUE);
static final ImmutableSet<ClassName> BIND_VALUE_INTO_SET_ANNOTATIONS =
ImmutableSet.of(
ClassNames.ANDROID_BIND_VALUE_INTO_SET);
static final ImmutableSet<ClassName> BIND_ELEMENTS_INTO_SET_ANNOTATIONS =
ImmutableSet.of(
ClassNames.ANDROID_BIND_ELEMENTS_INTO_SET);
static final ImmutableSet<ClassName> BIND_VALUE_INTO_MAP_ANNOTATIONS =
ImmutableSet.of(
ClassNames.ANDROID_BIND_VALUE_INTO_MAP);

/** @return the {@code TestRoot} annotated class's name. */
abstract TypeElement testElement();

Expand Down Expand Up @@ -108,8 +121,7 @@ static BindValueElement create(Element element) {

ImmutableList<AnnotationMirror> mapKeys = Processors.getMapKeyAnnotations(element);
Optional<AnnotationMirror> optionalMapKeys;
if (annotationClassName.equals(ClassNames.BIND_VALUE_INTO_MAP)
) {
if (BIND_VALUE_INTO_MAP_ANNOTATIONS.contains(annotationClassName)) {
ProcessorErrors.checkState(
mapKeys.size() == 1,
element,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import dagger.internal.codegen.extension.DaggerStreams;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.AnnotationMirror;
Expand All @@ -47,18 +46,18 @@
@IncrementalAnnotationProcessor(ISOLATING)
@AutoService(Processor.class)
public final class BindValueProcessor extends BaseProcessor {

private static final ImmutableSet<ClassName> SUPPORTED_ANNOTATIONS =
ImmutableSet.of(
ClassNames.BIND_VALUE,
ClassNames.BIND_VALUE_INTO_MAP,
ClassNames.BIND_VALUE_INTO_SET,
ClassNames.BIND_ELEMENTS_INTO_SET);
ImmutableSet.<ClassName>builder()
.addAll(BindValueMetadata.BIND_VALUE_ANNOTATIONS)
.addAll(BindValueMetadata.BIND_VALUE_INTO_SET_ANNOTATIONS)
.addAll(BindValueMetadata.BIND_ELEMENTS_INTO_SET_ANNOTATIONS)
.addAll(BindValueMetadata.BIND_VALUE_INTO_MAP_ANNOTATIONS)
.build();

private final Multimap<TypeElement, Element> testRootMap = ArrayListMultimap.create();

@Override
public Set<String> getSupportedAnnotationTypes() {
public ImmutableSet<String> getSupportedAnnotationTypes() {
return SUPPORTED_ANNOTATIONS.stream()
.map(TypeName::toString)
.collect(DaggerStreams.toImmutableSet());
Expand Down
9 changes: 5 additions & 4 deletions java/dagger/hilt/processor/internal/ClassNames.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,13 @@ public final class ClassNames {
get("dagger.hilt.android.testing", "OnComponentReadyRunner");
public static final ClassName ON_COMPONENT_READY_RUNNER_HOLDER =
get("dagger.hilt.android.testing", "OnComponentReadyRunner", "OnComponentReadyRunnerHolder");
public static final ClassName BIND_VALUE = get("dagger.hilt.android.testing", "BindValue");
public static final ClassName BIND_ELEMENTS_INTO_SET =
public static final ClassName ANDROID_BIND_VALUE =
get("dagger.hilt.android.testing", "BindValue");
public static final ClassName ANDROID_BIND_ELEMENTS_INTO_SET =
get("dagger.hilt.android.testing", "BindElementsIntoSet");
public static final ClassName BIND_VALUE_INTO_MAP =
public static final ClassName ANDROID_BIND_VALUE_INTO_MAP =
get("dagger.hilt.android.testing", "BindValueIntoMap");
public static final ClassName BIND_VALUE_INTO_SET =
public static final ClassName ANDROID_BIND_VALUE_INTO_SET =
get("dagger.hilt.android.testing", "BindValueIntoSet");
public static final ClassName APPLICATION_CONTEXT =
get("dagger.hilt.android.qualifiers", "ApplicationContext");
Expand Down

0 comments on commit f3ff00a

Please sign in to comment.