Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit f91a27e

Browse files
committed
Add support for Kotlin Coroutines
Kotlin Coroutines are now supported but require additional reflection entries due to how Coroutines generates bytecode with an `Object` return type, see spring-projects/spring-framework#21546 for a potential future fix. Closes gh-409
1 parent 3d789ed commit f91a27e

File tree

18 files changed

+493
-5
lines changed

18 files changed

+493
-5
lines changed

spring-native-configuration/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@
328328
<artifactId>lettuce-core</artifactId>
329329
<scope>provided</scope>
330330
</dependency>
331+
<dependency>
332+
<groupId>com.fasterxml.jackson.module</groupId>
333+
<artifactId>jackson-module-kotlin</artifactId>
334+
<scope>provided</scope>
335+
</dependency>
331336

332337
<dependency>
333338
<groupId>junit</groupId>
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,50 @@
11
package kotlin;
22

3-
import org.springframework.nativex.type.NativeConfiguration;
3+
import org.springframework.nativex.hint.AccessBits;
44
import org.springframework.nativex.hint.NativeHint;
55
import org.springframework.nativex.hint.ResourcesInfo;
66
import org.springframework.nativex.hint.TypeInfo;
7+
import org.springframework.nativex.type.NativeConfiguration;
78

9+
import static org.springframework.nativex.hint.AccessBits.*;
810
import static org.springframework.nativex.hint.AccessBits.DECLARED_CONSTRUCTORS;
911
import static org.springframework.nativex.hint.AccessBits.DECLARED_FIELDS;
1012
import static org.springframework.nativex.hint.AccessBits.DECLARED_METHODS;
1113
import static org.springframework.nativex.hint.AccessBits.PUBLIC_METHODS;
1214

13-
@NativeHint(
14-
trigger=kotlin.Unit.class,
15+
@NativeHint(trigger = kotlin.Unit.class,
1516
resources = {
1617
@ResourcesInfo(patterns= {
1718
"META-INF/.*.kotlin_module$",
1819
".*.kotlin_builtins",
1920
"META-INF/services/.*"
2021
})
2122
}, types = {
22-
@TypeInfo(types = kotlin.reflect.jvm.internal.ReflectionFactoryImpl.class, access = DECLARED_CONSTRUCTORS),
2323
@TypeInfo(types = kotlin.KotlinVersion.class, access = PUBLIC_METHODS | DECLARED_FIELDS | DECLARED_METHODS | DECLARED_CONSTRUCTORS),
2424
@TypeInfo(typeNames = {
2525
"kotlin.KotlinVersion[]",
2626
"kotlin.KotlinVersion$Companion",
2727
"kotlin.KotlinVersion$Companion[]"
2828
})
2929
})
30+
@NativeHint(trigger = kotlin.reflect.full.KClasses.class,
31+
types = {
32+
@TypeInfo(types = kotlin.reflect.full.KClasses.class, access = CLASS),
33+
@TypeInfo(types = kotlin.Metadata.class, access = DECLARED_METHODS),
34+
@TypeInfo(types = kotlin.reflect.jvm.internal.ReflectionFactoryImpl.class, access = DECLARED_CONSTRUCTORS),
35+
@TypeInfo(types = kotlin.reflect.jvm.internal.impl.resolve.scopes.DescriptorKindFilter.class, access = DECLARED_FIELDS)
36+
})
37+
@NativeHint(trigger = kotlin.coroutines.Continuation.class,
38+
types = @TypeInfo(types = kotlin.coroutines.Continuation.class, typeNames = "kotlin.coroutines.Continuation[]", access = LOAD_AND_CONSTRUCT_AND_PUBLIC_METHODS))
39+
@NativeHint(trigger = com.fasterxml.jackson.module.kotlin.KotlinModule.class,
40+
types = {
41+
@TypeInfo(types = com.fasterxml.jackson.module.kotlin.KotlinModule.class),
42+
@TypeInfo(typeNames = {
43+
"com.fasterxml.jackson.module.kotlin.KotlinModule$Builder",
44+
"com.fasterxml.jackson.module.kotlin.SingletonSupport",
45+
"java.lang.String"
46+
}, access = CLASS)
47+
})
48+
3049
public class KotlinHints implements NativeConfiguration {
3150
}

spring-native-configuration/src/main/java/org/springframework/SpringFrameworkHints.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
org.springframework.web.socket.sockjs.transport.TransportHandlingSockJsService.class,
6060
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.class,
6161
org.springframework.core.ReactiveAdapterRegistry.class,
62-
org.springframework.core.NativeDetector.class
62+
org.springframework.core.NativeDetector.class,
63+
org.springframework.core.KotlinDetector.class
6364
}, typeNames = {
6465
"org.springframework.aot.StaticSpringFactories",
6566
"org.springframework.aop.Advisor$1",

spring-native-docs/src/main/asciidoc/support.adoc

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Java 8, Java 11 and Kotlin 1.3+ are supported.
1212
Be aware that Java 11 native images are currently impacted by a transient footprint issue that may lead
1313
(or not) to bigger images due to https://github.com/oracle/graal/issues/3163[oracle/graal#3163].
1414

15+
Kotlin Coroutines are supported but require additional reflection entries due to how Coroutines generates
16+
bytecode with an `Object` return type, see https://github.com/spring-projects/spring-framework/issues/21546[this related issue]
17+
for a potential future fix.
18+
1519
=== Spring Boot
1620

1721
IMPORTANT: `spring-native` {version} is designed to be used with Spring Boot {boot-version}.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Spring Boot project with Spring WebFlux, Coroutines, Netty and Jackson.
2+
3+
To build and run the native application packaged in a lightweight container:
4+
```
5+
mvn spring-boot:build-image
6+
docker-compose up
7+
```
8+
9+
And then go to [http://localhost:8080/](http://localhost:8080/).
10+
11+
As an alternative, you can use `build.sh` (with a local GraalVM installation or combined with
12+
`run-dev-container.sh` at the root of `spring-native` project). See also the related issue
13+
[Take advantage of Paketo dev-oriented images](https://github.com/spring-projects-experimental/spring-native/issues/227).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
${PWD%/*samples/*}/scripts/compileWithMaven.sh && ${PWD%/*samples/*}/scripts/test.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3.1'
2+
services:
3+
webmvc-tomcat:
4+
image: webflux-kotlin:0.0.1-SNAPSHOT
5+
ports:
6+
- "8080:8080"

0 commit comments

Comments
 (0)