Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] AspectJ allows classes to implement classes and classes to extend interfaces

Hi,

I have noticed that AspectJ allows you to have classes implement classes
and have classes extend interfaces, both of which are illegal in Java.

Example:

public class A {

    public void a(){
        System.out.println("a()");
    }

    public static void main(String[] args) {
        new A().a();
    }
}

public class B {

    public void b(){
        System.out.println("b()");
    }
}

public interface I {

    public void i();
}

public aspect ClassExtendsInterface {

    declare parents : A extends I;

	public void A.i(){
	    System.out.println("i()");
	}
	
	after (A a) returning : target(a) && call(* A.a()) {
	    a.i();
	}
}

public aspect ImplementClass {
    declare parents : A implements B;

	public void A.b(){
	    super.b();
	}
	
	after (A a) returning : target(a) && call(* A.a()) {
	    a.b();
	}
}

This set of classes/aspects compiles and produces the following output:

a()
i()
b()

This appears to work because the inherits and extends relationships are
treated as
the same by the Abstract Syntax Model. Both are treated as a
DECLARE_PARENTS kind and then
reflection is used to determine the exact relationship for the "details"
string of the node so when you print out the node it correctly says that "A extends B"
and "A implements I". Treating these relationships as the same makes
sense if you think  of an interface as an abstract class that contains
only abstract methods.

Even though everything gets sorted out in the end (or appears to), what cases are there where this could be a problem? Why does the compiler/weaver not catch this when it is parsing the files? Perhaps someone more familiar with the compilation process has an idea?

(I found this problem when I was looking at the ModelCoverage test case
in the module ajde.testdata.examples.coverage. There is an aspect that has Point
extend Serializable and implement Observable).

John Anvik








Back to the top