Skip to content

Commit

Permalink
WELD-2758 Proxies for non-public class-based beans should reside in b…
Browse files Browse the repository at this point in the history
…ean's package if possible
  • Loading branch information
manovotn committed Sep 26, 2023
1 parent 6b129ea commit d6729a0
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ private static ProxyNameHolder createCompoundProxyName(String contextId, Bean<?>
}
interfaces.add(mostSpecificClass.getSimpleName());
}
// if the bean class is a non-public one (i.e. pack private), we prioritize placing proxy in the same package
// we skip built-in beans are those are often for jakarta.* classes and end up in Weld's default package anyway
if (proxyPackage == null && bean != null
&& !Modifier.isPublic(bean.getBeanClass().getModifiers())
&& !(bean instanceof AbstractBuiltInBean)) {
proxyPackage = bean.getBeanClass().getPackage().getName();
}
final Set<String> declaringClasses = new HashSet<>();
for (Class<?> type : typeInfo.getInterfaces()) {
Class<?> declaringClass = type.getDeclaringClass();
Expand All @@ -271,7 +278,7 @@ private static ProxyNameHolder createCompoundProxyName(String contextId, Bean<?>
proxyPackage = typeInfo.getPackageNameForClass(type);
}
}
// no need to sort the set, because we copied and already sorted one
// no need to sort the set, because we copied already sorted set
Iterator<String> iterator = interfaces.iterator();
while (iterator.hasNext()) {
name.append(iterator.next());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jboss.weld.tests.classDefining.packPrivate;

import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.test.util.Utils;
import org.jboss.weld.tests.classDefining.packPrivate.api.Alpha;
import org.jboss.weld.tests.classDefining.packPrivate.interceptor.MyInterceptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* See WELD-2758
*/
@RunWith(Arquillian.class)
public class ProxyForInterceptedPackagePrivateBeanTest {

@Deployment
public static Archive<?> deploy() {
return ShrinkWrap
.create(BeanArchive.class, Utils.getDeploymentNameAsHash(ProxyForInterceptedPackagePrivateBeanTest.class))
.addPackages(true, ProxyForInterceptedPackagePrivateBeanTest.class.getPackage());
}

@Inject
Instance<Object> instance;

@Test
public void testProxyCanBeCreated() {
Instance<Alpha> select = instance.select(Alpha.class);
Assert.assertTrue(select.isResolvable());
Assert.assertEquals(MyInterceptor.class.getSimpleName() + Alpha.class.getSimpleName(), select.get().ping());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jboss.weld.tests.classDefining.packPrivate.api;

public interface Alpha {

String ping();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.weld.tests.classDefining.packPrivate.impl;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Typed;

import org.jboss.weld.tests.classDefining.packPrivate.api.Alpha;
import org.jboss.weld.tests.classDefining.packPrivate.interceptor.SomeBinding;

/**
* Class is intentionally package private, @Typed to just the interface type and normal scoped to enforce client proxy
*/
@ApplicationScoped
@SomeBinding
@Typed(Alpha.class)
class AlphaImpl implements Alpha {
@Override
public String ping() {
return Alpha.class.getSimpleName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jboss.weld.tests.classDefining.packPrivate.interceptor;

import jakarta.annotation.Priority;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InvocationContext;

@Interceptor
@SomeBinding
@Priority(Interceptor.Priority.APPLICATION)
public class MyInterceptor {

@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
return this.getClass().getSimpleName() + context.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jboss.weld.tests.classDefining.packPrivate.interceptor;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import jakarta.interceptor.InterceptorBinding;

@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface SomeBinding {
}

0 comments on commit d6729a0

Please sign in to comment.