forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
De-dupe assisted parameter names and field names to avoid naming conf…
…licts. Fixes #2570 RELNOTES=De-dupe assisted parameter names and field names to avoid naming conflicts. #2570 PiperOrigin-RevId: 382425214
- Loading branch information
1 parent
7af40df
commit 3d93625
Showing
10 changed files
with
743 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
java/dagger/internal/codegen/writing/AssistedInjectionParameters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (C) 2017 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.internal.codegen.writing; | ||
|
||
import static com.google.auto.common.MoreElements.asExecutable; | ||
import static com.google.auto.common.MoreTypes.asDeclared; | ||
import static com.google.auto.common.MoreTypes.asExecutable; | ||
import static com.google.common.base.Preconditions.checkArgument; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.squareup.javapoet.ParameterSpec; | ||
import com.squareup.javapoet.TypeName; | ||
import dagger.internal.codegen.binding.AssistedInjectionAnnotations; | ||
import dagger.internal.codegen.binding.Binding; | ||
import dagger.internal.codegen.langmodel.DaggerTypes; | ||
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation; | ||
import dagger.spi.model.BindingKind; | ||
import java.util.List; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.type.ExecutableType; | ||
import javax.lang.model.type.TypeMirror; | ||
|
||
/** Utility class for generating unique assisted parameter names for a component shard. */ | ||
final class AssistedInjectionParameters { | ||
/** | ||
* Returns the list of assisted parameters as {@link ParameterSpec}s. | ||
* | ||
* <p>The type of each parameter will be the resolved type given by the binding key, and the name | ||
* of each parameter will be the name given in the {@link | ||
* dagger.assisted.AssistedInject}-annotated constructor. | ||
*/ | ||
public static ImmutableList<ParameterSpec> assistedParameterSpecs( | ||
Binding binding, DaggerTypes types, ShardImplementation shardImplementation) { | ||
checkArgument(binding.kind() == BindingKind.ASSISTED_INJECTION); | ||
ExecutableElement constructor = asExecutable(binding.bindingElement().get()); | ||
ExecutableType constructorType = | ||
asExecutable(types.asMemberOf(asDeclared(binding.key().type().java()), constructor)); | ||
return assistedParameterSpecs( | ||
constructor.getParameters(), constructorType.getParameterTypes(), shardImplementation); | ||
} | ||
|
||
private static ImmutableList<ParameterSpec> assistedParameterSpecs( | ||
List<? extends VariableElement> paramElements, | ||
List<? extends TypeMirror> paramTypes, | ||
ShardImplementation shardImplementation) { | ||
ImmutableList.Builder<ParameterSpec> assistedParameterSpecs = ImmutableList.builder(); | ||
for (int i = 0; i < paramElements.size(); i++) { | ||
VariableElement paramElement = paramElements.get(i); | ||
TypeMirror paramType = paramTypes.get(i); | ||
if (AssistedInjectionAnnotations.isAssistedParameter(paramElement)) { | ||
assistedParameterSpecs.add( | ||
ParameterSpec.builder( | ||
TypeName.get(paramType), | ||
shardImplementation.getUniqueFieldNameForAssistedParam(paramElement)) | ||
.build()); | ||
} | ||
} | ||
return assistedParameterSpecs.build(); | ||
} | ||
|
||
private AssistedInjectionParameters() {} | ||
} |
125 changes: 125 additions & 0 deletions
125
java/dagger/internal/codegen/writing/AssistedPrivateMethodBindingExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (C) 2021 The Dagger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dagger.internal.codegen.writing; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.squareup.javapoet.MethodSpec.methodBuilder; | ||
import static dagger.internal.codegen.binding.AssistedInjectionAnnotations.assistedParameterSpecs; | ||
import static dagger.internal.codegen.javapoet.CodeBlocks.parameterNames; | ||
import static dagger.internal.codegen.writing.AssistedInjectionParameters.assistedParameterSpecs; | ||
import static dagger.internal.codegen.writing.ComponentImplementation.MethodSpecKind.PRIVATE_METHOD; | ||
import static javax.lang.model.element.Modifier.PRIVATE; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.CodeBlock; | ||
import com.squareup.javapoet.TypeName; | ||
import dagger.assisted.Assisted; | ||
import dagger.assisted.AssistedFactory; | ||
import dagger.assisted.AssistedInject; | ||
import dagger.internal.codegen.binding.BindingRequest; | ||
import dagger.internal.codegen.binding.ContributionBinding; | ||
import dagger.internal.codegen.compileroption.CompilerOptions; | ||
import dagger.internal.codegen.javapoet.Expression; | ||
import dagger.internal.codegen.langmodel.DaggerTypes; | ||
import dagger.internal.codegen.writing.ComponentImplementation.ShardImplementation; | ||
import dagger.spi.model.BindingKind; | ||
import dagger.spi.model.RequestKind; | ||
import javax.lang.model.type.TypeMirror; | ||
|
||
/** A binding expression that wraps private method call for assisted fatory creation. */ | ||
final class AssistedPrivateMethodBindingExpression extends MethodBindingExpression { | ||
private final ShardImplementation shardImplementation; | ||
private final ContributionBinding binding; | ||
private final BindingRequest request; | ||
private final BindingExpression wrappedBindingExpression; | ||
private final CompilerOptions compilerOptions; | ||
private final DaggerTypes types; | ||
private String methodName; | ||
|
||
@AssistedInject | ||
AssistedPrivateMethodBindingExpression( | ||
@Assisted BindingRequest request, | ||
@Assisted ContributionBinding binding, | ||
@Assisted BindingExpression wrappedBindingExpression, | ||
ComponentImplementation componentImplementation, | ||
DaggerTypes types, | ||
CompilerOptions compilerOptions) { | ||
super(componentImplementation.shardImplementation(binding), types); | ||
checkArgument(binding.kind() == BindingKind.ASSISTED_INJECTION); | ||
checkArgument(request.requestKind() == RequestKind.INSTANCE); | ||
this.binding = checkNotNull(binding); | ||
this.request = checkNotNull(request); | ||
this.wrappedBindingExpression = checkNotNull(wrappedBindingExpression); | ||
this.shardImplementation = componentImplementation.shardImplementation(binding); | ||
this.compilerOptions = compilerOptions; | ||
this.types = types; | ||
} | ||
|
||
Expression getAssistedDependencyExpression(ClassName requestingClass) { | ||
return Expression.create( | ||
returnType(), | ||
requestingClass.equals(shardImplementation.name()) | ||
? CodeBlock.of( | ||
"$N($L)", methodName(), parameterNames(assistedParameterSpecs(binding, types))) | ||
: CodeBlock.of( | ||
"$L.$N($L)", | ||
shardImplementation.shardFieldReference(), | ||
methodName(), | ||
parameterNames(assistedParameterSpecs(binding, types)))); | ||
} | ||
|
||
@Override | ||
protected CodeBlock methodCall() { | ||
throw new IllegalStateException("This should not be accessed"); | ||
} | ||
|
||
@Override | ||
protected TypeMirror returnType() { | ||
return types.accessibleType(binding.contributedType(), shardImplementation.name()); | ||
} | ||
|
||
private String methodName() { | ||
if (methodName == null) { | ||
// Have to set methodName field before implementing the method in order to handle recursion. | ||
methodName = shardImplementation.getUniqueMethodName(request); | ||
|
||
// TODO(bcorso): Fix the order that these generated methods are written to the component. | ||
shardImplementation.addMethod( | ||
PRIVATE_METHOD, | ||
methodBuilder(methodName) | ||
.addModifiers(PRIVATE) | ||
.addParameters(assistedParameterSpecs(binding, types, shardImplementation)) | ||
.returns(TypeName.get(returnType())) | ||
.addStatement( | ||
"return $L", | ||
wrappedBindingExpression | ||
.getDependencyExpression(shardImplementation.name()) | ||
.codeBlock()) | ||
.build()); | ||
} | ||
return methodName; | ||
} | ||
|
||
@AssistedFactory | ||
static interface Factory { | ||
AssistedPrivateMethodBindingExpression create( | ||
BindingRequest request, | ||
ContributionBinding binding, | ||
BindingExpression wrappedBindingExpression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.