[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-dev] Declaring final inter-type fields/methods/etc

Andrew Clement wrote:


Hi Keven,

I was going through the AspectJ bugs earlier on today, and I believe there is one that
covers what you are describing:


Bugzilla Bug 52105: final static constant fields not treated as such

Here is a direct link, there is quite a bit of discussion in the bug report:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=52105

Basically we don't make them final because they get initialized in the aspect rather than
the type upon which you make the declaration. If they were final we wouldn't be able to do this.


The bug is still open and we haven't decided how to resolve it yet ...

Andy.

Thanks.  This appears to be the exact situation I am in.

You mention that they get initialized in the aspect. Uninitialized <static> final variables can be initialized in a static constructor. This means that I can do the following (for instance):

public class MyClass {
 public static final int FOOBAR;

 static {
    FOOBAR = 10;
 }
}

public aspect MyAspect {
   pointcut si(): staticinitialization(MyClass);

 before(): si() {
   System.out.println(MyClass.FOOBAR);
 }

 after(): si() {
   System.out.println(MyClass.FOOBAR);
 }
}

This results in the following output:
0
10

Does this suggest that a possible future solution would be to add before advice to do the initialization and weaving of final variables?

As far as the use case (suggested by Matthew Webster), it goes back to my question a couple of months ago regarding dynamically defining fields such that the fields can be used (specifically) as a branch of a switch statement. As Gregor Kiczales pointed out in that thread, a parser which created an aspect that was then weaved would be a possible solution. Public static final fields (in this case) are (IMO) the cleanest solution. However, they are by no means the only solution.

Thanks again!

--
Keven Ring               | "Oh no,  Not Again..."
The MITRE Corporation    |   Bowl of Petunias -
7515 Colshire Drive      |   The Hitchhikers Guide to the Galaxy
McLean VA 22102-7508     |
PH: (703)883-7026        |