Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Intertype declaration with methods returning instances of the class itself - correction

> What I mean was
>  		   public (@MyInterface *) getInstance() { 
>                          return new (@MyInterface *)();
>                  }
>          }
> >         Is the previous possible?

You can declare a method

  aspect A { 
    public MyInterface MyInterface.getInstance() { ... }
  }

But there's no macro facility in AspectJ to generate code
of the form

   aspect A {
     public {name} {name}.getInstance() { ... }
   }

   class A {
     public A getInstance() {...}
   }
   class B {
     public B getInstance() {...}
   }

For factories, you'd like to write type-specific 
declarations and implementations anyway, e.g., here on C:  

  aspect A_forC {
    public C C.getInstance() { return new C(); }
  }


Using a superinterface and covariant return types can help
you specify a policy for all factories.  You can even
segregate results and factories if you want separate 
implementations:

  interface Result {}

  interface Factory {
     Result getInstance();
  }

  aspect A  {
    // default implementation
    public Result Factory.getInstance() { 
      throw new UnsupportedOperationException(); 
    }
  }

  // C is both result and factory
  class C implements Result; 
  aspect A_forC {
    declare parents: C implements Factory;
    // note covariant return type C for Result
    public C C.getInstance() { 
      return new C(); 
    }
  }

  // D is factory for B
  class B implements Result {}
  aspect A_forB {
    declare parents: D implements Factory;
    public B D.getInstance() { 
      return new B(); 
    }
  }

(warning: see bug 128443 for why I declared interfaces
directly on B,D: 
  https://bugs.eclipse.org/bugs/show_bug.cgi?id=128443
)

Then you can implement any policies you like on all
factories.

e.g., to have only one result per factory instance, 

  interface Factory {
    Result getInstance();
    pointcut factory() : 
      execution(Result Factory.getInstance());
    // AspectJ 5 recognizes covariant return types, so you
    // don't have to say "Result+" in the execution
pointcut
  }

  aspect FactorySingleton perinstance(factory()) {
     Factory result;
     Factory around() : Factory.factory() {
       if (null == result) {
         result = proceed();
       }
       return result; 
    }
  }

Does that make sense?

Wes


On Fri, 17 Feb 2006 14:04:58 +0000
 Paulo Alexandre Corigo Zenida <paulo.zenida@xxxxxxxx>
wrote:
> Em Sexta, 17 de Fevereiro de 2006 09:54, o Paulo
> Alexandre Corigo Zenida 
> escreveu:
> > Hi all,
> >
> >         I wonder if, in Intertype declaration, is it
> possible for me to add
> > a method to any class with a common characteristic
> (annotated with
> > something, for example, that returns an instance of the
> class itself? To
> > better understand what I want, let me show an example:
> >
> >         @MyAnnotation
> >         public class A {
> >
> >         }
> >
> >         @MyAnnotation
> >         public class B {
> >
> >         }
> >
> >         public aspect MyAspect {
> >                 
> >                 // this does not compile
> >                 public void (@MyAnnotation *).foo() {
> >
> >                 }
> >
> >                 // So, I have tried this:
> >                 declare parents : (@MyAnnotation *)
> implements MyInterface;
> >
> >                 interface MyInterface {
> >
> >                 }
> >
> >                 // this works ok
> >                 public void MyInterface.foo() {
> >                         System.out.println("In foo");
> >                 }
> >
> >                 // But this does not compile! I would
> like to create a
> >                 // method returning an instance of the
> class itself. 
> > 		   // Any ideas about how to do this? 
> > 		   public (@Singleton *) getInstance() { 
> >                         return new (@Singleton *)();
> >                 }
> What I mean was
>  		   public (@MyInterface *) getInstance() { 
>                          return new (@MyInterface *)();
>                  }
>          }
> >
> >         Is the previous possible?
> >
> >         Thanks in advance. Cheers,
> >
> >                 Paulo Zenida



Back to the top