Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Ok, I have another crazy scenario:

This is a long but simple question, please bear with me:

public class A
{
	private B m_b;
	
	public void method()
	{
		m_b = new B( "Test" );
		m_b.method();
	}
}

public class B extends D
{
	public B(String string)
	{
	}

	public void method()
	{
	}
}

public class C extends D
{
	public C(String string)
	{
	}

	public void method()
	{
	}
}

My original question was:
Is it possible to change A so that when it tries to do this:

		m_b = new B( "Test" );
		m_b.method();		
		
it actually effectively does this:

		m_b = new C( "Test" );
		m_b.method(); // calls C's method()		

David Vollbracht (thanks!) gave this solution, which works:

aspect BToC
{
   declare parents: C extends B;

   /**
    * No-arg constructor for class B so Class C doesn't
    * need to be changed
    */
   public B.new()
   {
      this( "Dummy String" );
   }

   pointcut createB( String string ) :
      call( B.new( String ) ) && args( string );

   B around( String string ) : createB( string )
   {
      return new C( string );
   }
}

Here's the question (finally!).  Is there a way within the 'around' part
to still allow the 
creation of 'B' (see example above) and get access to that instance?
Something like this:

   B around( String string ) : createB( string )
   {
	B b = ...some way to get access to the originally intended
object instance B...

      return new C( string );
   }


Thanks for any help!

Ted



Back to the top