Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] the codes cannot be compiled.

At the constructor call join point (new RegSingleton()), the contract
with the caller is that an instance of RegSingleton will be returned.
However, your around advice only guarantees that a Singleton will be
returned, hence the advice is not guaranteed to honour the original
contract and the compiler complains. The simplest change to your
program in this case is to change the return type of the around advice
to "Object". I know that sounds counter-intuitive given the
explanation I just gave, but Object is treated specially when used as
the return type of around advice and means "always return whatever the
underlying join point is specified to return". This is done to enable
the writing of generic (used here in the non-Java 5 sense) advice that
applies across a range of join points. Your program illustrates one
such use as (I imagine) you may have many different subtypes of
Singleton and want to write one piece of advice that applies across
all of them.

The behaviour of around advice when returning Object is described
briefly in the semantics guide here
http://www.eclipse.org/aspectj/doc/released/progguide/semantics-advice.html,
and also in section 7.1.3 of the Eclipse AspectJ book.

Regards, Adrian.

On 11/09/05, Guofeng Zhang <guofeng@xxxxxxxxxxxxx> wrote:
> The compiler for The line 7 and Line 12 complains that
>      [iajc] D:\workdir\DPsample2\main\src\com\designpattern\singleton\RegSinglet
> onProtocol.aj:12 [error] incompatible return type applying to constructor-call(v
> oid com.designpattern.singleton.RegSingleton.<init>())
>      [iajc]
>      [iajc] D:\workdir\DPsample2\main\src\com\designpattern\singleton\Main.java:
> 7 [error] incompatible return type applying to constructor-call(void com.designp
> attern.singleton.RegSingleton.<init>())
>      [iajc] System.out.println( new RegSingleton() );
>      [iajc] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>      [iajc] D:\workdir\DPsample2\main\src\com\designpattern\singleton\RegSinglet
> onProtocol.aj:12 [warning] advice defined in com.designpattern.singleton.RegSing
> letonProtocol has not been applied [Xlint:adviceDidNotMatch]
>      [iajc] Singleton around() : call((Singleton+).new(..))
>      [iajc]           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> public aspect RegSingletonProtocol {
>     private Hashtable singletons=new Hashtable();
>     public interface Singleton{}
>     declare parents : RegSingleton implements Singleton;
>     Singleton around() : call((Singleton+).new(..))    // line 12
>     {
>         Class singleton=thisJoinPoint.getSignature().getDeclaringType();
>         if(singletons.get(singleton)==null){
>            singletons.put(singleton, proceed());
>         }
>         return (Singleton)singletons.get(singleton);
>     }
> }
> //
> public class Main
> {
>  public static void main(String [] args)
>  {
>   System.out.println( new RegSingleton() );   // Line 7
>  }
> }
> //
> public class RegSingleton
> {
>  private static int count=0;
>  public RegSingleton()
>  {
>      count++;
>      System.out.println("Class No. "+count);
>  }
> }
> 
> What's wrong with the above codes.
> 
> Thanks
> 
> guofeng
> 
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
> 


-- 
-- Adrian
adrian.colyer@xxxxxxxxx


Back to the top