Skip to main content

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


Hi John,

As you point out, AspectJ doesn't police which is specified in the source - you can use either 'extends' or 'implements'.  In fact the choice you made in the source is not captured in the associated attribute on the class file that represents your declare parents statement.  This means when binary weaving we have no idea which you specified and I think we default to 'extends' - this is why that piece of code is in the the structure model building logic which has a look at the types involved to see if 'implements' is more likely.

Whichever you specify, weaving will execute correctly.  Personally I'd rather not completely forget what the user specifies - I'd like to include it in the attribute so the structure model can be correct - but I'm not sure we can go as far as policing it.  I don't know the history of why we decided not to, but it may have something to do with the use of type patterns in the declare parents statement.  Look at this case:

interface I {}

class X implements I {}

interface IMarker {}

aspect A {
  declare parents: I+ [implements? or extends?]  IMarker;
}

Now:
- because 'I' and 'IMarker' are interfaces, it should be extends.
- because 'X' is a class (which matches 'I+') and 'IMarker' is an interface, it should be implements.

So both are possibly correct...  I think we leave it up to the programmer to simply express how they are thinking about the types involved when they write the program.  I don't think its possible to come up with a case that will cause an error because we ignore what was specified.  Perhaps the only error scenario we could think about reporting is when the 'new parent' is definetly a class and yet they have specified 'implements'.

regards,
Andy.
---
Andy Clement
AspectJ/AJDT Development



John Anvik <janvik@xxxxxxxxx>
Sent by: aspectj-dev-admin@xxxxxxxxxxx

01/09/2004 01:07

Please respond to
aspectj-dev@xxxxxxxxxxx

To
aspectj-dev@xxxxxxxxxxx
cc
Subject
[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






_______________________________________________
aspectj-dev mailing list
aspectj-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/aspectj-dev


Back to the top