From bd991cde3f8d295d9c2d716d8e46c0cebf776b39 Mon Sep 17 00:00:00 2001 From: Roberto Lublinerman Date: Mon, 16 Dec 2024 08:52:15 -0800 Subject: [PATCH] [javac] Improve construction of type variables. PiperOrigin-RevId: 706725562 --- .../frontend/javac/JavaEnvironment.java | 45 ++++++++++++------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/transpiler/java/com/google/j2cl/transpiler/frontend/javac/JavaEnvironment.java b/transpiler/java/com/google/j2cl/transpiler/frontend/javac/JavaEnvironment.java index bf404d7721..c7c57e2080 100644 --- a/transpiler/java/com/google/j2cl/transpiler/frontend/javac/JavaEnvironment.java +++ b/transpiler/java/com/google/j2cl/transpiler/frontend/javac/JavaEnvironment.java @@ -395,26 +395,37 @@ private TypeVariable createTypeVariable( javax.lang.model.type.TypeVariable typeVariable, List elementAnnotations, boolean inNullMarkedScope) { - if (typeVariable instanceof CapturedType) { - return createWildcardTypeVariable( - typeVariable.getUpperBound(), typeVariable.getLowerBound(), inNullMarkedScope); - } + boolean isCapture = typeVariable instanceof CapturedType; Supplier boundTypeDescriptorFactory = () -> createTypeDescriptor(typeVariable.getUpperBound(), inNullMarkedScope); + TypeDescriptor lowerBound = + typeVariable.getLowerBound() != null + && typeVariable.getLowerBound().getKind() != TypeKind.NULL + ? createTypeDescriptor(typeVariable.getLowerBound(), inNullMarkedScope) + : null; + List classComponents = getClassComponents(typeVariable); KtVariance ktVariance = getKtVariance(((TypeVariableSymbol) typeVariable.asElement()).baseSymbol()); + return TypeVariable.newBuilder() .setUpperBoundTypeDescriptorFactory(boundTypeDescriptorFactory) + .setLowerBoundTypeDescriptor(lowerBound) .setUniqueKey( - String.join("::", classComponents) + (isCapture ? "capture of " : "") + + String.join("::", classComponents) + (typeVariable.getUpperBound() != null - ? typeVariable.getUpperBound().toString() + ? "::^::" + typeVariable.getUpperBound() + : "") + + (typeVariable.getLowerBound() != null + ? "::v::" + typeVariable.getLowerBound() : "")) .setKtVariance(ktVariance) - .setName(typeVariable.asElement().getSimpleName().toString()) + .setName( + ((TypeVariableSymbol) typeVariable.asElement()).baseSymbol().getSimpleName().toString()) + .setCapture(isCapture) .setNullabilityAnnotation(getNullabilityAnnotation(typeVariable, elementAnnotations)) .build(); } @@ -423,15 +434,14 @@ private TypeVariable createWildcardTypeVariable( @Nullable TypeMirror upperBound, @Nullable TypeMirror lowerBound, boolean inNullMarkedScope) { return TypeVariable.newBuilder() .setUpperBoundTypeDescriptorFactory( - () -> createTypeDescriptor(upperBound, inNullMarkedScope)) + () -> lowerBound == null ? createTypeDescriptor(upperBound, inNullMarkedScope) : null) .setLowerBoundTypeDescriptor(createTypeDescriptor(lowerBound, inNullMarkedScope)) .setWildcard(true) .setName("?") .setUniqueKey( - "::^::" - + (upperBound != null ? upperBound.toString() : "") - + "::v::" - + (lowerBound != null ? lowerBound.toString() : "")) + (inNullMarkedScope ? "+" : "-") + + (upperBound != null ? "::^::" + upperBound : "") + + (lowerBound != null ? "::v::" + lowerBound : "")) .build(); } @@ -838,9 +848,14 @@ private MethodDescriptor createDeclaredMethodDescriptor( List typeArguments) { ImmutableList typeParameterTypeDescriptors = declarationMethodElement.getTypeParameters().stream() - .map(Element::asType) - .map(this::createTypeDescriptor) - .map(TypeVariable.class::cast) + .map(TypeParameterElement::asType) + .map(javax.lang.model.type.TypeVariable.class::cast) + .map( + tv -> + createTypeVariable( + tv, + ImmutableList.of(), + enclosingTypeDescriptor.getTypeDeclaration().isNullMarked())) .collect(toImmutableList()); boolean isStatic = isStatic(declarationMethodElement);