Thanks for your help Ron, your aspect is very
useful.
I
would like to do this in all classes, so I modified the inter-type
declaration:
declare parents: * implements Writable;
But now the
AspectJ generates a compilation error:
can't change the parents of
java.lang.Object AddMethodIfNotDefined.java GUI/fatt line
9
How can I do it?
Thanks,
Andre
Try using declare parents to make the target class(es) extend an interface,
then introduce the method on the interface. E.g.,
public aspect AddMethodIfNotDefined { private interface
Writable {} public void Writable.write()
{ System.out.println("default
write"); }
declare parents: *Writer implements Writable;
public static void main(String args[])
{ new
AWriter().write(); new
BWriter().write(); } }
class AWriter { public void write()
{ System.out.println("A
write"); } }
class BWriter { }
java AddMethodIfNotDefined A write default write
...
Ron
Ron Bodkin
Chief Technology Officer
New Aspects of Software
o: (415) 824-4690
m: (415) 509-2895
------------Original Message------------
From: Andr�Dantas Rocha <ad-rocha@xxxxxxxxxx>
To: <aspectj-users@xxxxxxxxxxx>
Date: Tue, Apr-20-2004 10:40 PM
Subject: [aspectj-users] Method introduction
Hi,
I would like to
introduce a method only if it do not already exists in the target class.
Is it
possible?
Thanks,
Andre
|