Skip to content

Autogenerate default Java constructors #2765

@owengray-google

Description

@owengray-google

Describe the bug
You do not autogenerate the default Java constructor. We autogenerate it downstream, but that causes us issues with the explicitly-hidden-constructor case.

(the first set of assertions is the Documentable tree, the second set is the component tree we/dackka generate from it)

@Test
fun `Default constructors are autogenerated when no explicit constructor is present`() {
    val moduleJ = """
        public class Foo {}
    """.trimIndent().render(java = true)
    val classlikeJ = moduleJ.page("Foo").data.content
    val moduleK = """
        public class Foo {}
    """.trimIndent().render(java = false)
    val classlikeK = moduleK.page("Foo").data.content

    // test the Documentables tree. The correct behavior would be both isNotEmpty
    for (module in listOf(moduleJ, moduleK)) {
        val classlike = module.explicitClasslike("Foo")
        if (module == moduleK)
            assertThat((classlike as DClass).constructors).isEmpty()
        else
            assertThat((classlike as DClass).constructors).isNotEmpty()
    }

    // test the Documentables tree. The correct behavior is both isNotEmpty
    for (classlike in listOf(classlikeJ, classlikeK)) {
        assertThat(classlike.data.publicConstructorsDetails).isNotEmpty()
    }
}

@Test
fun `Default constructors are not autogenerated when private-constuctor pattern is used`() {
    val moduleJ = """
        public class Foo { private Foo() {} }
    """.trimIndent().render(java = true)
    val classlikeJ = moduleJ.page("Foo").data.content
    val moduleK = """
        public class Foo private constructor() {}
    """.trimIndent().render(java = false)
    val classlikeK = moduleK.page("Foo").data.content

    // test the Documentables tree. The correct behavior is both isEmpty
    for (module in listOf(moduleJ, moduleK)) {
        val classlike = module.explicitClasslike("Foo")
        assertThat((classlike as DClass).constructors).isEmpty()
    }

    // test our Components tree. The correct behavior is both isEmpty.
    // Our fix for the next test breaks us in this case. I think this is unavoidably upstream.
    for (classlike in listOf(classlikeJ, classlikeK)) {
        if (classlike == classlikeK)
            assertThat(classlike.data.publicConstructorsDetails).isNotEmpty()
        else
            assertThat(classlike.data.publicConstructorsDetails).isEmpty()        }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfeedback: GoogleAn issue/PR submitted by colleagues at Google, most likely related to the Android API reference docsgood first issueA beginner-friendly issue for which some assistance is expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions