Skip to content

Commit

Permalink
Remove dead code in InjectionMethods.
Browse files Browse the repository at this point in the history
This code has never been executed since its introduction in CL/164502837 (in 2017) because it is gated on an impossible conditional statement. We could correct this conditional to what it probably should have been, but this seems to just add unnecessary (and sometimes invalid) casts to the generated code, so I think it's best to just delete it.

RELNOTES=N/A
PiperOrigin-RevId: 398351534
  • Loading branch information
bcorso authored and Dagger Team committed Sep 22, 2021
1 parent 7fc798d commit 5964681
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
51 changes: 3 additions & 48 deletions java/dagger/internal/codegen/writing/InjectionMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
import static com.google.common.base.Preconditions.checkArgument;
import static com.squareup.javapoet.MethodSpec.methodBuilder;
import static dagger.internal.codegen.base.RequestKinds.requestTypeName;
import static dagger.internal.codegen.binding.ConfigurationAnnotations.getNullableType;
import static dagger.internal.codegen.binding.SourceFiles.generatedClassNameForBinding;
import static dagger.internal.codegen.binding.SourceFiles.memberInjectedFieldSignatureForVariable;
Expand Down Expand Up @@ -67,7 +66,6 @@
import dagger.internal.codegen.langmodel.DaggerTypes;
import dagger.spi.model.DaggerAnnotation;
import dagger.spi.model.DependencyRequest;
import dagger.spi.model.RequestKind;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -77,7 +75,6 @@
import javax.lang.model.element.Parameterizable;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/** Convenience methods for creating and invoking {@link InjectionMethod}s. */
Expand Down Expand Up @@ -150,7 +147,7 @@ static CodeBlock invoke(
KotlinMetadataUtil metadataUtil) {
ImmutableList.Builder<CodeBlock> arguments = ImmutableList.builder();
moduleReference.ifPresent(arguments::add);
invokeArguments(binding, dependencyUsage, uniqueAssistedParameterName, requestingClass)
invokeArguments(binding, dependencyUsage, uniqueAssistedParameterName)
.forEach(arguments::add);

ClassName enclosingClass = generatedClassNameForBinding(binding);
Expand All @@ -161,8 +158,7 @@ static CodeBlock invoke(
static ImmutableList<CodeBlock> invokeArguments(
ProvisionBinding binding,
Function<DependencyRequest, CodeBlock> dependencyUsage,
Function<VariableElement, String> uniqueAssistedParameterName,
ClassName requestingClass) {
Function<VariableElement, String> uniqueAssistedParameterName) {
ImmutableMap<VariableElement, DependencyRequest> dependencyRequestMap =
binding.provisionDependencies().stream()
.collect(
Expand All @@ -177,8 +173,7 @@ static ImmutableList<CodeBlock> invokeArguments(
arguments.add(CodeBlock.of("$L", uniqueAssistedParameterName.apply(parameter)));
} else if (dependencyRequestMap.containsKey(parameter)) {
DependencyRequest request = dependencyRequestMap.get(parameter);
arguments.add(
injectionMethodArgument(request, dependencyUsage.apply(request), requestingClass));
arguments.add(dependencyUsage.apply(request));
} else {
throw new AssertionError("Unexpected parameter: " + parameter);
}
Expand Down Expand Up @@ -376,46 +371,6 @@ private static String methodName(InjectionSite injectionSite) {
}
}

private static CodeBlock injectionMethodArgument(
DependencyRequest dependency, CodeBlock argument, ClassName generatedTypeName) {
TypeMirror keyType = dependency.key().type().java();
CodeBlock.Builder codeBlock = CodeBlock.builder();
if (!isRawTypeAccessible(keyType, generatedTypeName.packageName())
&& isTypeAccessibleFrom(keyType, generatedTypeName.packageName())) {
if (!dependency.kind().equals(RequestKind.INSTANCE)) {
TypeName usageTypeName = accessibleType(dependency);
codeBlock.add("($T) ($T)", usageTypeName, rawTypeName(usageTypeName));
} else if (dependency.requestElement().get().java().asType().getKind().equals(
TypeKind.TYPEVAR)) {
codeBlock.add("($T)", keyType);
}
}
return codeBlock.add(argument).build();
}

/**
* Returns the parameter type for {@code dependency}. If the raw type is not accessible, returns
* {@link Object}.
*/
private static TypeName accessibleType(DependencyRequest dependency) {
TypeName typeName =
requestTypeName(dependency.kind(), accessibleType(dependency.key().type().java()));
return dependency
.requestElement()
.map(element -> element.java().asType().getKind().isPrimitive())
.orElse(false)
? typeName.unbox()
: typeName;
}

/**
* Returns the accessible type for {@code type}. If the raw type is not accessible, returns {@link
* Object}.
*/
private static TypeName accessibleType(TypeMirror type) {
return isRawTypePubliclyAccessible(type) ? TypeName.get(type) : TypeName.OBJECT;
}

private enum InstanceCastPolicy {
CAST_IF_NOT_PUBLIC, IGNORE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ private Expression invokeMethod(ClassName requestingClass) {
ProvisionMethod.invokeArguments(
provisionBinding,
request -> dependencyArgument(request, requestingClass).codeBlock(),
shardImplementation::getUniqueFieldNameForAssistedParam,
requestingClass));
shardImplementation::getUniqueFieldNameForAssistedParam));
ExecutableElement method = asExecutable(provisionBinding.bindingElement().get());
CodeBlock invocation;
switch (method.getKind()) {
Expand Down

0 comments on commit 5964681

Please sign in to comment.