Skip to content

Commit bb2a0d2

Browse files
bcorsoDagger Team
authored andcommitted
Remove more unnecessary TypeVisitors
In these simple cases, the visitor pattern just makes the logic more complicated than necessary. RELNOTES=N/A PiperOrigin-RevId: 399274915
1 parent 17acb59 commit bb2a0d2

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

java/dagger/internal/codegen/base/OptionalType.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,25 @@
1616

1717
package dagger.internal.codegen.base;
1818

19+
import static com.google.auto.common.MoreTypes.asTypeElement;
1920
import static com.google.common.base.Preconditions.checkArgument;
21+
import static dagger.internal.codegen.extension.DaggerStreams.toImmutableMap;
22+
import static dagger.internal.codegen.extension.DaggerStreams.valuesOf;
2023

21-
import com.google.auto.common.MoreElements;
2224
import com.google.auto.common.MoreTypes;
2325
import com.google.auto.value.AutoValue;
2426
import com.google.common.base.Equivalence;
27+
import com.google.common.collect.ImmutableMap;
2528
import com.squareup.javapoet.ClassName;
2629
import com.squareup.javapoet.CodeBlock;
2730
import com.squareup.javapoet.ParameterizedTypeName;
2831
import com.squareup.javapoet.TypeName;
2932
import dagger.internal.codegen.javapoet.TypeNames;
3033
import dagger.spi.model.Key;
31-
import java.util.Optional;
32-
import javax.lang.model.element.Name;
34+
import javax.lang.model.element.TypeElement;
3335
import javax.lang.model.type.DeclaredType;
36+
import javax.lang.model.type.TypeKind;
3437
import javax.lang.model.type.TypeMirror;
35-
import javax.lang.model.type.TypeVisitor;
36-
import javax.lang.model.util.SimpleTypeVisitor8;
3738

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

55+
// Keep a cache from class name to OptionalKind for quick look-up.
56+
private static final ImmutableMap<ClassName, OptionalKind> OPTIONAL_KIND_BY_CLASS_NAME =
57+
valuesOf(OptionalKind.class)
58+
.collect(toImmutableMap(value -> value.className, value -> value));
59+
5460
private final ClassName className;
5561
private final String absentMethodName;
5662

@@ -59,6 +65,14 @@ public enum OptionalKind {
5965
this.absentMethodName = absentMethodName;
6066
}
6167

68+
private static boolean isOptionalKind(TypeElement type) {
69+
return OPTIONAL_KIND_BY_CLASS_NAME.containsKey(ClassName.get(type));
70+
}
71+
72+
private static OptionalKind of(TypeElement type) {
73+
return OPTIONAL_KIND_BY_CLASS_NAME.get(ClassName.get(type));
74+
}
75+
6276
/** Returns {@code valueType} wrapped in the correct class. */
6377
public ParameterizedTypeName of(TypeName valueType) {
6478
return ParameterizedTypeName.get(className, valueType);
@@ -90,20 +104,6 @@ public CodeBlock presentObjectExpression(CodeBlock value) {
90104
}
91105
}
92106

93-
private static final TypeVisitor<Optional<OptionalKind>, Void> OPTIONAL_KIND =
94-
new SimpleTypeVisitor8<Optional<OptionalKind>, Void>(Optional.empty()) {
95-
@Override
96-
public Optional<OptionalKind> visitDeclared(DeclaredType t, Void p) {
97-
for (OptionalKind optionalKind : OptionalKind.values()) {
98-
Name qualifiedName = MoreElements.asType(t.asElement()).getQualifiedName();
99-
if (qualifiedName.contentEquals(optionalKind.className.canonicalName())) {
100-
return Optional.of(optionalKind);
101-
}
102-
}
103-
return Optional.empty();
104-
}
105-
};
106-
107107
/**
108108
* The optional type itself, wrapped using {@link MoreTypes#equivalence()}.
109109
*
@@ -120,7 +120,7 @@ private DeclaredType declaredOptionalType() {
120120

121121
/** Which {@code Optional} type is used. */
122122
public OptionalKind kind() {
123-
return declaredOptionalType().accept(OPTIONAL_KIND, null).get();
123+
return OptionalKind.of(asTypeElement(declaredOptionalType()));
124124
}
125125

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

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

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

0 commit comments

Comments
 (0)