Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] how to do cuckoo's egg with shared interface?

Title: how to do cuckoo's egg with shared interface?

The UML diagram of the Cuckoos Egg pattern (AspectJ Cookbook, recipe 23.1) shows an optional SharedInterface interface between the OriginalClass and the ReplacementClass, and the documentation states that the ReplacementClass must inherit from the type expected on the constructor call. In this example, that type is the SharedInterface interface.

I dont believe this variant of the pattern can be achieved in AspectJ.

Observe the following example:

public class Toy {

        public static void main( String[] args ) {

                Amusement a = new AmusementImpl1();

                System.out.println( a.getClass() );

        }

}

interface Amusement {}

class AmusementImpl1 implements Amusement {}

class AmusementImpl2 implements Amusement {}

abstract aspect Amusing {

        abstract pointcut cuckooEggPC();

        Object around() : cuckooEggPC() && !cflow( this( Amusing+ ) ) {

                return new AmusementImpl2();

        }

}

// Warning: advice defined in Amusing has not been applied

aspect InterfaceCallNew extends Amusing { pointcut cuckooEggPC() : call( Amusement.new( .. ) ); } 

aspect InterfaceExecNew extends Amusing { pointcut cuckooEggPC() : execution( Amusement.new( .. ) ); }

// Exception in thread "main" java.lang.ClassCastException: com.amazon.imds.aggregator.aspects.AmusementImpl2

aspect InterfacePlusCallNew extends Amusing { pointcut cuckooEggPC() : call( Amusement+.new( .. ) ); }

aspect ClassCallNew extends Amusing { pointcut cuckooEggPC() : call( AmusementImpl1.new( .. ) ); }

// No cuckoo egg effect

aspect InterfacePlusExecNew extends Amusing { pointcut cuckooEggPC() : execution( Amusement+.new( .. ) ); }

aspect ClassExecNew extends Amusing { pointcut cuckooEggPC() : execution( AmusementImpl1.new( .. ) ); }

I would like to be able to swap implementations of the interface without knowing which particular implementation was being swapped out.  Can it be done?

Josh


Back to the top