Skip to content

Commit

Permalink
Merge pull request quarkusio#30980 from geoand/bytecode-recording-sin…
Browse files Browse the repository at this point in the history
…gle-ctor

Make use of @RecordableConstructor optional when there is a single constructor
  • Loading branch information
geoand authored Feb 9, 2023
2 parents 20c459c + d897eef commit dc0444e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1197,21 +1197,30 @@ public void prepare(MethodContext context) {
}
}
} else {
for (Constructor<?> ctor : param.getClass().getConstructors()) {
Constructor<?>[] ctors = param.getClass().getConstructors();
Constructor<?> selectedCtor = null;
if (ctors.length == 1) {
// if there is a single constructor we use it regardless of the presence of @RecordableConstructor annotation
selectedCtor = ctors[0];
}
for (Constructor<?> ctor : ctors) {
if (RecordingAnnotationsUtil.isRecordableConstructor(ctor)) {
nonDefaultConstructorHolder = new NonDefaultConstructorHolder(ctor, null);
nonDefaultConstructorHandles = new DeferredParameter[ctor.getParameterCount()];

if (ctor.getParameterCount() > 0) {
Parameter[] ctorParameters = ctor.getParameters();
for (int i = 0; i < ctor.getParameterCount(); ++i) {
String name = ctorParameters[i].getName();
constructorParamNameMap.put(name, i);
}
}
selectedCtor = ctor;
break;
}
}
if (selectedCtor != null) {
nonDefaultConstructorHolder = new NonDefaultConstructorHolder(selectedCtor, null);
nonDefaultConstructorHandles = new DeferredParameter[selectedCtor.getParameterCount()];

if (selectedCtor.getParameterCount() > 0) {
Parameter[] ctorParameters = selectedCtor.getParameters();
for (int i = 0; i < selectedCtor.getParameterCount(); ++i) {
String name = ctorParameters[i].getName();
constructorParamNameMap.put(name, i);
}
}
}
}

Set<String> handledProperties = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ public void testRecordableConstructor() throws Exception {
TestRecorder recorder = generator.getRecordingProxy(TestRecorder.class);
recorder.bean(bean);
}, new OtherTestConstructorBean("Jane", "Citizen").setAge(30));

runTest(generator -> {
TestSingleConstructorBean bean = new TestSingleConstructorBean("Jane", "Citizen", 30);
TestRecorder recorder = generator.getRecordingProxy(TestRecorder.class);
recorder.bean(bean);
}, new TestSingleConstructorBean("Jane", "Citizen", 30));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void bean(OtherTestConstructorBean bean) {
RESULT.add(bean);
}

public void bean(TestSingleConstructorBean bean) {
RESULT.add(bean);
}

public void result(RuntimeValue<TestJavaBean> bean) {
RESULT.add(bean.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.deployment.recording;

import java.util.Objects;

public class TestSingleConstructorBean {

private final String first;
private final String last;
private final int age;

public TestSingleConstructorBean(String first, String last, int age) {
this.first = first;
this.last = last;
this.age = age;
}

public String getFirst() {
return first;
}

public String getLast() {
return last;
}

public int getAge() {
return age;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
TestSingleConstructorBean that = (TestSingleConstructorBean) o;
return age == that.age &&
Objects.equals(first, that.first) &&
Objects.equals(last, that.last);
}

@Override
public int hashCode() {
return Objects.hash(first, last, age);
}
}

0 comments on commit dc0444e

Please sign in to comment.