Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArC - producers - change the client proxy class package #22828

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -895,7 +895,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 @@ -922,7 +922,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 @@ -1793,7 +1793,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.")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall in Weld we were replacing a regex of java.* meaning we'd replace even javax.something etc. And latest version also does that for jakarta package but TBF I don't remember why we chose/needed to do that...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBF I don't remember why we chose/needed to do that...

Exactly. That's the reason why we only keep java. for now ;-).

// 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 @@ -84,7 +84,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();

}