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

Get exception tying to generate empty fun interface which inherit another interface with exact one abstract function #1972

Open
jizoquval opened this issue Aug 29, 2024 · 3 comments
Labels

Comments

@jizoquval
Copy link

Describe the bug
Using KotlinPoet 1.18.1 I get the exception:
java.lang.IllegalStateException: Functional interfaces must have exactly one abstract function. Contained 0: []
when try to generate valid kotlin code. I'm trying to generate functional interface which doesn't contains new methods and inherits another functional interface with method declaration.

To Reproduce

    val bClassName = ClassName("","B")
    val aClassName = ClassName("","A")
    val fileSpec = FileSpec.builder("", "Example")
        .addType(
            TypeSpec.funInterfaceBuilder(bClassName)
                .addFunction(
                    FunSpec.builder("foo")
                        .addModifiers(KModifier.ABSTRACT)
                        .build(),
                )
                .build()
        )
        .addType(
            TypeSpec.funInterfaceBuilder(aClassName)
                .addSuperinterface(bClassName)
                .build()
        )
        .build()
    fileSpec.writeTo(System.out)

Expected behavior
I expect to get generated code instead of exception.

Additional context
This is a correct kotlin code and it can be compiled.

fun interface A: B

fun interface B {
  fun foo()
}
@jizoquval jizoquval added the bug label Aug 29, 2024
@Egorand
Copy link
Collaborator

Egorand commented Aug 29, 2024

Does the compiler check that B is a functional interface, or an interface that has exactly one method?

But given that KotlinPoet's correctness checks don't have to be as exhaustive as the real compiler's, I think we can allow zero methods in a functional interface if it has a superinterface. Would that make sense?

PRs welcome!

@jizoquval
Copy link
Author

@Egorand B can be a simple interface, not fun interface. As I understand the compiler only checks that B has exact one method.

Yes, I think such solution will make sense

@jizoquval
Copy link
Author

I can try to fix it and open PR.

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

No branches or pull requests

2 participants