Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Re: A switch oddity

Hi -

>>> So, basically, a final Java field
>>>cannot be declared in AspectJ, right?

It can, but there seem to be bugs or limitations...

The code below compiles fine using 1.1.1 or the
current tree, but when run throws the exception
below.  It runs fine if the inter-type declaration
is directly on the target class, rather than on the
interface.

This looks like a problem with implementing final fields
on interfaces (which in Java can't have fields).  I don't
think that because the access specifiers (public, private,
etc.) of an ITD are relative to the aspect, that
final is somehow relative to the aspect; the semantics
of final should be preserved, subject to implementation
limitations.  This is another thing that might not be
possible to do perfectly when weaving Java bytecode.

So it's a bug either way.  We should implement it if we can
for top-level subtypes, or the compiler should signal an
error and we should document the limitation.

Wes

------- output at runtime
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.Runnable.ajc$i
nterFieldSet$A$java_lang_Runnable$name(Ljava/lang/String;)V
        at A.ajc$interFieldInit$A$java_lang_Runnable$name(JunkTest.java:11)
        at JunkTest.<init>(JunkTest.java:4)
        at JunkTest.main(JunkTest.java:6)

------- JunkTest.java
public class JunkTest implements Runnable {
    public static void main(String[] args) {
        new JunkTest().blah();
    }
    public void run() {
    }
}
aspect A {
    public final String Runnable.name = "Runnable";
    public void Runnable.blah() {
        int i = name.length();
        System.out.println("got " + name + i);
    }
}
-------


Charles Zhang wrote:

But this has different effect than declaring inside the Java class as
Matthew pointed out earlier. For example,
 aspect A {
     public final String Runnable.name = "Runnable";
     void Runnable.Run(int len){
       switch(len) {
       case name.length: //do something
    }
   }
}

This won't even pass compilation, although if "name" is declared same way
in class rather than in aspects, this code is perfectly legal. I don't
know if this is intended in AspectJ.


Charles

On Tue, 10 Feb 2004, Wes Isberg wrote:


aspect A {

   public final String Runnable.name = "Runnable";
}

Charles Zhang wrote:


Hey, Matthew, thanks for the reply. So, basically, a final Java field
cannot be declared in AspectJ, right? For my application, I can always get
around. But I can't think of philosophically why aspects should be
prohibited from doing so. Could this be a bug or an intended behaviour?
Thanks again.

Charles

On Tue, 10 Feb 2004, Matthew Webster wrote:





Charles,

Modifiers on inter-type declarations refer only to usage by the aspect and
do no determine the actual Java visibility. For example a private ITD field
is actually public WRT Java but given a mangled name so that it cannot be
used by other types. The same applies to final i.e. a final ITD field can
only be set in the scope of an initializer in the aspect that declares it.
These rules are policed by the AspectJ compiler. A final ITD field could
not be declared final WRT Java because otherwise its value could not be set
in the scope of the aspect (static final fields can only be set in a static
initializer and final fields in a constructor).

The implementation of final has some unfortunate side effects as you have
discovered.

Matthew Webster
AOSD Project
Java Technology Centre, MP146
IBM Hursley Park, Winchester,  SO21 2JN, England
Telephone: +44 196 2816139 (external) 246139 (internal)
Email: Matthew Webster/UK/IBM @ IBMGB, matthew_webster@xxxxxxxxxx
http://w3.hursley.ibm.com/~websterm/

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


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


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


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




Back to the top