4 Replies Latest reply on Oct 27, 2016 7:15 PM by ljnelson

    Specializing a bean you don't control?

    ljnelson

      How do you specialize a bean you don't control? I hope this is a sensible question.

       

      What I mean is: suppose a colleague hands me a bean archive with a FrobnicatorImpl bean in it (that implements Frobnicator).

       

      Suppose further I don't want to, or cannot, know anything about FrobnicatorImpl, but I do know what it means to implement Frobnicator. Moreover, Is like to "fully replace" all usages of FrobnicatorImpl with  MyFrobnicator.

       

      I know that I would make MyFrobnicator an alternative. But I am fuzzy about how to avoid the traps mentioned in the spec around alternatives. Specialization, if I am reading things right, which is designed to avoid the traps, says that MyFrobnicator would have to extend FrobnicatorImpl. But what would I do if extending that class is not an option?

       

      I know this is a rookie question and so am prepared for Pointers To Elsewhere™. Thanks!

        • 1. Re: Specializing a bean you don't control?
          tremes

          Hi Laird,

          That's a good question. TBH only the solution using an alternative came to my mind. I was also thinking about  vetoing other beans in portable extension, but it's not very convenient here. Using an alternative is really easier.

          • 2. Re: Specializing a bean you don't control?
            ljnelson

            So to sum up (correct me where I'm wrong):

            • You can only specialize if you can subclass.
            • Specialization is mainly to work around producer method problems (if the bean you're specializing defines producer methods that "make" other objects).
            • The case I described cannot be fully handled by @Alternatives, but it's as good as we've got right now

             

            Is that accurate?

            • 3. Re: Specializing a bean you don't control?
              tremes
              • Yes
              • I would say not only "producer method problems". Specialization of bean without any producer could be sometimes helpful as well (e.g Mocks).
              • I think it could be doable with @Alternative. You can select the alternative for the entire app by using @Priority (I guess you read it:-)). Did you try it?
              • 4. Re: Specializing a bean you don't control?
                ljnelson

                Tomas Remes wrote:

                 

                • Yes
                • I would say not only "producer method problems". Specialization of bean without any producer could be sometimes helpful as well (e.g Mocks).
                • I think it could be doable with @Alternative. You can select the alternative for the entire app by using @Priority (I guess you read it:-)). Did you try it?

                Not yet.  I was under the impression that I shouldn't really try it this way, for all the reasons that cause specialization to exist.  That is, selecting the alternative for the entire app by using @Priority doesn't obviate the need for specialization, as I understood the reference documentation (Chapter 13. Specialization, inheritance and alternatives ) in section 13.2.  But I can give it a shot!