Skip to content

Commit

Permalink
Remove more unnecessary TypeVisitors
Browse files Browse the repository at this point in the history
In these simple cases, the visitor pattern just makes the logic more complicated than necessary.

RELNOTES=N/A
PiperOrigin-RevId: 399274915
  • Loading branch information
bcorso authored and Dagger Team committed Sep 27, 2021
1 parent 17acb59 commit bb2a0d2
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions java/dagger/internal/codegen/base/OptionalType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@

package dagger.internal.codegen.base;

import static com.google.auto.common.MoreTypes.asTypeElement;
import static com.google.common.base.Preconditions.checkArgument;
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
import static dagger.internal.codegen.extension.DaggerStreams.valuesOf;

import com.google.auto.common.MoreElements;
import com.google.auto.common.MoreTypes;
import com.google.auto.value.AutoValue;
import com.google.common.base.Equivalence;
import com.google.common.collect.ImmutableMap;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import dagger.internal.codegen.javapoet.TypeNames;
import dagger.spi.model.Key;
import java.util.Optional;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeVisitor;
import javax.lang.model.util.SimpleTypeVisitor8;

/**
* Information about an {@code Optional} {@link TypeMirror}.
Expand All @@ -51,6 +52,11 @@ public enum OptionalKind {
/** {@link java.util.Optional}. */
JDK_OPTIONAL(TypeNames.JDK_OPTIONAL, "empty");

// Keep a cache from class name to OptionalKind for quick look-up.
private static final ImmutableMap<ClassName, OptionalKind> OPTIONAL_KIND_BY_CLASS_NAME =
valuesOf(OptionalKind.class)
.collect(toImmutableMap(value -> value.className, value -> value));

private final ClassName className;
private final String absentMethodName;

Expand All @@ -59,6 +65,14 @@ public enum OptionalKind {
this.absentMethodName = absentMethodName;
}

private static boolean isOptionalKind(TypeElement type) {
return OPTIONAL_KIND_BY_CLASS_NAME.containsKey(ClassName.get(type));
}

private static OptionalKind of(TypeElement type) {
return OPTIONAL_KIND_BY_CLASS_NAME.get(ClassName.get(type));
}

/** Returns {@code valueType} wrapped in the correct class. */
public ParameterizedTypeName of(TypeName valueType) {
return ParameterizedTypeName.get(className, valueType);
Expand Down Expand Up @@ -90,20 +104,6 @@ public CodeBlock presentObjectExpression(CodeBlock value) {
}
}

private static final TypeVisitor<Optional<OptionalKind>, Void> OPTIONAL_KIND =
new SimpleTypeVisitor8<Optional<OptionalKind>, Void>(Optional.empty()) {
@Override
public Optional<OptionalKind> visitDeclared(DeclaredType t, Void p) {
for (OptionalKind optionalKind : OptionalKind.values()) {
Name qualifiedName = MoreElements.asType(t.asElement()).getQualifiedName();
if (qualifiedName.contentEquals(optionalKind.className.canonicalName())) {
return Optional.of(optionalKind);
}
}
return Optional.empty();
}
};

/**
* The optional type itself, wrapped using {@link MoreTypes#equivalence()}.
*
Expand All @@ -120,7 +120,7 @@ private DeclaredType declaredOptionalType() {

/** Which {@code Optional} type is used. */
public OptionalKind kind() {
return declaredOptionalType().accept(OPTIONAL_KIND, null).get();
return OptionalKind.of(asTypeElement(declaredOptionalType()));
}

/** The value type. */
Expand All @@ -130,7 +130,7 @@ public TypeMirror valueType() {

/** Returns {@code true} if {@code type} is an {@code Optional} type. */
private static boolean isOptional(TypeMirror type) {
return type.accept(OPTIONAL_KIND, null).isPresent();
return type.getKind() == TypeKind.DECLARED && OptionalKind.isOptionalKind(asTypeElement(type));
}

/** Returns {@code true} if {@code key.type()} is an {@code Optional} type. */
Expand Down

0 comments on commit bb2a0d2

Please sign in to comment.