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

Document AOT limitations related to Kotlin identifiers with backticks #32487

Closed
juliuskrah opened this issue Mar 18, 2024 · 2 comments
Closed
Assignees
Labels
theme: aot An issue related to Ahead-of-time processing theme: kotlin An issue related to Kotlin support type: documentation A documentation task
Milestone

Comments

@juliuskrah
Copy link

juliuskrah commented Mar 18, 2024

Affects: <6.1.2>
Reproducer: https://github.com/juliuskrah/graphql-demo/tree/spf-32487


Given this valid kotlin class:

class `20240318CreateProductTable` {

    fun createProductTable() {

    }

    fun addProductData() {

    }
}

and Bean class:

class ExampleBean {
    lateinit var migrationClass: Class<*>
}

with a runtime bean definition:

@Configuration(proxyBeanMethods = false)
class MyConfig {
    companion object {
        @Bean
        fun beanProcessor(): BeanFactoryPostProcessor {
            return BeanFactoryPostProcessor {
                if (it is DefaultListableBeanFactory) {
                    val beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(ExampleBean::class.java)
                        .addPropertyValue("migrationClass", com.example.graph.migration.`20240318CreateProductTable`::class.java)
                        .beanDefinition
                    it.registerBeanDefinition("exampleBean", beanDefinition)
                }
            }
        }
    }
}

generates the following invalid java class:

import com.example.graph.migration.20240318CreateProductTable;
import org.springframework.aot.generate.Generated;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;

@Generated
public class ExampleBean__BeanDefinitions {
  /**
   * Get the bean definition for 'exampleBean'.
   */
  public static BeanDefinition getExampleBeanBeanDefinition() {
    RootBeanDefinition beanDefinition = new RootBeanDefinition(ExampleBean.class);
    beanDefinition.getPropertyValues().addPropertyValue("migrationClass", 20240318CreateProductTable.class);
    beanDefinition.setInstanceSupplier(ExampleBean::new);
    return beanDefinition;
  }
}

that results in:

> Task :compileAotJava FAILED
/path/to/file/graphql-demo/build/generated/aotSources/com/example/graph/ExampleBean__BeanDefinitions.java:3: error: ';' expected
import com.example.graph.migration.20240318CreateProductTable;
                                  ^
/path/to/file/graphql-demo/build/generated/aotSources/com/example/graph/ExampleBean__BeanDefinitions.java:18: error: ')' or ',' expected
    beanDefinition.getPropertyValues().addPropertyValue("migrationClass", 20240318CreateProductTable.class);
                                                                                  ^
/path/to/file/graphql-demo/build/generated/aotSources/com/example/graph/ExampleBean__BeanDefinitions.java:18: error: not a statement
    beanDefinition.getPropertyValues().addPropertyValue("migrationClass", 20240318CreateProductTable.class);
                                                                                                    ^
/path/to/file/graphql-demo/build/generated/aotSources/com/example/graph/ExampleBean__BeanDefinitions.java:18: error: ';' expected
    beanDefinition.getPropertyValues().addPropertyValue("migrationClass", 20240318CreateProductTable.class);
                                                                                                          ^
4 errors

FAILURE: Build failed with an exception.

Expectation

Kotlin sources should generate Kotlin aotSources

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 18, 2024
juliuskrah added a commit to juliuskrah/graphql-demo that referenced this issue Mar 18, 2024
@sdeleuze sdeleuze added theme: aot An issue related to Ahead-of-time processing theme: kotlin An issue related to Kotlin support labels Mar 18, 2024
@sdeleuze sdeleuze self-assigned this Mar 18, 2024
@snicoll snicoll changed the title Kotlin aot should generate kotlin classes Spring AOT generates invalid class name for Kotin classes that do not match Java conventions Mar 19, 2024
@snicoll snicoll added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 19, 2024
@snicoll snicoll added this to the 6.1.x milestone Mar 19, 2024
@snicoll
Copy link
Member

snicoll commented Mar 19, 2024

Thanks for the report.

Kotlin sources should generate Kotlin aotSources

I can understand how, as a Kotlin user, you'd expect that but that would significantly increase the complexity of both the generation process and the tooling around it. There is no need to resort to that to fix this issue, Kotlin having different convention with class names is news to me, we'll just have to be more defensive about those.

@sdeleuze sdeleuze added type: documentation A documentation task and removed type: bug A general bug labels Mar 19, 2024
@sdeleuze
Copy link
Contributor

sdeleuze commented Mar 19, 2024

As far as I can tell, Kotlin classes with backticks seems to be one of the rare cases where there is no workaround for Java interrop (@JvmName can be applied to function or class generated in a Kotlin file, but not to customize an explicit Kotlin class name).

I am also not in favor of generating Kotlin code (far too involve) and not in favor throwing an exception because the regexp or algorithm designed to identify invalid class names could easily be wrong.

As a consequence, I turn this issue into a documentation one in order to document this limitation inherited from Kotlin own limitation in terms of Java interop.

Notice that backticks in class names, while technically possible, are not recommended in Kotlin coding conventions (usage of backticks is mainly for test methods).

@sdeleuze sdeleuze changed the title Spring AOT generates invalid class name for Kotin classes that do not match Java conventions Document AOT limitations related to Kotlin identifiers with backticks Mar 19, 2024
@sdeleuze sdeleuze modified the milestones: 6.1.x, 6.1.6 Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: aot An issue related to Ahead-of-time processing theme: kotlin An issue related to Kotlin support type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants