Skip to content

Commit

Permalink
Fix combination of @QuarkusMain and Kotlin native application
Browse files Browse the repository at this point in the history
Fixes: #29754
  • Loading branch information
geoand committed Dec 8, 2022
1 parent 96826d2 commit 2846cd2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.deployment.steps;

import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;

final class KotlinUtil {

private static final DotName KOTLIN_METADATA_ANNOTATION = DotName.createSimple("kotlin.Metadata");

private KotlinUtil() {
}

static boolean isKotlinClass(ClassInfo classInfo) {
return classInfo.hasDeclaredAnnotation(KOTLIN_METADATA_ANNOTATION);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.deployment.steps;

import static io.quarkus.deployment.steps.KotlinUtil.isKotlinClass;
import static io.quarkus.gizmo.MethodDescriptor.ofConstructor;
import static io.quarkus.gizmo.MethodDescriptor.ofMethod;

Expand All @@ -17,6 +18,7 @@

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ArrayType;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.MethodInfo;
Expand Down Expand Up @@ -341,12 +343,13 @@ public MainClassBuildItem mainClassBuildStep(BuildProducer<GeneratedClassBuildIt
if (nameValue != null) {
name = nameValue.asString();
}
ClassInfo classInfo = i.target().asClass();
if (quarkusMainAnnotations.containsKey(name)) {
throw new RuntimeException(
"More than one @QuarkusMain method found with name '" + name + "': "
+ i.target().asClass().name() + " and " + quarkusMainAnnotations.get(name));
+ classInfo.name() + " and " + quarkusMainAnnotations.get(name));
}
quarkusMainAnnotations.put(name, i.target().asClass().name().toString());
quarkusMainAnnotations.put(name, sanitizeMainClassName(classInfo));
}

if (packageConfig.mainClass.isPresent()) {
Expand Down Expand Up @@ -410,6 +413,19 @@ public MainClassBuildItem mainClassBuildStep(BuildProducer<GeneratedClassBuildIt
return new MainClassBuildItem(mainClassName);
}

private static String sanitizeMainClassName(ClassInfo mainClass) {
String className = mainClass.name().toString();
if (isKotlinClass(mainClass)) {
MethodInfo mainMethod = mainClass.method("main",
ArrayType.create(Type.create(DotName.createSimple(String.class.getName()), Type.Kind.CLASS), 1));
if (mainMethod == null) {
className += "Kt";
}

}
return className;
}

private void generateMainForQuarkusApplication(String quarkusApplicationClassName,
BuildProducer<GeneratedClassBuildItem> generatedClass) {
ClassCreator file = new ClassCreator(new GeneratedClassGizmoAdaptor(generatedClass, true), MAIN_CLASS, null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.deployment.steps;

import static io.quarkus.deployment.steps.KotlinUtil.isKotlinClass;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.util.HashSet;
Expand Down Expand Up @@ -34,8 +36,6 @@ public class RegisterForReflectionBuildStep {

private static final Logger log = Logger.getLogger(RegisterForReflectionBuildStep.class);

private static final DotName KOTLIN_METADATA_ANNOTATION = DotName.createSimple("kotlin.Metadata");

@BuildStep
public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities capabilities,
BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
Expand Down Expand Up @@ -98,10 +98,6 @@ public void build(CombinedIndexBuildItem combinedIndexBuildItem, Capabilities ca
}
}

private static boolean isKotlinClass(ClassInfo classInfo) {
return classInfo.hasDeclaredAnnotation(KOTLIN_METADATA_ANNOTATION);
}

/**
* BFS Recursive Method to register a class and it's inner classes for Reflection.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.it.kotser

import io.quarkus.runtime.Quarkus.run
import io.quarkus.runtime.annotations.QuarkusMain

@QuarkusMain
class GreetingApplication

fun main(args: Array<String>) {
run(*args)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.it.resteasy.reactive.kotlin

import io.quarkus.runtime.Quarkus.run
import io.quarkus.runtime.annotations.QuarkusMain

@QuarkusMain
class GreetingApplication {

companion object {
@JvmStatic
fun main(args: Array<String>) {
run(*args)
}
}
}

0 comments on commit 2846cd2

Please sign in to comment.