Skip to content

Commit 7990907

Browse files
Kotlin reflections support
1 parent d48e074 commit 7990907

28 files changed

Lines changed: 2344 additions & 32 deletions

File tree

bytebuddy-proxy-support/src/main/java/dev/restate/bytebuddy/proxysupport/ByteBuddyProxyFactory.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ public final class ByteBuddyProxyFactory implements ProxyFactory {
5151
public <T> @Nullable T createProxy(Class<T> clazz, MethodInterceptor interceptor) {
5252
// Cannot proxy final classes
5353
if (Modifier.isFinal(clazz.getModifiers())) {
54-
throw new IllegalArgumentException("Class " + clazz + " is final, cannot be proxied.");
54+
if (ReflectionUtils.isKotlinClass(clazz)) {
55+
throw new IllegalArgumentException(
56+
clazz
57+
+ " is not open, cannot be proxied. Make it open to be able to proxy it, or refactor it extracting the restate interface out of it.");
58+
}
59+
throw new IllegalArgumentException(
60+
clazz
61+
+ " is final, cannot be proxied. Remove the final keyword, or refactor it extracting the restate interface out of it.");
5562
}
5663

5764
try {
@@ -87,7 +94,7 @@ public Method getMethod() {
8794

8895
return proxyInstance;
8996
} catch (Exception e) {
90-
throw new IllegalArgumentException("Cannot create proxy for class " + clazz, e);
97+
throw new IllegalArgumentException("Cannot create proxy for " + clazz, e);
9198
}
9299
}
93100

@@ -99,10 +106,11 @@ private <T> Class<?> generateProxyClass(Class<T> clazz) throws NoSuchFieldExcept
99106
ReflectionUtils.getUniqueDeclaredMethods(
100107
clazz,
101108
method ->
102-
ReflectionUtils.findAnnotation(method, Handler.class) != null
103-
|| ReflectionUtils.findAnnotation(method, Shared.class) != null
104-
|| ReflectionUtils.findAnnotation(method, Workflow.class) != null
105-
|| ReflectionUtils.findAnnotation(method, Exclusive.class) != null);
109+
!Modifier.isStatic(method.getModifiers())
110+
&& (ReflectionUtils.findAnnotation(method, Handler.class) != null
111+
|| ReflectionUtils.findAnnotation(method, Shared.class) != null
112+
|| ReflectionUtils.findAnnotation(method, Workflow.class) != null
113+
|| ReflectionUtils.findAnnotation(method, Exclusive.class) != null));
106114
for (var method : methods) {
107115
validateMethod(method);
108116
}
@@ -114,10 +122,12 @@ private <T> Class<?> generateProxyClass(Class<T> clazz) throws NoSuchFieldExcept
114122
: byteBuddy.subclass(clazz);
115123

116124
var annotationMatcher =
117-
isAnnotatedWith(Handler.class)
118-
.or(isAnnotatedWith(Exclusive.class))
119-
.or(isAnnotatedWith(Shared.class))
120-
.or(isAnnotatedWith(Workflow.class));
125+
not(isStatic())
126+
.and(
127+
isAnnotatedWith(Handler.class)
128+
.or(isAnnotatedWith(Exclusive.class))
129+
.or(isAnnotatedWith(Shared.class))
130+
.or(isAnnotatedWith(Workflow.class)));
121131
try (var unloaded =
122132
builder
123133
// Add a field to store the interceptor

client-kotlin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ dependencies {
99
api(project(":client")) { exclude("dev.restate", "sdk-serde-jackson") }
1010
api(project(":sdk-serde-kotlinx"))
1111

12+
implementation(project(":common-kotlin"))
1213
implementation(libs.kotlinx.coroutines.core)
14+
implementation(kotlin("reflect"))
1315
}

0 commit comments

Comments
 (0)