-
Notifications
You must be signed in to change notification settings - Fork 288
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
How to omit modifier for protected
function in object
?
#1389
Comments
There is no way to generate this with KotlinPoet at present. |
Test is here: https://kotlin.godbolt.org/z/vch44E1qK Note that explicit API mode is ON. Adding any visibility modifier to the override function will cause compilation failure. |
Somewhat related to #1301 in that there are situations in which we can omit explicit visibility modifiers. In this case, omission is actually critical. |
Any possible workaround anyone can think of? Is there any way in Kotlinpoet to just output text (knowing you'd be responsible for imports and such) as a way to work around it when the really structured APIs make life difficult? (An example is that |
Looks like #1550 made generating this code possible! @Test fun protectedOverrideInObject() {
val publicInterface = TypeSpec.interfaceBuilder("PublicInterface")
.addProperty("myInt", Int::class)
.build()
val abstractFoo = TypeSpec.classBuilder("Foo")
.addModifiers(KModifier.ABSTRACT)
.addType(TypeSpec.classBuilder("Bar")
.addModifiers(KModifier.PROTECTED, KModifier.INNER)
.addSuperinterface(ClassName("", "PublicInterface"))
.addProperty(PropertySpec.builder("myInt", Int::class)
.addModifiers(KModifier.OVERRIDE)
.initializer("42")
.build())
.build())
.addFunction(FunSpec.builder("toInt")
.receiver(ClassName("", "Bar"))
.addModifiers(KModifier.PROTECTED, KModifier.ABSTRACT)
.returns(Int::class)
.build())
.build()
val fooInstance = TypeSpec.objectBuilder("FooInstance")
.superclass(ClassName("", "Foo"))
.addFunction(FunSpec.builder("toInt")
.addModifiers(KModifier.OVERRIDE)
.receiver(ClassName("", "Bar"))
.returns(Int::class)
.addStatement("return myInt")
.build())
.build()
assertThat(FileSpec.builder("test", "Test")
.addType(publicInterface)
.addType(abstractFoo)
.addType(fooInstance)
.build()
.toString())
.isEqualTo("""
|package test
|
|import Bar
|import Foo
|import PublicInterface
|import kotlin.Int
|
|public interface PublicInterface {
| public val myInt: Int
|}
|
|public abstract class Foo {
| protected abstract fun Bar.toInt(): Int
|
| protected inner class Bar : PublicInterface {
| override val myInt: Int = 42
| }
|}
|
|public object FooInstance : Foo() {
| override fun Bar.toInt(): Int = myInt
|}
|""".trimMargin())
} |
If I have
I get an error saying that
Modifier 'protected' is not applicable inside 'standalone object'
. So I need to remove theprotected
modifier.Note that I can't replace
protected
withpublic
as that will trigger apublic member exposes its
protected (in Foo)receiver type Bar
.So, how do I generate this with Kotlinpoet? I can't figure out how to create the method with just the
override
modifier and nothing else, but that's the only way to avoid an error.The text was updated successfully, but these errors were encountered: