Skip to content

Commit

Permalink
Support constructor injection in Kotlin Panache repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Dec 8, 2021
1 parent 7ac7d02 commit ab30ef5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class KotlinPanacheClassOperationGenerationVisitor extends ClassVisitor {
public static final String NULLABLE_DESCRIPTOR = "Lorg/jetbrains/annotations/Nullable;";
public static final ByteCodeType OBJECT = new ByteCodeType(Object.class);
protected static final ByteCodeType CLASS = new ByteCodeType(Class.class);
private static final String CTOR_METHOD_NAME = "<init>";
protected final Function<String, String> argMapper;
protected final ClassInfo classInfo;
protected final ByteCodeType entityUpperBound;
Expand Down Expand Up @@ -291,7 +292,7 @@ private void emitNullCheck(MethodVisitor mv, Type returnType) {
.replace("java.util.List", "kotlin.collections.List")));

mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NullPointerException",
"<init>", "(Ljava/lang/String;)V", false);
CTOR_METHOD_NAME, "(Ljava/lang/String;)V", false);
mv.visitInsn(ATHROW);
mv.visitLabel(label);
mv.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Object" });
Expand Down Expand Up @@ -538,6 +539,9 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str
} else if (name.contains("$")) {
//some agents such as jacoco add new methods, they generally have $ in the name
return super.visitMethod(access, name, descriptor, signature, exceptions);
} else if (name.equals(CTOR_METHOD_NAME)) {
//Arc can add no-args constructors to support intercepted beans
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import io.quarkus.mongodb.panache.kotlin.PanacheMongoRepository
import javax.enterprise.context.ApplicationScoped

@ApplicationScoped
class BookRepository : PanacheMongoRepository<Book>
class BookRepository(private val dummyService: DummyService) : PanacheMongoRepository<Book>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.it.mongodb.panache.book

import javax.enterprise.context.ApplicationScoped

// used only to validate that we can inject CDI beans into Panache repositories written in Kotlin
@ApplicationScoped
class DummyService {
}

0 comments on commit ab30ef5

Please sign in to comment.