Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omit protected modifier on override function #1551

Closed
wants to merge 1 commit into from
Closed

Omit protected modifier on override function #1551

wants to merge 1 commit into from

Conversation

Omico
Copy link
Contributor

@Omico Omico commented May 16, 2023

Fix #1389

@Omico Omico closed this May 19, 2023
@Omico
Copy link
Contributor Author

Omico commented May 19, 2023

Seem it already omits the "protected" modifier.

  @Test fun `omit protected modifier in object`() {
    val interfaceType = TypeSpec.interfaceBuilder("Interface")
      .addModifiers(PUBLIC)
      .addProperty("id", Int::class)
      .build()
    assertThat(interfaceType.toString()).isEqualTo(
      """
      |public interface Interface {
      |  public val id: kotlin.Int
      |}
      |
      """.trimMargin(),
    )
    val interfaceClassName = ClassName("", "Interface")
    val protectedInnerClass = ClassName("", "ProtectedInnerClass")
    val abstractClass = TypeSpec.classBuilder("AbstractClass")
      .addModifiers(PUBLIC, ABSTRACT)
      .addType(
        TypeSpec.classBuilder("ProtectedInnerClass")
          .addSuperinterface(interfaceClassName)
          .addModifiers(PROTECTED, INNER)
          .addProperty(
            PropertySpec.builder("id", Int::class)
              .addModifiers(KModifier.OVERRIDE)
              .initializer("%L", 0)
              .build(),
          )
          .build(),
      )
      .addFunction(
        FunSpec.builder("toInt")
          .addModifiers(PROTECTED, ABSTRACT)
          .receiver(protectedInnerClass)
          .returns(Int::class)
          .build(),
      )
      .build()
    assertThat(abstractClass.toString()).isEqualTo(
      """
      |public abstract class AbstractClass {
      |  protected abstract fun ProtectedInnerClass.toInt(): kotlin.Int
      |
      |  protected inner class ProtectedInnerClass : Interface {
      |    override val id: kotlin.Int = 0
      |  }
      |}
      |
      """.trimMargin(),
    )
    val abstractClassName = ClassName("", "AbstractClass")
    val instance = TypeSpec.objectBuilder("Instance")
      .superclass(abstractClassName)
      .addFunction(
        FunSpec.builder("toInt")
          .addModifiers(KModifier.OVERRIDE)
          .receiver(protectedInnerClass)
          .addStatement("return id")
          .returns(Int::class)
          .build(),
      )
      .build()
    // Already omit the "protected" modifier here, no need to fix.
    assertThat(instance.toString()).isEqualTo(
      """
      |public object Instance : AbstractClass() {
      |  override fun ProtectedInnerClass.toInt(): kotlin.Int = id
      |}
      |
      """.trimMargin(),
    )
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to omit modifier for protected function in object?
1 participant