You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/07/10 14:21:48 UTC

[camel-quarkus] branch main updated: Ref #5056: Improve the replacement of RecorderContext#classProxy (#5077)

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

nfilotto 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 d804d670d2 Ref #5056: Improve the replacement of RecorderContext#classProxy (#5077)
d804d670d2 is described below

commit d804d670d23ce309c6e6a6511f5d0b1cc8fc4a6c
Author: Nicolas Filotto <es...@users.noreply.github.com>
AuthorDate: Mon Jul 10 16:21:42 2023 +0200

    Ref #5056: Improve the replacement of RecorderContext#classProxy (#5077)
    
    ## Motivation
    
    The first proposal to replace `RecorderContext#classProxy` works but is suboptimal, let's use the approach recommended by Quarkus Team members.
    
    ## Modifications:
    
    * Removes the common recorder with a method allowing to delay the loading of the generated classes
    * Loads the class in the recorder methods
---
 .../language/runtime/LanguageSupportRecorder.java  | 28 ----------------------
 .../groovy/deployment/GroovyProcessor.java         |  6 ++---
 .../groovy/runtime/GroovyExpressionRecorder.java   |  5 ++--
 .../component/joor/deployment/JoorProcessor.java   | 10 ++++----
 .../joor/runtime/JoorExpressionRecorder.java       | 18 +++++++++-----
 5 files changed, 21 insertions(+), 46 deletions(-)

diff --git a/extensions-support/language/runtime/src/main/java/org/apache/camel/quarkus/support/language/runtime/LanguageSupportRecorder.java b/extensions-support/language/runtime/src/main/java/org/apache/camel/quarkus/support/language/runtime/LanguageSupportRecorder.java
deleted file mode 100644
index 41b9796190..0000000000
--- a/extensions-support/language/runtime/src/main/java/org/apache/camel/quarkus/support/language/runtime/LanguageSupportRecorder.java
+++ /dev/null
@@ -1,28 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.quarkus.support.language.runtime;
-
-import io.quarkus.runtime.RuntimeValue;
-import io.quarkus.runtime.annotations.Recorder;
-
-@Recorder
-public class LanguageSupportRecorder {
-
-    public RuntimeValue<Class<?>> loadClass(String name) throws ClassNotFoundException {
-        return new RuntimeValue<>(Class.forName(name, true, Thread.currentThread().getContextClassLoader()));
-    }
-}
diff --git a/extensions/groovy/deployment/src/main/java/org/apache/camel/quarkus/component/groovy/deployment/GroovyProcessor.java b/extensions/groovy/deployment/src/main/java/org/apache/camel/quarkus/component/groovy/deployment/GroovyProcessor.java
index 183200f5a0..53d5324d80 100644
--- a/extensions/groovy/deployment/src/main/java/org/apache/camel/quarkus/component/groovy/deployment/GroovyProcessor.java
+++ b/extensions/groovy/deployment/src/main/java/org/apache/camel/quarkus/component/groovy/deployment/GroovyProcessor.java
@@ -41,7 +41,6 @@ import org.apache.camel.quarkus.support.language.deployment.ExpressionBuildItem;
 import org.apache.camel.quarkus.support.language.deployment.ExpressionExtractionResultBuildItem;
 import org.apache.camel.quarkus.support.language.deployment.ScriptBuildItem;
 import org.apache.camel.quarkus.support.language.runtime.ExpressionUID;
-import org.apache.camel.quarkus.support.language.runtime.LanguageSupportRecorder;
 import org.apache.camel.quarkus.support.language.runtime.ScriptUID;
 import org.codehaus.groovy.control.CompilationUnit;
 import org.codehaus.groovy.control.CompilerConfiguration;
@@ -149,9 +148,8 @@ class GroovyProcessor {
     @BuildStep(onlyIf = NativeBuild.class)
     CamelBeanBuildItem configureLanguage(
             GroovyExpressionRecorder recorder,
-            LanguageSupportRecorder languageRecorder,
             ExpressionExtractionResultBuildItem result,
-            List<GroovyExpressionSourceBuildItem> sources) throws ClassNotFoundException {
+            List<GroovyExpressionSourceBuildItem> sources) {
 
         if (result.isSuccess() && !sources.isEmpty()) {
             RuntimeValue<GroovyLanguage.Builder> builder = recorder.languageBuilder();
@@ -159,7 +157,7 @@ class GroovyProcessor {
                 recorder.addScript(
                         builder,
                         source.getOriginalCode(),
-                        languageRecorder.loadClass(source.getClassName()));
+                        source.getClassName());
             }
             final RuntimeValue<GroovyLanguage> language = recorder.languageNewInstance(builder);
             return new CamelBeanBuildItem("groovy", GroovyLanguage.class.getName(), language);
diff --git a/extensions/groovy/runtime/src/main/java/org/apache/camel/quarkus/component/groovy/runtime/GroovyExpressionRecorder.java b/extensions/groovy/runtime/src/main/java/org/apache/camel/quarkus/component/groovy/runtime/GroovyExpressionRecorder.java
index e0ee300344..756a1812f3 100644
--- a/extensions/groovy/runtime/src/main/java/org/apache/camel/quarkus/component/groovy/runtime/GroovyExpressionRecorder.java
+++ b/extensions/groovy/runtime/src/main/java/org/apache/camel/quarkus/component/groovy/runtime/GroovyExpressionRecorder.java
@@ -29,9 +29,10 @@ public class GroovyExpressionRecorder {
     }
 
     @SuppressWarnings("unchecked")
-    public void addScript(RuntimeValue<GroovyLanguage.Builder> builder, String content, RuntimeValue<Class<?>> clazz) {
+    public void addScript(RuntimeValue<GroovyLanguage.Builder> builder, String content, String className) {
         try {
-            builder.getValue().addScript(content, (Class<Script>) clazz.getValue());
+            builder.getValue().addScript(content,
+                    (Class<Script>) Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
diff --git a/extensions/joor/deployment/src/main/java/org/apache/camel/quarkus/component/joor/deployment/JoorProcessor.java b/extensions/joor/deployment/src/main/java/org/apache/camel/quarkus/component/joor/deployment/JoorProcessor.java
index a6378ab77a..03ef3755a7 100644
--- a/extensions/joor/deployment/src/main/java/org/apache/camel/quarkus/component/joor/deployment/JoorProcessor.java
+++ b/extensions/joor/deployment/src/main/java/org/apache/camel/quarkus/component/joor/deployment/JoorProcessor.java
@@ -53,7 +53,6 @@ import org.apache.camel.quarkus.support.language.deployment.ExpressionBuildItem;
 import org.apache.camel.quarkus.support.language.deployment.ExpressionExtractionResultBuildItem;
 import org.apache.camel.quarkus.support.language.deployment.ScriptBuildItem;
 import org.apache.camel.quarkus.support.language.runtime.ExpressionUID;
-import org.apache.camel.quarkus.support.language.runtime.LanguageSupportRecorder;
 import org.apache.camel.quarkus.support.language.runtime.ScriptUID;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -158,10 +157,9 @@ class JoorProcessor {
     CamelBeanBuildItem configureLanguage(
             JoorExpressionConfig config,
             JoorExpressionRecorder recorder,
-            LanguageSupportRecorder languageRecorder,
             CamelContextBuildItem context,
             ExpressionExtractionResultBuildItem result,
-            List<JoorExpressionSourceBuildItem> sources) throws ClassNotFoundException {
+            List<JoorExpressionSourceBuildItem> sources) {
 
         if (result.isSuccess() && !sources.isEmpty()) {
             final RuntimeValue<JoorExpressionCompiler.Builder> expressionCompilerBuilder = recorder
@@ -176,13 +174,13 @@ class JoorProcessor {
                             expressionScriptingCompilerBuilder,
                             camelContext,
                             source.getId(),
-                            languageRecorder.loadClass(source.getClassName()));
+                            source.getClassName());
                 } else {
                     recorder.addExpression(
                             expressionCompilerBuilder,
                             camelContext,
                             source.getId(),
-                            languageRecorder.loadClass(source.getClassName()));
+                            source.getClassName());
                 }
             }
             final RuntimeValue<JoorLanguage> language = recorder.languageNewInstance(config, expressionCompilerBuilder,
@@ -191,7 +189,7 @@ class JoorProcessor {
             if (config.resultType.isPresent()) {
                 recorder.setResultType(
                         language,
-                        languageRecorder.loadClass(config.resultType.get()));
+                        config.resultType.get());
             }
 
             return new CamelBeanBuildItem("joor", JoorLanguage.class.getName(), language);
diff --git a/extensions/joor/runtime/src/main/java/org/apache/camel/quarkus/component/joor/runtime/JoorExpressionRecorder.java b/extensions/joor/runtime/src/main/java/org/apache/camel/quarkus/component/joor/runtime/JoorExpressionRecorder.java
index 62d47febce..21a5c71e36 100644
--- a/extensions/joor/runtime/src/main/java/org/apache/camel/quarkus/component/joor/runtime/JoorExpressionRecorder.java
+++ b/extensions/joor/runtime/src/main/java/org/apache/camel/quarkus/component/joor/runtime/JoorExpressionRecorder.java
@@ -36,8 +36,12 @@ public class JoorExpressionRecorder {
         return language;
     }
 
-    public void setResultType(RuntimeValue<JoorLanguage> language, RuntimeValue<Class<?>> resultType) {
-        language.getValue().setResultType(resultType.getValue());
+    public void setResultType(RuntimeValue<JoorLanguage> language, String className) {
+        try {
+            language.getValue().setResultType(Class.forName(className, true, Thread.currentThread().getContextClassLoader()));
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
     }
 
     public RuntimeValue<JoorExpressionCompiler.Builder> expressionCompilerBuilder() {
@@ -49,20 +53,22 @@ public class JoorExpressionRecorder {
     }
 
     public void addExpression(RuntimeValue<JoorExpressionCompiler.Builder> builder, RuntimeValue<CamelContext> ctx, String id,
-            RuntimeValue<Class<?>> clazz) {
+            String className) {
         try {
             builder.getValue().addExpression(id,
-                    (JoorMethod) clazz.getValue().getConstructor(CamelContext.class).newInstance(ctx.getValue()));
+                    (JoorMethod) Class.forName(className, true, Thread.currentThread().getContextClassLoader())
+                            .getConstructor(CamelContext.class).newInstance(ctx.getValue()));
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
     public void addScript(RuntimeValue<JoorExpressionScriptingCompiler.Builder> builder, RuntimeValue<CamelContext> ctx,
-            String id, RuntimeValue<Class<?>> clazz) {
+            String id, String className) {
         try {
             builder.getValue().addScript(id,
-                    (JoorScriptingMethod) clazz.getValue().getConstructor(CamelContext.class).newInstance(ctx.getValue()));
+                    (JoorScriptingMethod) Class.forName(className, true, Thread.currentThread().getContextClassLoader())
+                            .getConstructor(CamelContext.class).newInstance(ctx.getValue()));
         } catch (Exception e) {
             throw new RuntimeException(e);
         }