-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31843 from Ladicek/arc-kotlin-suspend-functions-i…
…nterception ArC: Kotlin improvements
- Loading branch information
Showing
14 changed files
with
365 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...pendent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/KotlinDotNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import org.jboss.jandex.DotName; | ||
|
||
class KotlinDotNames { | ||
static final DotName METADATA = DotName.createSimple("kotlin.Metadata"); | ||
|
||
static final DotName CONTINUATION = DotName.createSimple("kotlin.coroutines.Continuation"); | ||
} |
36 changes: 36 additions & 0 deletions
36
independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/KotlinUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.quarkus.arc.processor; | ||
|
||
import java.lang.reflect.Modifier; | ||
|
||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.MethodInfo; | ||
import org.jboss.jandex.Type; | ||
|
||
public class KotlinUtils { | ||
public static boolean isKotlinClass(ClassInfo clazz) { | ||
return clazz.hasDeclaredAnnotation(KotlinDotNames.METADATA); | ||
} | ||
|
||
public static boolean isKotlinMethod(MethodInfo method) { | ||
return isKotlinClass(method.declaringClass()); | ||
} | ||
|
||
public static boolean isKotlinSuspendMethod(MethodInfo method) { | ||
if (!isKotlinMethod(method)) { | ||
return false; | ||
} | ||
|
||
Type lastParameter = method.parameterType(method.parametersCount() - 1); | ||
return KotlinDotNames.CONTINUATION.equals(lastParameter.name()); | ||
} | ||
|
||
public static boolean isNoninterceptableKotlinMethod(MethodInfo method) { | ||
// the Kotlin compiler generates somewhat streamlined bytecode when it determines | ||
// that a `suspend` method cannot be overridden, and that bytecode doesn't work | ||
// well with subclassing-based interception | ||
// | ||
// a `suspend` method can be overridden when it is `open` and is declared in an `open` class | ||
return isKotlinSuspendMethod(method) | ||
&& (Modifier.isFinal(method.flags()) || Modifier.isFinal(method.declaringClass().flags())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 0 additions & 74 deletions
74
...jects/arc/tests/src/test/java/io/quarkus/arc/test/interceptors/KotlinInterceptorTest.java
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
.../tests/src/test/kotlin/io/quarkus/arc/test/interceptors/InterceptableSuspendMethodTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.arc.test.interceptors | ||
|
||
import io.quarkus.arc.Arc | ||
import io.quarkus.arc.test.ArcTestContainer | ||
import jakarta.annotation.Priority | ||
import jakarta.inject.Singleton | ||
import jakarta.interceptor.AroundInvoke | ||
import jakarta.interceptor.Interceptor | ||
import jakarta.interceptor.InterceptorBinding | ||
import jakarta.interceptor.InvocationContext | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.runBlocking | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
import kotlin.test.assertEquals | ||
|
||
class InterceptableSuspendMethodTest { | ||
@RegisterExtension | ||
val container = ArcTestContainer(MyInterceptorBinding::class.java, MyInterceptor::class.java, | ||
MyService::class.java) | ||
|
||
@Test | ||
fun test() { | ||
val service = Arc.container().instance(MyService::class.java).get() | ||
val result = runBlocking { | ||
service.hello() | ||
} | ||
|
||
assertEquals("hello", result) | ||
assertEquals(1, MyInterceptor.intercepted) | ||
} | ||
|
||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@InterceptorBinding | ||
annotation class MyInterceptorBinding | ||
|
||
@MyInterceptorBinding | ||
@Priority(1) | ||
@Interceptor | ||
class MyInterceptor { | ||
companion object { | ||
var intercepted = 0 | ||
} | ||
|
||
@AroundInvoke | ||
fun intercept(ctx: InvocationContext): Any { | ||
intercepted++ | ||
return ctx.proceed() | ||
} | ||
} | ||
|
||
@Singleton | ||
open class MyService { | ||
@MyInterceptorBinding | ||
open suspend fun hello(): String { | ||
delay(10) | ||
return "hello" | ||
} | ||
} | ||
} |
Oops, something went wrong.