5 Replies Latest reply on Jan 6, 2005 2:43 PM by john howard

    Duplicate method names in mixins

    john howard Newbie

      Is there any way to support duplicate method names in mixin interfaces. I have an introduction that has two mixins and I only seem to be able to get this to work if none of the interfaces have methods of the same name.

      Is this expected behaviour?

      I was trying to extend a model so that I could both support a tree view and a graphical workflow view, both interfaces having the method getChildren(). I'm new to AOP so I'm not sure if this is an appropriate use of the technology, just seemed like a good idea at the time.

      Thanks,

      John.

      Example:

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <aop>
       <introduction class="test.HelloWorld">
       <mixin>
       <interfaces>
       test.Test
       </interfaces>
       <class>test.HelloWorldImpl</class>
       <construction>new test.HelloWorldImpl(this)</construction>
       </mixin>
       <mixin>
       <interfaces>
       test.Test1
       </interfaces>
       <class>test.HelloWorldImpl1</class>
       <construction>new test.HelloWorldImpl1(this)</construction>
       </mixin>
       </introduction>
      
      </aop>
      public interface Test {
       String getWorldName();
      }
      public interface Test1 {
       String getWorldName();
      }
      



        • 1. Re: Duplicate method names in mixins
          Kabir Khan Master

          Hi,

          the method names would need to be different. The mixin methods get compiled into the main class, so for the examples you show you would end up with something like:

          public HelloWorld implements Test, Test1
          {
           //Original HelloWorld code
          
           //Added during instrumentation
           Test test = new HelloWorldImpl(this);
           Test1 test1 = new HelloWorldImpl1(this);
          
           public String getWorldName()
           {
           return test.getWorldName();
           }
          
           public String getWorldName()
           {
           return test1.getWorldName();
           }
          
          }
          


          So you end up with two methods with the same signature, which is not allowed. This is similar to if in "normal" java you wanted to implement two interfaces with the same method names; you would only implement one "copy" of the method.

          Kabir

          • 2. Re: Duplicate method names in mixins
            Kabir Khan Master

            It should be possible to create a mixin that implements BOTH interfaces though

            • 3. Re: Duplicate method names in mixins
              john howard Newbie

              Hi Kabir,

              Thanks for the reply, now I understand the implementation I understand the problem. Does make it kind of restrictive for AOP though, perhaps Java should be extended to support multiple inheritance of this type. I saw some discussion on a Java forum suggesting that calls could be namespaced using the interface that they represent e.g.

               public String Test.getWorldName()
               {
               return test.getWorldName();
               }
              
               public String Test1.getWorldName()
               {
               return test1.getWorldName();
               }
              
              


              Seemed a neat solution.

              John.

              • 4. Re: Duplicate method names in mixins
                Bill Burke Master

                You don't want multi-inheritance in Java. Then you have to worry about a lot of things like how classes are initialized and such. The virtual vs. non-virtual decision that C++ has.

                We made the decision in JBoss AOP to have non-virtual inheritance.

                • 5. Re: Duplicate method names in mixins
                  john howard Newbie

                  I remember C++ multiple inheritance well, since converting to Java, Coplien's purple book sits quietly gathering dust on a shelf.

                  On the project I was working on I had been using IAdaptable to extend basic java classes so that I could use them in a tree view and a graphical view. As a first look at AOP I thought I could improve on this via an introduction and a mixin. I still have two mixin classes one for the tree view and one for the graphical view, so from my code's point of view there is no multiple inheritance and I have no desire to go back there. I don't really know if this is an appropriate use of AOP, maybe not, but it does work well and simplifies the code. If it is appropriate the maybe AOP should support it in some way, in my case I could change the method signatures, but that won't always be the case.

                  Just wondered if what I am doing is sensible?

                  Thanks,

                  John.