10 Replies Latest reply on Feb 17, 2010 4:44 PM by koichik0818

    Scope type without @Scope or @NormalScope

    koichik0818

      CDI spec (2.4.2. Defining new scope types) says



      All scope types must also specify the @javax.inject.Scope or @javax.enterprise.context.NormalScope meta-annotation


      But a scope type without the @Scope or @NormalScope works well.


      1) declare scope type without @Scope or @NormalScope.


      @Target( { TYPE, METHOD, FIELD })
      @Retention(RUNTIME)
      public @interface SomeScoped {}
      


      2) register the scope.


      beforeBeanDiscovery.addScope(SomeScoped.class, true, false);
      



      3) register the context.


      afterBeanDiscovery.addContext(new SomeContext());
      



      I think that the @SomeScoped should become Error If all scope types must specify @Scope or @NormalScope.


      But Weld accept the @SomeScoped if I register it (step 2).


      Which is right?

        • 1. Re: Scope type without @Scope or @NormalScope
          nickarls

          If this it the case, I would call it a bug. Could you file a JIRA?

          • 2. Re: Scope type without @Scope or @NormalScope
            koichik0818

            Does that mean it's a Weld's bug?
            If so, I have another question.


            CDI spec (2.4.2. Defining new scope types) says



            Custom scopes are normally defined by portable extensions, which must also provide a context object, as defined in Section 6.2, 'The Context interface', that implements the custom scope.


            But, I can use a scope type without register (I must register only context).


            Is this portable? Depends on implementation(Weld)? or Weld's bug?

            • 3. Re: Scope type without @Scope or @NormalScope
              nickarls

              Without digging that much into the spec, I'm guessing:


              They are not linked. Context.getScope() returns the scope type but I'm not sure if the container is obliged to check if it's a registered scope. Of course, if you intend to use it normally @FooScoped, you should register it.

              • 4. Re: Scope type without @Scope or @NormalScope
                koichik0818

                Thanks for the reply, but I want to know the correct interpretation of CDI spec.


                I think that the Weld's behavior is rational.


                1) I do not have to register a scope type with @Scope or @NormalScope.
                2) I must register a scope type without @Scope or @NormalScope.


                However, 2) does not match CDI spec.
                Then I do not have to call BBD#addScope() when using Weld.


                In CDI spec, when do I have to register a scope type?
                Why is there BBD#addScope()?

                • 5. Re: Scope type without @Scope or @NormalScope
                  pmuir

                  Koichi Kobayashi wrote on Feb 10, 2010 02:21:


                  Thanks for the reply, but I want to know the correct interpretation of CDI spec.

                  I think that the Weld's behavior is rational.

                  1) I do not have to register a scope type with @Scope or @NormalScope.
                  2) I must register a scope type without @Scope or @NormalScope.

                  However, 2) does not match CDI spec.
                  Then I do not have to call BBD#addScope() when using Weld.

                  In CDI spec, when do I have to register a scope type?
                  Why is there BBD#addScope()?



                  Both (1) and (2) are correct and are contained in the spec. addScope() has the effect of causing the CDI implementation to see the annotation as having @Scope/@NormalScope.

                  • 6. Re: Scope type without @Scope or @NormalScope
                    koichik0818

                    Pete Muir wrote on Feb 17, 2010 12:23:



                    Both (1) and (2) are correct and are contained in the spec.


                    (2) means that I can use a scope type without @Scope or @NormalScope.


                    for example,


                    @Target( { TYPE, METHOD, FIELD })
                    @Retention(RUNTIME)
                    public @interface BadScoped {}
                    



                    I can use the scope type if (and only if) I register it.


                    public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) {
                        bbd.addScope(BadScoped.class, true, false);
                    }
                    



                    But CDI spec (2.4.2. Defining new scope types) says



                    All scope types must also specify the @javax.inject.Scope or @javax.enterprise.context.NormalScope meta-annotation


                    Therefor, @BadScoped Should become error, shouldn't it?

                    • 7. Re: Scope type without @Scope or @NormalScope
                      pmuir

                      This is not the intent of the spec, I guess it just needs some clarification - Gavin reads these forums, so will pick it up.

                      • 8. Re: Scope type without @Scope or @NormalScope
                        gavin.king

                        Pete Muir wrote on Feb 17, 2010 12:23:



                        Both (1) and (2) are correct and are contained in the spec. addScope() has the effect of causing the CDI implementation to see the annotation as having @Scope/@NormalScope.


                        Right, exactly.

                        • 9. Re: Scope type without @Scope or @NormalScope
                          gavin.king

                          There is no problem here, the whole SPI is understood to be about overriding and messing with the annotation-based metamodel. That's the point of it.

                          • 10. Re: Scope type without @Scope or @NormalScope
                            koichik0818

                            Thanks for the reply. I understood it.