This is an automated email from the ASF dual-hosted git repository.

jamesnetherton pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/main by this push:
     new 5bc7d3f  Fix Gradle native builds for Spring backed extensions
5bc7d3f is described below

commit 5bc7d3f4609b155f0c58adc87b0542417ec5134e
Author: James Netherton <[email protected]>
AuthorDate: Fri Mar 18 15:13:51 2022 +0000

    Fix Gradle native builds for Spring backed extensions
    
    Fixes #3617
---
 .../spring/deployment/SpringKotlinProcessor.java   | 69 +++++++++++-----------
 1 file changed, 36 insertions(+), 33 deletions(-)

diff --git 
a/extensions-support/spring/deployment/src/main/java/org/apache/camel/quarkus/support/spring/deployment/SpringKotlinProcessor.java
 
b/extensions-support/spring/deployment/src/main/java/org/apache/camel/quarkus/support/spring/deployment/SpringKotlinProcessor.java
index ebf81d7..d6f6eeb 100644
--- 
a/extensions-support/spring/deployment/src/main/java/org/apache/camel/quarkus/support/spring/deployment/SpringKotlinProcessor.java
+++ 
b/extensions-support/spring/deployment/src/main/java/org/apache/camel/quarkus/support/spring/deployment/SpringKotlinProcessor.java
@@ -16,54 +16,57 @@
  */
 package org.apache.camel.quarkus.support.spring.deployment;
 
+import io.quarkus.bootstrap.model.ApplicationModel;
 import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
 import io.quarkus.deployment.annotations.BuildProducer;
 import io.quarkus.deployment.annotations.BuildStep;
 import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
+import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
 import io.quarkus.deployment.pkg.steps.NativeBuild;
 import io.quarkus.gizmo.ClassCreator;
 
 public class SpringKotlinProcessor {
 
     @BuildStep(onlyIf = NativeBuild.class)
-    void generateKotlinReflectClasses(BuildProducer<GeneratedClassBuildItem> 
generatedClass) {
+    void generateKotlinReflectClasses(
+            BuildProducer<GeneratedClassBuildItem> generatedClass,
+            CurateOutcomeBuildItem curateOutcome) {
+
         // TODO: Investigate removing this. See 
https://github.com/apache/camel-quarkus/issues/534
         // The native image build fails with a NoClassDefFoundError without 
this. Possibly similar to https://github.com/oracle/graal/issues/656.
 
-        try {
-            Class.forName("kotlin.reflect.KParameter");
-        } catch (ClassNotFoundException e) {
-            ClassCreator.builder()
-                    .className("kotlin.reflect.KParameter")
-                    .classOutput(new 
GeneratedClassGizmoAdaptor(generatedClass, false))
-                    .setFinal(true)
-                    .superClass(Object.class)
-                    .build()
-                    .close();
+        // If Kotlin is on the application classpath we don't need to do 
anything.
+        // This check is preferable to trying to discover Kotlin via 
Class.forname etc,
+        // which is not reliable for Gradle builds as kotlin-stdlib is part of 
the Gradle distribution.
+        // Thus such classes will be discoverable at build time but not at 
runtime, which leads to native build issues.
+        ApplicationModel model = curateOutcome.getApplicationModel();
+        if (isKotlinStdlibAvailable(model)) {
+            return;
         }
 
-        try {
-            Class.forName("kotlin.reflect.KCallable");
-        } catch (ClassNotFoundException e) {
-            ClassCreator.builder()
-                    .className("kotlin.reflect.KCallable")
-                    .classOutput(new 
GeneratedClassGizmoAdaptor(generatedClass, false))
-                    .setFinal(false)
-                    .superClass(Object.class)
-                    .build()
-                    .close();
-        }
+        createClass(generatedClass, "kotlin.reflect.KParameter", 
Object.class.getName(), true);
+        createClass(generatedClass, "kotlin.reflect.KCallable", 
Object.class.getName(), false);
+        createClass(generatedClass, "kotlin.reflect.KFunction", 
"kotlin.reflect.KCallable", false);
+    }
 
-        try {
-            Class.forName("kotlin.reflect.KFunction");
-        } catch (ClassNotFoundException e) {
-            ClassCreator.builder()
-                    .className("kotlin.reflect.KFunction")
-                    .classOutput(new 
GeneratedClassGizmoAdaptor(generatedClass, false))
-                    .setFinal(false)
-                    .superClass("kotlin.reflect.KCallable")
-                    .build()
-                    .close();
-        }
+    private boolean isKotlinStdlibAvailable(ApplicationModel applicationModel) 
{
+        return applicationModel
+                .getDependencies()
+                .stream()
+                .anyMatch(dependency -> 
dependency.getArtifactId().startsWith("kotlin-stdlib"));
+    }
+
+    private void createClass(
+            BuildProducer<GeneratedClassBuildItem> generatedClass,
+            String clasName,
+            String superClassName,
+            boolean isFinal) {
+        ClassCreator.builder()
+                .className(clasName)
+                .classOutput(new GeneratedClassGizmoAdaptor(generatedClass, 
false))
+                .setFinal(isFinal)
+                .superClass(superClassName)
+                .build()
+                .close();
     }
 }

Reply via email to