Skip to content

Commit f0d829e

Browse files
rlublecopybara-github
authored andcommitted
[J2KT] Replace custom zip with Streams.zip in passes.
Replace the custom `zip` implementation in `AbstractJ2ktNormalizationPass` with `Streams.zip` as per TODO. PiperOrigin-RevId: 926682016
1 parent ed9a341 commit f0d829e

4 files changed

Lines changed: 41 additions & 52 deletions

File tree

transpiler/java/com/google/j2cl/transpiler/passes/AbstractJ2ktNormalizationPass.java

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
*/
1616
package com.google.j2cl.transpiler.passes;
1717

18-
import static com.google.common.base.Preconditions.checkArgument;
1918
import static com.google.common.collect.ImmutableList.toImmutableList;
2019

2120
import com.google.common.collect.ImmutableList;
2221
import com.google.common.collect.ImmutableSet;
22+
import com.google.common.collect.Streams;
2323
import com.google.errorprone.annotations.FormatMethod;
2424
import com.google.j2cl.common.SourcePosition;
2525
import com.google.j2cl.transpiler.ast.ArrayTypeDescriptor;
@@ -32,8 +32,6 @@
3232
import com.google.j2cl.transpiler.ast.TypeDescriptors;
3333
import com.google.j2cl.transpiler.ast.TypeVariable;
3434
import com.google.j2cl.transpiler.ast.UnionTypeDescriptor;
35-
import java.util.List;
36-
import java.util.function.BiFunction;
3735

3836
/**
3937
* Abstract base class for Kotlin passes, providing shared functionality:
@@ -90,16 +88,13 @@ private static TypeDescriptor projectCaptures(
9088
seen));
9189

9290
case DeclaredTypeDescriptor descriptor ->
93-
descriptor.isRaw()
94-
? descriptor
95-
: descriptor.withTypeArguments(
96-
zip(
97-
typeDescriptor.isRaw()
98-
? ImmutableList.of()
99-
: descriptor.getTypeDeclaration().getTypeParameterDescriptors(),
100-
descriptor.getTypeArgumentDescriptors(),
91+
descriptor.withTypeArguments(
92+
Streams.zip(
93+
descriptor.getTypeDeclaration().getTypeParameterDescriptors().stream(),
94+
descriptor.getTypeArgumentDescriptors().stream(),
10195
(typeParameter, typeArgument) ->
102-
projectArgumentCaptures(typeArgument, typeParameter, seen)));
96+
projectArgumentCaptures(typeArgument, typeParameter, seen))
97+
.collect(toImmutableList()));
10398

10499
case TypeVariable typeVariable when !typeVariable.isWildcardOrCapture() -> typeVariable;
105100
case TypeVariable typeVariable when seen.contains(typeVariable) -> typeVariable;
@@ -185,12 +180,11 @@ static TypeVariable getArrayComponentTypeParameterDescriptor() {
185180
/** Returns type argument descriptors with nullability which satisfy their declarations. */
186181
static ImmutableList<TypeDescriptor> getTypeArgumentDescriptorsWithValidNullability(
187182
DeclaredTypeDescriptor declaredTypeDescriptor) {
188-
return declaredTypeDescriptor.isRaw()
189-
? ImmutableList.of()
190-
: zip(
191-
declaredTypeDescriptor.getTypeArgumentDescriptors(),
192-
declaredTypeDescriptor.getTypeDeclaration().getTypeParameterDescriptors(),
193-
AbstractJ2ktNormalizationPass::getTypeArgumentDescriptorWithValidNullability);
183+
return Streams.zip(
184+
declaredTypeDescriptor.getTypeArgumentDescriptors().stream(),
185+
declaredTypeDescriptor.getTypeDeclaration().getTypeParameterDescriptors().stream(),
186+
AbstractJ2ktNormalizationPass::getTypeArgumentDescriptorWithValidNullability)
187+
.collect(toImmutableList());
194188
}
195189

196190
/** Returns type argument descriptor with nullability which satisfies its declaration. */
@@ -216,19 +210,4 @@ static TypeDescriptor removeRedundantNullabilityAnnotation(TypeDescriptor typeDe
216210
}
217211
return typeDescriptor;
218212
}
219-
220-
// TODO(b/406815802): Replace all usages with `Streams.zip`
221-
static <A, B, R> ImmutableList<R> zip(
222-
List<A> listA, List<B> listB, BiFunction<? super A, ? super B, ? extends R> function) {
223-
checkArgument(
224-
listA.size() == listB.size(),
225-
"Lists are of different sizes (%s, %s)",
226-
listA.size(),
227-
listB.size());
228-
ImmutableList.Builder<R> builder = ImmutableList.builder();
229-
for (int i = 0; i < listA.size(); i++) {
230-
builder.add(function.apply(listA.get(i), listB.get(i)));
231-
}
232-
return builder.build();
233-
}
234213
}

transpiler/java/com/google/j2cl/transpiler/passes/AddDisambiguatingOverloadResolutionCastsJ2kt.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.google.j2cl.transpiler.passes;
1717

18+
import static com.google.common.collect.ImmutableList.toImmutableList;
1819
import static java.util.function.Predicate.isEqual;
1920

2021
import com.google.common.collect.Streams;
@@ -70,10 +71,12 @@ public Node rewriteInvocation(Invocation invocation) {
7071
// parameter's type.
7172
return invocation.toBuilder()
7273
.setArguments(
73-
zip(
74-
invocation.getTarget().getParameterTypeDescriptors(),
75-
invocation.getArguments(),
76-
AddDisambiguatingOverloadResolutionCastsJ2kt::castArgumentToParameterType))
74+
Streams.zip(
75+
invocation.getTarget().getParameterTypeDescriptors().stream(),
76+
invocation.getArguments().stream(),
77+
AddDisambiguatingOverloadResolutionCastsJ2kt
78+
::castArgumentToParameterType)
79+
.collect(toImmutableList()))
7780
.build();
7881
}
7982
});

transpiler/java/com/google/j2cl/transpiler/passes/PropagateNullability.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,11 @@ public Node rewriteInvocation(Invocation invocation) {
298298
// Propagate nullability from parameters into arguments, this covers the cases where the
299299
// expression is a lambda and its parameters are inferred from the surrounding context.
300300
ImmutableList<Expression> rewrittenArguments =
301-
zip(
302-
invocation.getArguments(),
303-
rewrittenMethodDescriptor.getParameterTypeDescriptors(),
304-
PropagateNullability::propagateNullabilityToExpression);
301+
Streams.zip(
302+
invocation.getArguments().stream(),
303+
rewrittenMethodDescriptor.getParameterTypeDescriptors().stream(),
304+
PropagateNullability::propagateNullabilityToExpression)
305+
.collect(toImmutableList());
305306

306307
if (rewrittenMethodDescriptor.equals(methodDescriptor)
307308
&& rewrittenArguments.equals(invocation.getArguments())) {
@@ -323,12 +324,15 @@ public Node rewriteFunctionExpression(FunctionExpression functionExpression) {
323324
ImmutableList<TypeDescriptor> typeArgumentDescriptors =
324325
toNonRawTypeDescriptor(functionalInterface).getTypeArgumentDescriptors();
325326
ImmutableList<TypeDescriptor> inferredTypeArgumentDescriptors =
326-
zip(
327-
typeParameterDescriptors,
328-
typeArgumentDescriptors,
329-
(typeParameterDescriptor, typeArgumentDescriptor) ->
330-
propagateTypeArgumentNullabilityFromReturnExpressions(
331-
typeParameterDescriptor, typeArgumentDescriptor, functionExpression));
327+
Streams.zip(
328+
typeParameterDescriptors.stream(),
329+
typeArgumentDescriptors.stream(),
330+
(typeParameterDescriptor, typeArgumentDescriptor) ->
331+
propagateTypeArgumentNullabilityFromReturnExpressions(
332+
typeParameterDescriptor,
333+
typeArgumentDescriptor,
334+
functionExpression))
335+
.collect(toImmutableList());
332336
DeclaredTypeDescriptor inferredFunctionalInterface =
333337
functionalInterface.withTypeArguments(inferredTypeArgumentDescriptors);
334338
Streams.forEachPair(

transpiler/java/com/google/j2cl/transpiler/passes/PropagateNullabilityInOverrides.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package com.google.j2cl.transpiler.passes;
1717

18+
import static com.google.common.collect.ImmutableList.toImmutableList;
19+
1820
import com.google.common.collect.Streams;
1921
import com.google.j2cl.transpiler.ast.AbstractRewriter;
2022
import com.google.j2cl.transpiler.ast.CompilationUnit;
@@ -74,11 +76,12 @@ private static MethodDescriptor propagateNullability(MethodDescriptor from, Meth
7476
specialize(parametrization, from.getReturnTypeDescriptor()),
7577
to.getReturnTypeDescriptor()))
7678
.setParameterDescriptors(
77-
zip(
78-
from.getParameterTypeDescriptors(),
79-
to.getParameterDescriptors(),
80-
(fromTd, toPd) ->
81-
propagateParameterNullability(specialize(parametrization, fromTd), toPd)))
79+
Streams.zip(
80+
from.getParameterTypeDescriptors().stream(),
81+
to.getParameterDescriptors().stream(),
82+
(fromTd, toPd) ->
83+
propagateParameterNullability(specialize(parametrization, fromTd), toPd))
84+
.collect(toImmutableList()))
8285
.build();
8386
}
8487

0 commit comments

Comments
 (0)