[cdi-dev] [JBoss JIRA] (CDI-686) Could InterceptionFactory accept an interface as type parameter

Xavier Dury (JIRA) issues at jboss.org
Tue Sep 18 03:50:00 EDT 2018


    [ https://issues.jboss.org/browse/CDI-686?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13634642#comment-13634642 ] 

Xavier Dury edited comment on CDI-686 at 9/18/18 3:49 AM:
----------------------------------------------------------

Workaround:

{code:java}
public interface MyService { ... }

public abstract class MyAbstractService implements MyService {} // purely abstract

public final class MyServiceImpl extends MyAbstractService { ... }

@Produces
@ApplicationScoped
public MyService myService(InterceptionFactory<MyAbstractService> factory) {
    factory.configure().add(new AnnotationLiteral<Transactional>() {});
    return factory.createInterceptedInstance(new MyServiceImpl(...));
}
{code}

It's a bit ridiculous (and you lose your one-shot at inheritance) but it demonstrates that if it works with a purely abstract class, there's no solid reason why it couldn't work with interfaces.



was (Author: xdury):
Workaround:

{code:java}
public interface MyService { ... }

public abstract class MyAbstractService implements MyService {} // purely abstract

public final class MyServiceImpl extends MyAbstractService { ... }

@Produces
@ApplicationScoped
public MyService myService(InterceptionFactory<MyAbstractService> factory) {
    factory.configure().add(new AnnotationLiteral<Transactional>() {});
    return factory.createInterceptedInstance(new MyServiceImpl(...));
}
{code}

It's a bit ridiculous (and you lose your own-shot at inheritance) but it demonstrates that if it works with a purely abstract class, there's no solid reason why it couldn't work with interfaces.


> Could InterceptionFactory accept an interface as type parameter
> ---------------------------------------------------------------
>
>                 Key: CDI-686
>                 URL: https://issues.jboss.org/browse/CDI-686
>             Project: CDI Specification Issues
>          Issue Type: Clarification
>    Affects Versions: 2.0 .Final
>            Reporter: Antoine Sabot-Durand
>            Assignee: Antoine Sabot-Durand
>             Fix For: 2.0 .Final
>
>
> If you take this code:
> {code:java}
>     @Produces
>     public List<Object> produceList(InterceptionFactory<List<Object>> interceptionFactory) {
>         interceptionFactory.ignoreFinalMethods().configure().filterMethods((m) -> {
>             if (m.getJavaMember().getName().equals("add")
>                     && m.getJavaMember().getParameterCount() == 1) {
>                 return true;
>             }
>             return false;
>         }).findFirst().get().add(Monitor.Literal.INSTANCE);
>         return interceptionFactory.createInterceptedInstance(new ArrayList<>());
>     }
> {code}
> Parameterized type for injected {{InterceptionFactory}} is an interface {{List<Object>}}, so when calling {{configure()}}, user will work with an {{AnnotatedTypeConfigurator<List<Object>>}} to apply interceptor binding.
> In a standard interceptor usage, interceptor binding on interface are ignored (even if they have {{@Inherited}} annotation), so doing it with {{InterceptionFactory}} could be confusing for some user.



--
This message was sent by Atlassian JIRA
(v7.5.0#75005)


More information about the cdi-dev mailing list