spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
56.39k stars 38.06k forks source link

GeneratedFiles#addSourceFile should not allow to add a source in the default package #31628

Closed snicoll closed 10 months ago

snicoll commented 10 months ago

When a JavaFile is added to the list of files, its class name is deduced from its package name and type name. When adding a JavaFile that was added in the default package, this leads to:

Exception in thread "main" java.lang.IllegalArgumentException: 'className' must be a valid identifier, got '.MyConfig__BeanDefinitions'
        at org.springframework.util.Assert.isTrue(Assert.java:122)
        at org.springframework.aot.generate.GeneratedFiles.getClassNamePath(GeneratedFiles.java:164)
        at org.springframework.aot.generate.GeneratedFiles.addSourceFile(GeneratedFiles.java:70)
        at org.springframework.aot.generate.GeneratedFiles.addSourceFile(GeneratedFiles.java:47)
        at org.springframework.aot.generate.GeneratedClasses.writeTo(GeneratedClasses.java:198)
        at org.springframework.aot.generate.DefaultGenerationContext.writeGeneratedContent(DefaultGenerationContext.java:136)
        at org.springframework.context.aot.ContextAotProcessor.performAotProcessing(ContextAotProcessor.java:108)
        at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:84)
        at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:49)
        at org.springframework.context.aot.AbstractAotProcessor.process(AbstractAotProcessor.java:82)
        at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:80)
snicoll commented 10 months ago

Unfortunately, fixing the logic does not help as we can't refer to a class in the default package. So our attempt to refer to it leads to:

/Users/snicoll/Downloads/demo-kotlin-root/build/generated/aotSources/com/example/demokotlinroot/DemoKotlinRootApplicationKt__BeanFactoryRegistrations.java:46: error: cannot find symbol
    beanFactory.registerBeanDefinition("config-MyConfig", MyConfig__BeanDefinitions.getConfigMyConfigBeanDefinition());

Given we don't know the visibility of the component, we'll probably have to reject that use case upfront.

snicoll commented 10 months ago

This throws the following now:

Exception in thread "main" java.lang.IllegalArgumentException: Could not add 'MyConfig__BeanDefinitions', processing classes in the default package is not supported. Did you forget to add a package statement?
        at org.springframework.aot.generate.GeneratedFiles.validatePackage(GeneratedFiles.java:174)
        at org.springframework.aot.generate.GeneratedFiles.addSourceFile(GeneratedFiles.java:47)
        at org.springframework.aot.generate.GeneratedClasses.writeTo(GeneratedClasses.java:198)
        at org.springframework.aot.generate.DefaultGenerationContext.writeGeneratedContent(DefaultGenerationContext.java:136)
        at org.springframework.context.aot.ContextAotProcessor.performAotProcessing(ContextAotProcessor.java:108)
        at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:84)
        at org.springframework.context.aot.ContextAotProcessor.doProcess(ContextAotProcessor.java:49)
        at org.springframework.context.aot.AbstractAotProcessor.process(AbstractAotProcessor.java:82)
        at org.springframework.boot.SpringApplicationAotProcessor.main(SpringApplicationAotProcessor.java:80)