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

Matej Novotny (JIRA) issues at jboss.org
Wed Sep 12 09:01:04 EDT 2018


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

Matej Novotny commented on CDI-686:
-----------------------------------

So, the reason why {{InterceptionFactory}} doesn't work on interfaces is basically spec chapter [4. Inheritance and specialization|http://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#inheritance], which says:

bq. Type-level metadata is never inherited from interfaces implemented by a bean.

This means that adding the interceptor binding on interface won't affect the actual bean. Hence the interception would not work.

bq. If I declare InterceptionFactory<ExternalServiceImplementation> instead...

Well, here it's obviously because of proxyability requirements as stated in the [{{createInterceptedInstance()}} docs|http://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#interception_factory] - cannot work with {{final}} type. You could work around {{final}} methods with {{ignoreFinalMethods()}} switch though.

What you could do is probably use a proxyable class in between the interface and the implementation and base the interception on that. Could even be an abstract class I think.

> 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