Skip to content

Commit

Permalink
[javac] Initial cleanup of method descriptor creation.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 700167424
  • Loading branch information
rluble authored and copybara-github committed Nov 26, 2024
1 parent bb7802b commit 7ef2124
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ private Variable createVariable(JCVariableDecl variableDeclaration, boolean isPa
}

private Method.Builder newMethodBuilder(ExecutableElement methodElement) {
MethodDescriptor methodDescriptor =
environment.createDeclarationMethodDescriptor(methodElement);
MethodDescriptor methodDescriptor = environment.createMethodDescriptor(methodElement);
return Method.newBuilder().setMethodDescriptor(methodDescriptor);
}

Expand Down Expand Up @@ -871,14 +870,9 @@ private Expression convertMemberReference(JCMemberReference memberReference) {
.build();
}

com.sun.tools.javac.code.Type returnType =
methodSymbol.isConstructor()
? methodSymbol.getEnclosingElement().asType()
: memberReference.referentType.getReturnType();
MethodDescriptor targetMethodDescriptor =
environment.createMethodDescriptor(
/* methodType= */ (ExecutableType) memberReference.referentType,
/* returnType= */ returnType,
/* methodType= */ memberReference.referentType.asMethodType(),
/* declarationMethodElement= */ methodSymbol);
Expression qualifier = convertExpressionOrNull(memberReference.getQualifierExpression());
if (qualifier instanceof JavaScriptConstructorReference) {
Expand Down Expand Up @@ -985,14 +979,14 @@ private Expression convertNewClass(JCNewClass expression) {
? convertClassDeclaration(expression.getClassBody(), expression)
: null;

MethodSymbol constructorBinding = (MethodSymbol) expression.constructor;
MethodSymbol constructorElement = (MethodSymbol) expression.constructor;
DeclaredTypeDescriptor targetType = environment.createDeclaredTypeDescriptor(expression.type);
MethodDescriptor constructorMethodDescriptor =
environment.createMethodDescriptor(
/* enclosingTypeDescriptor= */ targetType,
/* methodElement= */ (MethodSymbol)
constructorBinding.asMemberOf(expression.type, environment.internalTypes),
/* declarationMethodElement= */ constructorBinding);
/* methodType= */ (ExecutableType)
constructorElement.asMemberOf(expression.type, environment.internalTypes).asType(),
/* declarationMethodElement= */ constructorElement);
Expression qualifier = convertExpressionOrNull(expression.getEnclosingExpression());
List<Expression> arguments =
convertArguments(constructorMethodDescriptor, expression.getArguments());
Expand Down Expand Up @@ -1033,13 +1027,12 @@ private Expression convertMethodInvocation(JCMethodInvocation methodInvocation)

MethodDescriptor methodDescriptor =
environment.createMethodDescriptor(
/* methodType= */ (ExecutableType) methodInvocation.getMethodSelect().type,
/* returnType= */ methodInvocation.type,
/* methodType= */ methodInvocation.getMethodSelect().type.asMethodType(),
/* declarationMethodElement= */ methodSymbol);

if (methodDescriptor.isConstructor()
&& methodDescriptor.isMemberOf(TypeDescriptors.get().javaLangEnum)) {
// Fix inconsitencies in calls to JRE's Enum constructor calls. Enum constructor has 2
// Fix inconsistencies in calls to JRE's Enum constructor calls. Enum constructor has 2
// implicit parameters (name and ordinal) that are added by a normalization pass. This removes
// the parameter definition from the descriptor so that they are consistent.
checkArgument(
Expand Down Expand Up @@ -1127,7 +1120,7 @@ private Expression convertIdent(JCIdent identifier) {
}

VarSymbol varSymbol = (VarSymbol) symbol;
if (symbol.getKind() == ElementKind.FIELD) {
if (symbol.getKind() == ElementKind.FIELD || symbol.getKind() == ElementKind.ENUM_CONSTANT) {
FieldDescriptor fieldDescriptor =
environment.createFieldDescriptor(varSymbol, identifier.type);
return FieldAccess.newBuilder().setTarget(fieldDescriptor).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,16 +579,23 @@ FieldDescriptor createFieldDescriptor(VariableElement variableElement, TypeMirro
*
* @param methodType an ExecutableType containing the (inferred) specialization of the method in a
* usage location.
* @param returnType the (inferred) specialized return type.
* @param declarationMethodElement the method declaration.
*/
MethodDescriptor createMethodDescriptor(
ExecutableType methodType, Type returnType, ExecutableElement declarationMethodElement) {
ExecutableType methodType, ExecutableElement declarationMethodElement) {

DeclaredTypeDescriptor enclosingTypeDescriptor =
createDeclaredTypeDescriptor(declarationMethodElement.getEnclosingElement().asType());
return createMethodDescriptor(enclosingTypeDescriptor, methodType, declarationMethodElement);
}

/** Create a MethodDescriptor directly based on the given JavaC ExecutableElement. */
MethodDescriptor createMethodDescriptor(
DeclaredTypeDescriptor enclosingTypeDescriptor,
ExecutableType methodType,
ExecutableElement declarationMethodElement) {

// TODO(b/380911302): Remove redundance in the creation of method descriptors.
// TODO(b/380911302): Remove redundancy in the creation of method descriptors.
// The enclosing type descriptor might be a subclass of the actual type descriptor, hence
// traverse the supertypes to find the actual enclosing type descriptor without loosing the
// parameterization.
Expand All @@ -601,60 +608,14 @@ MethodDescriptor createMethodDescriptor(
.findFirst()
.get();

MethodDescriptor declarationMethodDescriptor = null;
List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
if (isSpecialized(
enclosingTypeDescriptor, declarationMethodElement, parameterTypes, returnType)) {
declarationMethodDescriptor = createDeclarationMethodDescriptor(declarationMethodElement);
}

TypeDescriptor returnTypeDescriptor =
applyReturnTypeNullabilityAnnotations(
createTypeDescriptorWithNullability(
returnType,
declarationMethodElement.getAnnotationMirrors(),
enclosingTypeDescriptor.getTypeDeclaration().isNullMarked()),
declarationMethodElement);

ImmutableList.Builder<TypeDescriptor> parametersBuilder = ImmutableList.builder();
for (int i = 0; i < parameterTypes.size(); i++) {
parametersBuilder.add(
applyParameterNullabilityAnnotations(
createTypeDescriptorWithNullability(
parameterTypes.get(i),
declarationMethodElement.getParameters().get(i).getAnnotationMirrors(),
enclosingTypeDescriptor.getTypeDeclaration().isNullMarked()),
declarationMethodElement,
i));
}

// generate type parameters declared in the method.
return createDeclaredMethodDescriptor(
enclosingTypeDescriptor.toNullable(),
declarationMethodElement,
declarationMethodDescriptor,
parametersBuilder.build(),
returnTypeDescriptor);
}

/** Create a MethodDescriptor directly based on the given JavaC ExecutableElement. */
MethodDescriptor createMethodDescriptor(
DeclaredTypeDescriptor enclosingTypeDescriptor,
ExecutableElement methodElement,
ExecutableElement declarationMethodElement) {

MethodDescriptor declarationMethodDescriptor = null;

ImmutableList<TypeMirror> parameters =
methodElement.getParameters().stream()
.map(VariableElement::asType)
.collect(toImmutableList());
methodType.getParameterTypes().stream().collect(toImmutableList());

TypeMirror returnType = methodElement.getReturnType();
TypeMirror returnType = methodType.getReturnType();
if (isSpecialized(enclosingTypeDescriptor, declarationMethodElement, parameters, returnType)) {
declarationMethodDescriptor =
createDeclarationMethodDescriptor(
declarationMethodElement, enclosingTypeDescriptor.getDeclarationDescriptor());
declarationMethodDescriptor = createMethodDescriptor(declarationMethodElement);
}

TypeDescriptor returnTypeDescriptor =
Expand Down Expand Up @@ -686,16 +647,11 @@ MethodDescriptor createMethodDescriptor(
}

/** Create a MethodDescriptor directly based on the given JavaC ExecutableElement. */
MethodDescriptor createDeclarationMethodDescriptor(ExecutableElement methodElement) {
MethodDescriptor createMethodDescriptor(ExecutableElement methodElement) {
DeclaredTypeDescriptor enclosingTypeDescriptor =
createDeclaredTypeDescriptor(methodElement.getEnclosingElement().asType());
return createDeclarationMethodDescriptor(methodElement, enclosingTypeDescriptor);
}

/** Create a MethodDescriptor directly based on the given JavaC ExecutableElement. */
MethodDescriptor createDeclarationMethodDescriptor(
ExecutableElement methodElement, DeclaredTypeDescriptor enclosingTypeDescriptor) {
return createMethodDescriptor(enclosingTypeDescriptor, methodElement, methodElement);
return createMethodDescriptor(
enclosingTypeDescriptor, (ExecutableType) methodElement.asType(), methodElement);
}


Expand Down Expand Up @@ -1181,7 +1137,7 @@ TypeDeclaration createDeclarationForType(final TypeElement typeElement) {
|| element.getKind() == ElementKind.CONSTRUCTOR)
.map(MethodSymbol.class::cast)
.collect(toImmutableList())) {
MethodDescriptor methodDescriptor = createDeclarationMethodDescriptor(methodElement);
MethodDescriptor methodDescriptor = createMethodDescriptor(methodElement);
listBuilder.add(methodDescriptor);
}
return listBuilder.build();
Expand All @@ -1196,8 +1152,7 @@ TypeDeclaration createDeclarationForType(final TypeElement typeElement) {
// Get the actual abstract method from the frontend; which will return the unparameterized
// declaration possibly from a supertype.
var declaration =
createDeclarationMethodDescriptor(
getFunctionalInterfaceMethodDecl(typeElement.asType()));
createMethodDescriptor(getFunctionalInterfaceMethodDecl(typeElement.asType()));

if (declaration == null) {
return null;
Expand Down

0 comments on commit 7ef2124

Please sign in to comment.