use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.
the class RuntimeConfigSetupBuildStep method setupRuntimeConfig.
/**
* Generates a StartupTask that sets up the final runtime configuration and thus runs before any StartupTask that uses
* runtime configuration.
* If there are recorders that produce a ConfigSourceProvider, these objects are used to set up the final runtime
* configuration
*/
@BuildStep
@Consume(BootstrapConfigSetupCompleteBuildItem.class)
@Produce(RuntimeConfigSetupCompleteBuildItem.class)
void setupRuntimeConfig(List<RunTimeConfigurationSourceValueBuildItem> runTimeConfigurationSourceValues, BuildProducer<GeneratedClassBuildItem> generatedClass, BuildProducer<MainBytecodeRecorderBuildItem> mainBytecodeRecorder) {
ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, true);
try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput).className(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME).interfaces(StartupTask.class).build()) {
try (MethodCreator method = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
method.invokeVirtualMethod(ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class), method.getMethodParam(0), method.load("RuntimeConfigSetupBuildStep.setupRuntimeConfig"));
ResultHandle config = method.readStaticField(C_INSTANCE);
if (runTimeConfigurationSourceValues.isEmpty()) {
method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config, method.invokeStaticMethod(ofMethod(Collections.class, "emptyList", List.class)));
} else {
ResultHandle startupContext = method.getMethodParam(0);
ResultHandle configSourcesProvidersList = method.newInstance(ofConstructor(ArrayList.class, int.class), method.load(runTimeConfigurationSourceValues.size()));
for (RunTimeConfigurationSourceValueBuildItem runTimeConfigurationSourceValue : runTimeConfigurationSourceValues) {
RuntimeValue<ConfigSourceProvider> runtimeValue = runTimeConfigurationSourceValue.getConfigSourcesValue();
if (runtimeValue instanceof BytecodeRecorderImpl.ReturnedProxy) {
String proxyId = ((BytecodeRecorderImpl.ReturnedProxy) runtimeValue).__returned$proxy$key();
ResultHandle value = method.invokeVirtualMethod(ofMethod(StartupContext.class, "getValue", Object.class, String.class), startupContext, method.load(proxyId));
ResultHandle configSourceProvider = method.invokeVirtualMethod(ofMethod(RuntimeValue.class, "getValue", Object.class), method.checkCast(value, RuntimeValue.class));
method.invokeVirtualMethod(MethodDescriptor.ofMethod(ArrayList.class, "add", boolean.class, Object.class), configSourcesProvidersList, method.checkCast(configSourceProvider, ConfigSourceProvider.class));
} else {
log.warn("RuntimeValue " + runtimeValue + " was not produced by a recorder and it will thus be ignored");
}
}
method.invokeVirtualMethod(RunTimeConfigurationGenerator.C_READ_CONFIG, config, configSourcesProvidersList);
}
method.returnValue(null);
}
}
mainBytecodeRecorder.produce(new MainBytecodeRecorderBuildItem(RUNTIME_CONFIG_STARTUP_TASK_CLASS_NAME));
}
use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.
the class BootstrapConfigSetupBuildStep method setupBootstrapConfig.
/**
* Generates a StartupTask that creates a instance of the generated Config class
* It runs before any StartupTask that uses configuration
*/
@BuildStep
@Produce(BootstrapConfigSetupCompleteBuildItem.class)
void setupBootstrapConfig(BuildProducer<GeneratedClassBuildItem> generatedClass, BuildProducer<MainBytecodeRecorderBuildItem> mainBytecodeRecorder) {
ClassOutput classOutput = new GeneratedClassGizmoAdaptor(generatedClass, true);
try (ClassCreator clazz = ClassCreator.builder().classOutput(classOutput).className(BOOTSTRAP_CONFIG_STARTUP_TASK_CLASS_NAME).interfaces(StartupTask.class).build()) {
try (MethodCreator deploy = clazz.getMethodCreator("deploy", void.class, StartupContext.class)) {
deploy.invokeVirtualMethod(ofMethod(StartupContext.class, "setCurrentBuildStepName", void.class, String.class), deploy.getMethodParam(0), deploy.load("BootstrapConfigSetupBuildStep.setupBootstrapConfig"));
deploy.invokeStaticMethod(C_CREATE_BOOTSTRAP_CONFIG);
deploy.returnValue(null);
}
}
mainBytecodeRecorder.produce(new MainBytecodeRecorderBuildItem(BOOTSTRAP_CONFIG_STARTUP_TASK_CLASS_NAME));
}
use of io.quarkus.runtime.StartupTask in project quarkus by quarkusio.
the class BytecodeRecorderTestCase method runTest.
void runTest(Consumer<BytecodeRecorderImpl> generator, Object... expected) throws Exception {
TestRecorder.RESULT.clear();
TestClassLoader tcl = new TestClassLoader(getClass().getClassLoader());
BytecodeRecorderImpl recorder = new BytecodeRecorderImpl(tcl, false, TEST_CLASS);
generator.accept(recorder);
recorder.writeBytecode(new TestClassOutput(tcl));
StartupTask task = (StartupTask) tcl.loadClass(TEST_CLASS).getDeclaredConstructor().newInstance();
task.deploy(new StartupContext());
assertEquals(expected.length, TestRecorder.RESULT.size());
for (Object i : expected) {
if (i.getClass().isArray()) {
if (i instanceof int[]) {
assertArrayEquals((int[]) i, (int[]) TestRecorder.RESULT.poll());
} else if (i instanceof double[]) {
assertArrayEquals((double[]) i, (double[]) TestRecorder.RESULT.poll(), 0);
} else if (i instanceof Object[]) {
assertArrayEquals((Object[]) i, (Object[]) TestRecorder.RESULT.poll());
} else {
throw new RuntimeException("not implemented");
}
} else {
assertEquals(i, TestRecorder.RESULT.poll());
}
}
}
Aggregations