Skip to content

Commit

Permalink
ArC - producers - change the client proxy class package
Browse files Browse the repository at this point in the history
- use the package of the return type/field type
- previously, the package of the declaring class was used
- resolves quarkusio#22815
  • Loading branch information
mkouba committed Feb 11, 2022
1 parent 749ebb0 commit e089471
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.quarkus.arc.test.unproxyable;

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

import java.io.IOException;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Singleton;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.test.unproxyable.some.Resource;
import io.quarkus.test.QuarkusUnitTest;

// This test aims to test the https://github.com/quarkusio/quarkus/issues/22815 in Quarkus integration
// There is a duplicate test for ArC standalone: io.quarkus.arc.test.clientproxy.constructor.ProducerReturnTypePackagePrivateNoArgsConstructorTest
public class ProducerReturnTypePackagePrivateNoArgsConstructorTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addClasses(ProducerReturnTypePackagePrivateNoArgsConstructorTest.class, ResourceProducer.class,
Resource.class));

@Inject
Instance<Resource> instance;

@Test
public void testProducer() throws IOException {
assertTrue(instance.isResolvable());
assertEquals(5, instance.get().ping());
}

@Singleton
static class ResourceProducer {

@ApplicationScoped
@Produces
Resource resource() {
return Resource.from(5);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.arc.test.unproxyable.some;

public abstract class Resource {

Resource() {
}

public static Resource from(int ping) {
return new Resource() {

@Override
public int ping() {
return ping;
}
};
}

public abstract int ping();

}
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ private List<BeanInfo> findBeans(Collection<DotName> beanDefiningAnnotations, Li

// non-inherited stuff:
for (MethodInfo method : beanClass.methods()) {
if (Methods.isSynthetic(method)) {
if (method.isSynthetic()) {
continue;
}
if (annotationStore.getAnnotations(method).isEmpty()) {
Expand All @@ -926,7 +926,7 @@ private List<BeanInfo> findBeans(Collection<DotName> beanDefiningAnnotations, Li
while (aClass != null) {
for (MethodInfo method : aClass.methods()) {
Methods.MethodKey methodDescriptor = new Methods.MethodKey(method);
if (Methods.isSynthetic(method) || Methods.isOverriden(methodDescriptor, methods)) {
if (method.isSynthetic() || Methods.isOverriden(methodDescriptor, methods)) {
continue;
}
methods.add(methodDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1796,7 +1796,7 @@ protected void implementIsSuppressed(BeanInfo bean, ClassCreator beanCreator) {

private String getProxyTypeName(BeanInfo bean, String baseName) {
StringBuilder proxyTypeName = new StringBuilder();
proxyTypeName.append(bean.getTargetPackageName());
proxyTypeName.append(bean.getClientProxyPackageName());
if (proxyTypeName.length() > 0) {
proxyTypeName.append(".");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,22 @@ public String getTargetPackageName() {
return packageName;
}

public String getClientProxyPackageName() {
if (isProducerField() || isProducerMethod()) {
AnnotationTarget target = getTarget().get();
DotName typeName = target.kind() == Kind.FIELD ? target.asField().type().name()
: target.asMethod().returnType().name();
String packageName = DotNames.packageName(typeName);
if (packageName.startsWith("java.")) {
// It is not possible to place a class in a JDK package
packageName = AbstractGenerator.DEFAULT_PACKAGE;
}
return packageName;
} else {
return getTargetPackageName();
}
}

void validate(List<Throwable> errors, List<BeanDeploymentValidator> validators,
Consumer<BytecodeTransformer> bytecodeTransformerConsumer, Set<DotName> classesReceivingNoArgsCtor) {
Beans.validateBean(this, errors, validators, bytecodeTransformerConsumer, classesReceivingNoArgsCtor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Collection<Resource> generate(BeanInfo bean, String beanClassName,
ProviderType providerType = new ProviderType(bean.getProviderType());
ClassInfo providerClass = getClassByName(bean.getDeployment().getBeanArchiveIndex(), providerType.name());
String baseName = getBaseName(bean, beanClassName);
String targetPackage = bean.getTargetPackageName();
String targetPackage = bean.getClientProxyPackageName();
String generatedName = generatedNameFromTarget(targetPackage, baseName, CLIENT_PROXY_SUFFIX);
if (existingClasses.contains(generatedName)) {
return Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ final class Methods {
public static final String INIT = "<init>";
// static initializer
public static final String CLINIT = "<clinit>";
// copied from java.lang.reflect.Modifier.SYNTHETIC
static final int SYNTHETIC = 0x00001000;
// copied from java.lang.reflect.Modifier.BRIDGE
static final int BRIDGE = 0x00000040;

public static final String TO_STRING = "toString";

private static final List<String> IGNORED_METHODS = initIgnoredMethods();
Expand All @@ -60,10 +59,6 @@ private static List<String> initIgnoredMethods() {
private Methods() {
}

static boolean isSynthetic(MethodInfo method) {
return (method.flags() & SYNTHETIC) != 0;
}

static boolean isBridge(MethodInfo method) {
return (method.flags() & BRIDGE) != 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.arc.test.clientproxy.constructor;

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

import io.quarkus.arc.Arc;
import io.quarkus.arc.ClientProxy;
import io.quarkus.arc.test.ArcTestContainer;
import io.quarkus.arc.test.clientproxy.constructor.some.Resource;
import java.io.IOException;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Singleton;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

// This test aims to test the https://github.com/quarkusio/quarkus/issues/22815 in ArC standalone
// There is a duplicate test for Quarkus integration: io.quarkus.arc.test.unproxyable.ProducerReturnTypePackagePrivateNoArgsConstructorTest
public class ProducerReturnTypePackagePrivateNoArgsConstructorTest {

@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(ResourceProducer.class);

@Test
public void testProducer() throws IOException {
Resource res = Arc.container().instance(Resource.class).get();
assertNotNull(res);
assertTrue(res instanceof ClientProxy);
assertEquals(5, res.ping());
}

@Singleton
static class ResourceProducer {

@ApplicationScoped
@Produces
Resource resource() {
return Resource.from(5);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.arc.test.clientproxy.constructor.some;

public abstract class Resource {

Resource() {
}

public static Resource from(int ping) {
return new Resource() {

@Override
public int ping() {
return ping;
}
};
}

public abstract int ping();

}

0 comments on commit e089471

Please sign in to comment.