From 9d40de78c06a527088b7fdc57698009f25b8ef23 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 8 Jan 2024 17:15:24 +0200 Subject: [PATCH] Support Java Records in bytecode recorders --- .../deployment/recording/PropertyUtils.java | 8 ++++++++ .../extest/deployment/TestRecordProcessor.java | 16 ++++++++++++++++ .../extest/runtime/records/TestRecord.java | 4 ++++ .../runtime/records/TestRecordRecorder.java | 13 +++++++++++++ .../it/extension/TestRecordRecorderTest.java | 18 ++++++++++++++++++ 5 files changed, 59 insertions(+) create mode 100644 integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/TestRecordProcessor.java create mode 100644 integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecord.java create mode 100644 integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecordRecorder.java create mode 100644 integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/TestRecordRecorderTest.java diff --git a/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java b/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java index 02d3cf4dd5153..25959c196c0a2 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/recording/PropertyUtils.java @@ -2,7 +2,9 @@ package io.quarkus.deployment.recording; import java.lang.reflect.Method; +import java.lang.reflect.RecordComponent; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -19,6 +21,12 @@ final class PropertyUtils { private static final Function, Property[]> FUNCTION = new Function, Property[]>() { @Override public Property[] apply(Class type) { + if (type.isRecord()) { + RecordComponent[] recordComponents = type.getRecordComponents(); + return Arrays.stream(recordComponents) + .map(rc -> new Property(rc.getName(), rc.getAccessor(), null, rc.getType())).toArray(Property[]::new); + } + List ret = new ArrayList<>(); Method[] methods = type.getMethods(); diff --git a/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/TestRecordProcessor.java b/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/TestRecordProcessor.java new file mode 100644 index 0000000000000..047690092decf --- /dev/null +++ b/integration-tests/test-extension/extension/deployment/src/main/java/io/quarkus/extest/deployment/TestRecordProcessor.java @@ -0,0 +1,16 @@ +package io.quarkus.extest.deployment; + +import io.quarkus.deployment.annotations.BuildStep; +import io.quarkus.deployment.annotations.ExecutionTime; +import io.quarkus.deployment.annotations.Record; +import io.quarkus.extest.runtime.records.TestRecord; +import io.quarkus.extest.runtime.records.TestRecordRecorder; + +public class TestRecordProcessor { + + @BuildStep + @Record(ExecutionTime.RUNTIME_INIT) + public void record(TestRecordRecorder recorder) { + recorder.record(new TestRecord("foo", 100)); + } +} diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecord.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecord.java new file mode 100644 index 0000000000000..3c2a46ceb4725 --- /dev/null +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecord.java @@ -0,0 +1,4 @@ +package io.quarkus.extest.runtime.records; + +public record TestRecord(String name, int age) { +} diff --git a/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecordRecorder.java b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecordRecorder.java new file mode 100644 index 0000000000000..c8a848b5955e0 --- /dev/null +++ b/integration-tests/test-extension/extension/runtime/src/main/java/io/quarkus/extest/runtime/records/TestRecordRecorder.java @@ -0,0 +1,13 @@ +package io.quarkus.extest.runtime.records; + +import io.quarkus.runtime.annotations.Recorder; + +@Recorder +public class TestRecordRecorder { + + public static TestRecord testRecord; + + public void record(TestRecord testRecord) { + TestRecordRecorder.testRecord = testRecord; + } +} diff --git a/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/TestRecordRecorderTest.java b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/TestRecordRecorderTest.java new file mode 100644 index 0000000000000..0d9992b922c16 --- /dev/null +++ b/integration-tests/test-extension/tests/src/test/java/io/quarkus/it/extension/TestRecordRecorderTest.java @@ -0,0 +1,18 @@ +package io.quarkus.it.extension; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import io.quarkus.extest.runtime.records.TestRecordRecorder; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +public class TestRecordRecorderTest { + + @Test + public void test() { + assertEquals("foo", TestRecordRecorder.testRecord.name()); + assertEquals(100, TestRecordRecorder.testRecord.age()); + } +}