The issue the compiler indicated is that you are trying to change
*every* class in the world, even java.lang.Object, to extend an interface. I
believe there are fundamental VM issues with doing that.
Are you trying to add methods to classes in the Java runtime? Or are you
trying to add them to *your* classes?
For the latter, you could use package structure, e.g., declare parent:
org.mystuff..* implements Writable;
If you really want to extend the Java runtime, you could try writing an
introduction (ITD) on java.lang.Object, and then weave into rt.jar. I think that
would work, but it's not a common thing to do (and you need to look at the
license for your Java runtime; it may be a violation).
------------Original Message------------
From: Andr�Dantas Rocha <ad-rocha@xxxxxxxxxx>
To: <aspectj-users@xxxxxxxxxxx>
Date: Wed, Apr-21-2004 9:08 AM
Subject: RES: [aspectj-users] Method introduction
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