Skip to content

Commit

Permalink
Ref #5056: Replace the deprecated RecorderContext#classProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
essobedo authored and jamesnetherton committed Jul 7, 2023
1 parent bcf6bd4 commit 279944f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.pkg.steps.NativeBuild;
import io.quarkus.deployment.recording.RecorderContext;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.PathCollection;
import io.quarkus.runtime.RuntimeValue;
Expand All @@ -42,6 +41,7 @@
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;
Expand Down Expand Up @@ -145,14 +145,11 @@ void compileScriptsAOT(CurateOutcomeBuildItem curateOutcomeBuildItem,
}
}

// We still need to use RecorderContext#classProxy as using i.e. Class.forName does not work
// at runtime. See https://github.com/apache/camel-quarkus/issues/5056
@SuppressWarnings("deprecation")
@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIf = NativeBuild.class)
CamelBeanBuildItem configureLanguage(
RecorderContext recorderContext,
GroovyExpressionRecorder recorder,
LanguageSupportRecorder languageRecorder,
ExpressionExtractionResultBuildItem result,
List<GroovyExpressionSourceBuildItem> sources) throws ClassNotFoundException {

Expand All @@ -162,7 +159,7 @@ CamelBeanBuildItem configureLanguage(
recorder.addScript(
builder,
source.getOriginalCode(),
recorderContext.classProxy(source.getClassName()));
languageRecorder.loadClass(source.getClassName()));
}
final RuntimeValue<GroovyLanguage> language = recorder.languageNewInstance(builder);
return new CamelBeanBuildItem("groovy", GroovyLanguage.class.getName(), language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public RuntimeValue<GroovyLanguage.Builder> languageBuilder() {
}

@SuppressWarnings("unchecked")
public void addScript(RuntimeValue<GroovyLanguage.Builder> builder, String content, Class<?> clazz) {
public void addScript(RuntimeValue<GroovyLanguage.Builder> builder, String content, RuntimeValue<Class<?>> clazz) {
try {
builder.getValue().addScript(content, (Class<Script>) clazz);
builder.getValue().addScript(content, (Class<Script>) clazz.getValue());
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.pkg.PackageConfig;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.deployment.recording.RecorderContext;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.paths.PathCollection;
import io.quarkus.runtime.RuntimeValue;
Expand All @@ -54,6 +53,7 @@
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;
Expand Down Expand Up @@ -152,16 +152,13 @@ void compileExpressions(
}
}

// We still need to use RecorderContext#classProxy as using i.e. Class.forName does not work
// at runtime. See https://github.com/apache/camel-quarkus/issues/5056
@SuppressWarnings("deprecation")
@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIf = CompileAtBuildTime.class)
@Consume(CamelContextBuildItem.class)
CamelBeanBuildItem configureLanguage(
RecorderContext recorderContext,
JoorExpressionConfig config,
JoorExpressionRecorder recorder,
LanguageSupportRecorder languageRecorder,
CamelContextBuildItem context,
ExpressionExtractionResultBuildItem result,
List<JoorExpressionSourceBuildItem> sources) throws ClassNotFoundException {
Expand All @@ -179,13 +176,13 @@ CamelBeanBuildItem configureLanguage(
expressionScriptingCompilerBuilder,
camelContext,
source.getId(),
recorderContext.classProxy(source.getClassName()));
languageRecorder.loadClass(source.getClassName()));
} else {
recorder.addExpression(
expressionCompilerBuilder,
camelContext,
source.getId(),
recorderContext.classProxy(source.getClassName()));
languageRecorder.loadClass(source.getClassName()));
}
}
final RuntimeValue<JoorLanguage> language = recorder.languageNewInstance(config, expressionCompilerBuilder,
Expand All @@ -194,7 +191,7 @@ CamelBeanBuildItem configureLanguage(
if (config.resultType.isPresent()) {
recorder.setResultType(
language,
recorderContext.classProxy(config.resultType.get()));
languageRecorder.loadClass(config.resultType.get()));
}

return new CamelBeanBuildItem("joor", JoorLanguage.class.getName(), language);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public RuntimeValue<JoorLanguage> languageNewInstance(JoorExpressionConfig confi
return language;
}

public void setResultType(RuntimeValue<JoorLanguage> language, Class<?> resultType) {
language.getValue().setResultType(resultType);
public void setResultType(RuntimeValue<JoorLanguage> language, RuntimeValue<Class<?>> resultType) {
language.getValue().setResultType(resultType.getValue());
}

public RuntimeValue<JoorExpressionCompiler.Builder> expressionCompilerBuilder() {
Expand All @@ -49,21 +49,20 @@ public RuntimeValue<JoorExpressionScriptingCompiler.Builder> expressionScripting
}

public void addExpression(RuntimeValue<JoorExpressionCompiler.Builder> builder, RuntimeValue<CamelContext> ctx, String id,
Class<?> clazz) {
RuntimeValue<Class<?>> clazz) {
try {
builder.getValue().addExpression(id,
(JoorMethod) clazz.getConstructor(CamelContext.class).newInstance(ctx.getValue()));
(JoorMethod) clazz.getValue().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,
Class<?> clazz) {
String id, RuntimeValue<Class<?>> clazz) {
try {
builder.getValue().addScript(id,
(JoorScriptingMethod) clazz.getConstructor(CamelContext.class).newInstance(ctx.getValue()));
(JoorScriptingMethod) clazz.getValue().getConstructor(CamelContext.class).newInstance(ctx.getValue()));
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 279944f

Please sign in to comment.