Skip to content

Commit

Permalink
Inner class logic fixes. (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
prbprbprb authored Oct 15, 2024
1 parent cff2a6f commit 66c8e95
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
52 changes: 50 additions & 2 deletions api-doclet/src/main/kotlin/org/conscrypt/doclet/ClassInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassInfo> {
Expand All @@ -36,6 +39,50 @@ data class ClassInfo(val element: TypeElement) : Comparable<ClassInfo> {
.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 {
Expand Down Expand Up @@ -136,7 +183,7 @@ data class ClassInfo(val element: TypeElement) : Comparable<ClassInfo> {
fun generateHtml() = html {
div("package-name") { text("Package: $packageName") }
h1(simpleName)
pre(element.signature(), "class-signature")
pre(signature(), "class-signature")

compose {
description() +
Expand All @@ -149,6 +196,7 @@ data class ClassInfo(val element: TypeElement) : Comparable<ClassInfo> {

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
24 changes: 0 additions & 24 deletions api-doclet/src/main/kotlin/org/conscrypt/doclet/ElementUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 66c8e95

Please sign in to comment.