Skip to content

Commit

Permalink
[javac] Improve construction of type variables.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 706725562
  • Loading branch information
rluble authored and copybara-github committed Dec 16, 2024
1 parent 6328c55 commit bd991cd
Showing 1 changed file with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -395,26 +395,37 @@ private TypeVariable createTypeVariable(
javax.lang.model.type.TypeVariable typeVariable,
List<? extends AnnotationMirror> elementAnnotations,
boolean inNullMarkedScope) {
if (typeVariable instanceof CapturedType) {
return createWildcardTypeVariable(
typeVariable.getUpperBound(), typeVariable.getLowerBound(), inNullMarkedScope);
}
boolean isCapture = typeVariable instanceof CapturedType;

Supplier<TypeDescriptor> boundTypeDescriptorFactory =
() -> createTypeDescriptor(typeVariable.getUpperBound(), inNullMarkedScope);

TypeDescriptor lowerBound =
typeVariable.getLowerBound() != null
&& typeVariable.getLowerBound().getKind() != TypeKind.NULL
? createTypeDescriptor(typeVariable.getLowerBound(), inNullMarkedScope)
: null;

List<String> 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();
}
Expand All @@ -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();
}

Expand Down Expand Up @@ -838,9 +848,14 @@ private MethodDescriptor createDeclaredMethodDescriptor(
List<TypeDescriptor> typeArguments) {
ImmutableList<TypeVariable> 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);
Expand Down

0 comments on commit bd991cd

Please sign in to comment.