From 66c8e9508ba209da7924fe477232f28792b86807 Mon Sep 17 00:00:00 2001 From: Pete Bentley <44170157+prbprbprb@users.noreply.github.com> Date: Tue, 15 Oct 2024 08:30:29 +0100 Subject: [PATCH] Inner class logic fixes. (#1240) --- .../kotlin/org/conscrypt/doclet/ClassInfo.kt | 52 ++++++++++++++++++- .../org/conscrypt/doclet/ElementUtils.kt | 24 --------- .../org/conscrypt/doclet/FilterDoclet.kt | 4 +- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/api-doclet/src/main/kotlin/org/conscrypt/doclet/ClassInfo.kt b/api-doclet/src/main/kotlin/org/conscrypt/doclet/ClassInfo.kt index ada528bca..ad94abe6e 100644 --- a/api-doclet/src/main/kotlin/org/conscrypt/doclet/ClassInfo.kt +++ b/api-doclet/src/main/kotlin/org/conscrypt/doclet/ClassInfo.kt @@ -18,9 +18,12 @@ package org.conscrypt.doclet import org.conscrypt.doclet.FilterDoclet.Companion.classIndex import java.nio.file.Paths +import java.util.Locale import javax.lang.model.element.Element import javax.lang.model.element.ExecutableElement +import javax.lang.model.element.Modifier import javax.lang.model.element.TypeElement +import javax.lang.model.type.TypeMirror data class ClassInfo(val element: TypeElement) : Comparable { @@ -36,6 +39,50 @@ data class ClassInfo(val element: TypeElement) : Comparable { .map(classIndex::get) .sorted() + + private fun outerClass() = if (isInnerClass) { + classIndex.get(element.enclosingElement as TypeElement) + } else { + null + } + + fun innerName(): String = if (isInnerClass) { + "${outerClass()?.innerName()}.$simpleName" + } else { + simpleName + } + + private fun signature(): String { + val visibleModifiers = element.modifiers + .map(Modifier::toString) + .toMutableSet() + + val kind = element.kind.toString().lowercase(Locale.getDefault()) + if (kind == "interface") { + visibleModifiers.remove("abstract") + } + + val modifierString = visibleModifiers.joinToString(" ") + + val superName = superDisplayName(element.superclass) + + val interfaces = element.interfaces + .joinToString(", ") + .prefixIfNotEmpty(" implements ") + + return "$modifierString $kind ${innerName()}$superName$interfaces" + } + + private fun superDisplayName(mirror: TypeMirror): String { + val name = mirror.toString() + return when { + name == "none" || name == "java.lang.Object" -> "" + name.startsWith("java.lang.Enum") -> "" + else -> " extends $mirror " + } + } + + override fun compareTo(other: ClassInfo) = qualifiedName.compareTo(other.qualifiedName) private fun description() = html { @@ -136,7 +183,7 @@ data class ClassInfo(val element: TypeElement) : Comparable { fun generateHtml() = html { div("package-name") { text("Package: $packageName") } h1(simpleName) - pre(element.signature(), "class-signature") + pre(signature(), "class-signature") compose { description() + @@ -149,6 +196,7 @@ data class ClassInfo(val element: TypeElement) : Comparable { private fun relativePath(from: String, to: String) = Paths.get(from).parent.relativize(Paths.get(to)).toString() - } +private fun String.prefixIfNotEmpty(prefix: String): String + = if (isNotEmpty()) prefix + this else this diff --git a/api-doclet/src/main/kotlin/org/conscrypt/doclet/ElementUtils.kt b/api-doclet/src/main/kotlin/org/conscrypt/doclet/ElementUtils.kt index 0688afa9f..5739bf230 100644 --- a/api-doclet/src/main/kotlin/org/conscrypt/doclet/ElementUtils.kt +++ b/api-doclet/src/main/kotlin/org/conscrypt/doclet/ElementUtils.kt @@ -17,7 +17,6 @@ package org.conscrypt.doclet import com.sun.source.doctree.UnknownBlockTagTree -import java.util.Locale import javax.lang.model.element.Element import javax.lang.model.element.ElementKind import javax.lang.model.element.ExecutableElement @@ -90,34 +89,11 @@ fun formatType(typeMirror: TypeMirror): String { } } -fun TypeElement.signature(): String { - val modifiers = modifiers.joinToString(" ") - val kind = this.kind.toString().lowercase(Locale.getDefault()) - - val superName = superDisplayName(superclass) - - val interfaces = interfaces - .joinToString(", ") - .prefixIfNotEmpty(" implements ") - - return "$modifiers $kind $simpleName$superName$interfaces" -} - fun TypeElement.baseFileName(): String = if (enclosingElement.isType()) (enclosingElement as TypeElement).baseFileName() + "." + simpleName else qualifiedName.toString().replace('.', '/') -fun superDisplayName(mirror: TypeMirror): String { - return when (mirror.toString()) { - "none", "java.lang.Object" -> "" - else -> " extends $mirror " - } -} - private fun String.prefixIfNotEmpty(prefix: String): String = if (isNotEmpty()) prefix + this else this - -private fun String.suffixIfNotEmpty(prefix: String): String - = if (isNotEmpty()) this + prefix else this \ No newline at end of file diff --git a/api-doclet/src/main/kotlin/org/conscrypt/doclet/FilterDoclet.kt b/api-doclet/src/main/kotlin/org/conscrypt/doclet/FilterDoclet.kt index 0fc5497f0..94fb5ffdf 100644 --- a/api-doclet/src/main/kotlin/org/conscrypt/doclet/FilterDoclet.kt +++ b/api-doclet/src/main/kotlin/org/conscrypt/doclet/FilterDoclet.kt @@ -103,11 +103,11 @@ class FilterDoclet : Doclet { private fun generateClassFile(classInfo: ClassInfo) { val classFilePath = outputPath.resolve(classInfo.fileName) Files.createDirectories(classFilePath.parent) - val simpleName = classInfo.simpleName + val name = classInfo.innerName() html { body( - title = "$simpleName - conscrypt-openjdk API", + title = "$name - Conscrypt API", stylesheet = relativePath(classFilePath, cssPath), ) { compose {