|
| 1 | +[[appendix.configuration-metadata.symbol-processor]] |
| 2 | += Generating Metadata for Kotlin by Using the Symbol Processor |
| 3 | + |
| 4 | +Kotlin code can be processed by the Java annotation processor described in xref:specification:configuration-metadata/annotation-processor.adoc[] through kapt. |
| 5 | +As kapt is in maintenance mode, the `spring-boot-configuration-symbol-processor` jar provides an alternative that is built on https://kotlinlang.org/docs/ksp-overview.html[Kotlin Symbol Processing] (KSP) and that processes Kotlin sources directly. |
| 6 | + |
| 7 | +The symbol processor writes the same `META-INF/spring-configuration-metadata.json` file as the annotation processor, so IDEs and other tools consume it without any change. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +[[appendix.configuration-metadata.symbol-processor.configuring]] |
| 12 | +== Configuring the Symbol Processor |
| 13 | + |
| 14 | +KSP is applied with its Gradle plugin. |
| 15 | +Once the plugin has been applied, declare the symbol processor in the `ksp` configuration, as shown in the following example: |
| 16 | + |
| 17 | +[source,gradle] |
| 18 | +---- |
| 19 | +plugins { |
| 20 | + id "com.google.devtools.ksp" version "$kspVersion" |
| 21 | +} |
| 22 | +
|
| 23 | +dependencies { |
| 24 | + ksp "org.springframework.boot:spring-boot-configuration-symbol-processor" |
| 25 | +} |
| 26 | +---- |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +[[appendix.configuration-metadata.symbol-processor.mixed-modules]] |
| 31 | +== Modules That Contain Java and Kotlin |
| 32 | + |
| 33 | +Both processors write `META-INF/spring-configuration-metadata.json`, so a module that applies both fails to build with a duplicate entry for that file. |
| 34 | +Apply only one of them to a module: the annotation processor when the module declares its configuration properties in Java, and the symbol processor when it declares them in Kotlin. |
| 35 | +When properties are declared in both languages, keep the annotation processor and let it process the Kotlin types through kapt. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +[[appendix.configuration-metadata.symbol-processor.supported-types]] |
| 40 | +== Supported Types |
| 41 | + |
| 42 | +The symbol processor generates metadata for Kotlin types that are annotated with javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation], covering: |
| 43 | + |
| 44 | +* Constructor binding, including javadoc:org.springframework.boot.context.properties.bind.DefaultValue[format=annotation] and javadoc:org.springframework.boot.context.properties.bind.Name[format=annotation]. |
| 45 | +* JavaBean binding of `var` properties, and of read-only properties whose type is a `Collection` or a `Map`. |
| 46 | +* Methods that are annotated with javadoc:org.springframework.boot.context.properties.ConfigurationProperties[format=annotation], whose return type carries the properties. |
| 47 | +* Nested groups, either deduced from a type that is declared in the same top level type or requested with javadoc:org.springframework.boot.context.properties.NestedConfigurationProperty[format=annotation]. |
| 48 | +* Descriptions taken from the KDoc of a property, or from the `@property` and `@param` tags of the KDoc of the declaring class for constructor parameters. |
| 49 | +* Deprecations declared with `@Deprecated` or javadoc:org.springframework.boot.context.properties.DeprecatedConfigurationProperty[format=annotation]. |
| 50 | + |
| 51 | +Types that are annotated with an actuator endpoint annotation, such as `@Endpoint`, contribute their `access` property and, when they declare a main read operation, their `cache.time-to-live` property. |
| 52 | + |
| 53 | +Kotlin types are reported using their JVM names, so a `List<String>` property is described as `java.util.List<java.lang.String>` and an `Int` property is described as `java.lang.Integer`. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +[[appendix.configuration-metadata.symbol-processor.additional-metadata]] |
| 58 | +== Adding Additional Metadata |
| 59 | + |
| 60 | +The metadata of `META-INF/additional-spring-configuration-metadata.json` is merged into the generated file, which also removes the properties that the additional metadata ignores. |
| 61 | +As KSP gives a processor no access to the resources of the module, the directories to look into have to be listed with a processor option, as shown in the following example: |
| 62 | + |
| 63 | +[source,gradle] |
| 64 | +---- |
| 65 | +ksp { |
| 66 | + arg("org.springframework.boot.configurationprocessor.additionalMetadataLocations", "src/main/resources") |
| 67 | +} |
| 68 | +---- |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +[[appendix.configuration-metadata.symbol-processor.limitations]] |
| 73 | +== Limitations |
| 74 | + |
| 75 | +The symbol processor does not support the following features of the annotation processor: |
| 76 | + |
| 77 | +* Default values that are declared with an initializer, such as `var port: Int = 8080`. |
| 78 | +KSP does not give a processor access to initializers, so annotate the property with javadoc:org.springframework.boot.context.properties.bind.DefaultValue[format=annotation] to describe such a default. |
| 79 | +* Endpoint annotations that are meta-annotated, as opposed to the endpoint annotations of Spring Boot itself. |
| 80 | +* javadoc:org.springframework.boot.context.properties.ConfigurationPropertiesSource[format=annotation], and the per-type metadata that the annotation processor writes for it. |
| 81 | +* Lombok, which does not apply to Kotlin code. |
| 82 | + |
| 83 | +Generic type arguments that a superclass declares are not resolved against the type that extends it, so such a property is described using the upper bound of its type variable. |
0 commit comments