Skip to content

Commit

Permalink
[javac] Populate member types in TypeDeclaration.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 724099608
  • Loading branch information
rluble authored and copybara-github committed Feb 7, 2025
1 parent 682a149 commit 224899d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Type createType(ClassSymbol typeElement, JCTree sourcePositionNode) {
if (typeElement == null) {
return null;
}
TypeDeclaration typeDeclaration = environment.createDeclarationForType(typeElement);
TypeDeclaration typeDeclaration = environment.createTypeDeclaration(typeElement);

return new Type(
typeDeclaration.isAnonymous()
Expand Down Expand Up @@ -970,12 +970,12 @@ private Expression convertFieldAccess(JCFieldAccess fieldAccess) {

if (fieldAccess.name.contentEquals("this")) {
return new ThisReference(
environment.createDeclarationForType((ClassSymbol) expression.type.tsym).toDescriptor(),
environment.createTypeDeclaration((ClassSymbol) expression.type.tsym).toDescriptor(),
/* isQualified= */ true);
}
if (fieldAccess.name.contentEquals("super")) {
DeclaredTypeDescriptor typeDescriptor =
environment.createDeclarationForType((ClassSymbol) expression.type.tsym).toDescriptor();
environment.createTypeDeclaration((ClassSymbol) expression.type.tsym).toDescriptor();

boolean isQualified = !typeDescriptor.isInterface();
if (isQualified) {
Expand Down Expand Up @@ -1215,7 +1215,7 @@ private Expression convertIdent(JCIdent identifier) {
Symbol symbol = identifier.sym;
if (symbol instanceof ClassSymbol) {
return new JavaScriptConstructorReference(
environment.createDeclarationForType((ClassSymbol) identifier.sym));
environment.createTypeDeclaration((ClassSymbol) identifier.sym));
}
if (symbol instanceof MethodSymbol) {
throw new AssertionError("Unexpected symbol class: " + symbol.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ DeclaredTypeDescriptor createDeclaredTypeDescriptor(
return createTypeDescriptor(typeMirror, inNullMarkedScope, DeclaredTypeDescriptor.class);
}

/** Creates a specific subclass of TypeDescriptor from a TypeMirror. */
<T extends TypeDescriptor> T createTypeDescriptor(TypeMirror typeMirror, Class<T> clazz) {
return createTypeDescriptor(typeMirror, /* inNullMarkedScope= */ false, clazz);
}

/** Creates a specific subclass of TypeDescriptor from a TypeMirror. */
<T extends TypeDescriptor> T createTypeDescriptor(
TypeMirror typeMirror, boolean inNullMarkedScope, Class<T> clazz) {
Expand Down Expand Up @@ -455,16 +450,6 @@ private static DeclaredTypeDescriptor withNullability(
return nullable ? typeDescriptor.toNullable() : typeDescriptor.toNonNullable();
}

/**
* In case the given type element is nested, return the outermost possible enclosing type element.
*/
private static TypeElement toTopLevelTypeBinding(Element element) {
if (element.getEnclosingElement().getKind() == ElementKind.PACKAGE) {
return (TypeElement) element;
}
return toTopLevelTypeBinding(element.getEnclosingElement());
}

private ImmutableList<String> getClassComponents(
javax.lang.model.type.TypeVariable typeVariable) {
Element enclosingElement = typeVariable.asElement().getEnclosingElement();
Expand Down Expand Up @@ -1048,7 +1033,7 @@ private DeclaredTypeDescriptor createDeclaredType(

TypeElement typeElement = (TypeElement) classType.asElement();
DeclaredTypeDescriptor typeDescriptor =
createDeclarationForType(typeElement)
createTypeDeclaration(typeElement)
.toDescriptor(
createTypeArgumentDescriptors(
getTypeArguments(classType),
Expand Down Expand Up @@ -1198,7 +1183,7 @@ private static Kind getKindFromTypeBinding(TypeElement typeElement) {
}

@Nullable
TypeDeclaration createDeclarationForType(final TypeElement typeElement) {
TypeDeclaration createTypeDeclaration(final TypeElement typeElement) {
if (typeElement == null) {
return null;
}
Expand Down Expand Up @@ -1270,7 +1255,7 @@ TypeDeclaration createDeclarationForType(final TypeElement typeElement) {
boolean isNullMarked = isNullMarked(typeElement);
return TypeDeclaration.newBuilder()
.setClassComponents(getClassComponents(typeElement))
.setEnclosingTypeDeclaration(createDeclarationForType(getEnclosingType(typeElement)))
.setEnclosingTypeDeclaration(createTypeDeclaration(getEnclosingType(typeElement)))
.setEnclosingMethodDescriptorFactory(() -> getEnclosingMethodDescriptor(typeElement))
.setSuperTypeDescriptorFactory(
() ->
Expand Down Expand Up @@ -1330,6 +1315,13 @@ TypeDeclaration createDeclarationForType(final TypeElement typeElement) {
.setDeclaredMethodDescriptorsFactory(declaredMethods)
.setSingleAbstractMethodDescriptorFactory(singleAbstractMethod)
.setDeclaredFieldDescriptorsFactory(declaredFields)
.setMemberTypeDeclarationsFactory(
() ->
typeElement.getEnclosedElements().stream()
.filter(TypeElement.class::isInstance)
.map(TypeElement.class::cast)
.map(this::createTypeDeclaration)
.collect(toImmutableList()))
.setUnusableByJsSuppressed(JsInteropAnnotationUtils.isUnusableByJsSuppressed(typeElement))
.setDeprecated(isDeprecated(typeElement))
.build();
Expand Down

0 comments on commit 224899d

Please sign in to comment.