Skip to content

Commit

Permalink
Rework DefaultMethodTest to use InterceptionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Jun 21, 2023
1 parent 38185b6 commit de13ac2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 216 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.enterprise.inject.spi.Extension;
import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
import jakarta.enterprise.inject.spi.WithAnnotations;
import jakarta.enterprise.inject.spi.configurator.BeanConfigurator;

public class InterfaceBasedExtension implements Extension {
private static final Logger LOG = Logger.getLogger(InterfaceBasedExtension.class.getName());
Expand All @@ -39,21 +40,23 @@ public void rememberInterfaces(@Observes @WithAnnotations(RegisterInterfaceBased

public void registerBeans(@Observes AfterBeanDiscovery abd, BeanManager beanManager) {
for (Class<?> iface : interfaces) {
abd.addBean()
.beanClass(iface)
.types(iface)
.scope(Dependent.class)
.qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE, InterfaceBased.Literal.INSTANCE)
.createWith(ctx -> {
Object target = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[] { iface }, InterfaceBasedExtension::invoke);
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[] { iface }, new InterceptingInvocationHandler(iface, target, beanManager));
});
LOG.info("Registered bean for " + iface);
registerBean(iface, abd.addBean(), beanManager);
}
}

private <T> void registerBean(Class<T> beanType, BeanConfigurator<T> beanConfigurator, BeanManager bm) {
beanConfigurator.beanClass(beanType)
.types(beanType)
.scope(Dependent.class)
.qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE, InterfaceBased.Literal.INSTANCE)
.createWith(ctx -> {
T instance = (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[] { beanType }, InterfaceBasedExtension::invoke);
return bm.createInterceptionFactory(ctx, beanType).createInterceptedInstance(instance);
});
LOG.info("Registered bean for " + beanType);
}

private static Object invoke(Object proxy, Method method, Object[] args) {
throw new IllegalArgumentException("Always an exception");
}
Expand Down

0 comments on commit de13ac2

Please sign in to comment.