Skip to content

Commit

Permalink
Automagically add no-args constructor to synthetic beans.
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn authored and gsmet committed Aug 31, 2020
1 parent 4b3504b commit 614b91e
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.quarkus.arc.test.unproxyable;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.function.Consumer;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Vetoed;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.deployment.BeanRegistrarBuildItem;
import io.quarkus.arc.processor.BeanRegistrar;
import io.quarkus.builder.BuildChainBuilder;
import io.quarkus.builder.BuildContext;
import io.quarkus.builder.BuildStep;
import io.quarkus.gizmo.MethodDescriptor;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.test.QuarkusUnitTest;

public class SynthProxiableBeanWithoutNoArgConstructorTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(SynthBean.class))
.addBuildChainCustomizer(buildCustomizer());

static Consumer<BuildChainBuilder> buildCustomizer() {
return new Consumer<BuildChainBuilder>() {

@Override
public void accept(BuildChainBuilder builder) {
builder.addBuildStep(new BuildStep() {

@Override
public void execute(BuildContext context) {
context.produce(new BeanRegistrarBuildItem(new BeanRegistrar() {
@Override
public void register(RegistrationContext context) {
context.configure(SynthBean.class)
.scope(ApplicationScoped.class)
.types(SynthBean.class)
.unremovable()
.creator(mc -> {
ResultHandle ret = mc.newInstance(
MethodDescriptor.ofConstructor(SynthBean.class, String.class),
mc.load("foo"));
mc.returnValue(ret);
})
.done();
}
}));
}
}).produces(BeanRegistrarBuildItem.class).build();
}
};
}

@Test
public void testSyntheticBean() {
InstanceHandle<SynthBean> instance = Arc.container().instance(SynthBean.class);
assertTrue(instance.isAvailable());
assertEquals("foo", instance.get().getString());
}

@Vetoed
static class SynthBean {
private String s;

public SynthBean(String s) {
this.s = s;
}

public String getString() {
return s;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,37 @@ static void validateBean(BeanInfo bean, List<Throwable> errors, List<BeanDeploym
}
}
}
} else if (bean.isSynthetic()) {
// this is for synthetic beans that need to be proxied but their classes don't have no-args constructor
ClassInfo beanClass = getClassByName(bean.getDeployment().getIndex(), bean.getBeanClass());
MethodInfo noArgsConstructor = beanClass.method(Methods.INIT);
if (bean.getScope().isNormal() && !Modifier.isInterface(beanClass.flags()) && noArgsConstructor == null) {
if (bean.getDeployment().transformUnproxyableClasses) {
DotName superName = beanClass.superName();
if (!DotNames.OBJECT.equals(superName)) {
ClassInfo superClass = bean.getDeployment().getIndex().getClassByName(beanClass.superName());
if (superClass == null || !superClass.hasNoArgsConstructor()) {
// Bean class extends a class without no-args constructor
// It is not possible to generate a no-args constructor reliably
superName = null;
}
}
if (superName != null) {
String superClassName = superName.toString().replace('.', '/');
bytecodeTransformerConsumer.accept(new BytecodeTransformer(beanClass.name().toString(),
new NoArgConstructorTransformFunction(superClassName)));
} else {
errors.add(new DeploymentException(
"It's not possible to add a synthetic constructor with no parameters to the unproxyable bean class: "
+
beanClass));
}
} else {
errors.add(new DeploymentException(String
.format("Normal scoped beans must declare a non-private constructor with no parameters: %s",
bean)));
}
}
}
}

Expand Down

0 comments on commit 614b91e

Please sign in to comment.