Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] staticinitializer

Hi -

You may have forgotten that x is static:

> ..easiest way that I know of is that either all the class inherit from the
> same parent class that would contain the attribute x [...]
> ...or you define an interface
> that contains x and that is the target of the operation.

In Java, if the parent class or interface has a static variable x, any 
reference to x refers to the same value.  References to x via A, B, or C
(or unqualified references from within the parent class) would not have 
different values as desired:

> > I have three classes A, B, and C. In all three classes the static
> > variable x appears. 
...
> my
> goal is to write one line of code that causes A.x = "A", that causes B.x =
> "B", and that causes C.x = "C".

The following works in AspectJ 1.0 (not 1.1), but it's not good code:

---------- InitStatic.java (1.0-only code)

public class InitStatic {
    public static void main (String[] args) {
        System.out.println("A.x: " + A.x);
        System.out.println("B.x: " + B.x);
        System.out.println("C.x: " + C.x);
    } 
}

class A{}
class B{}
class C{}

aspect AA {
    static String (A || B || C).x = "to be replaced";

    Object around(String arg) : 
        set(static String (A||B||C).x) && args(arg) {
        String className = thisJoinPointStaticPart.getSignature()
                           .getDeclaringType().getName();
       return proceed(className);
    }
}
---------- 

One could of course define an instance variable in a supertype
and use it to store per-class state; that was the original approach
for Log4J per-class logging on the old aspectJ mailing list.
It wastes space/time, but the code is easier to read and applies 
to all subtypes.

For 1.1, I believe that pertype aspects have not made it;
that would be the right solution here, though I don't think
it would reduce the need to declare each:

    static String A.x = "...";
    static String B.x = "...";
    static String C.x = "...";

Of course, declaring each means you can use the literal reference
(or access other static members):

    static final Logger A.logger = Logger.getLogger(A.class);

Wes



"DiFrango, Ron" wrote:
> 
> Jake,
> 
> The easiest way that I know of is that either all the class inherit from the
> same parent class that would contain the attribute x and you define your
> pointcut using that parent class as the target or you define and interface
> that contains x and that is the target of the operation.  Without seeing
> your aspect and the classes A, B and C, it hard for me to give you more
> concrete example.
> 
> I hope this helps.
> 
> Later,
> 
> Ron
> 
> -----Original Message-----
> From: Jake Whitehill [mailto:jrw@xxxxxxxx]
> Sent: Thursday, February 06, 2003 4:14 AM
> To: 'aspectj-users@xxxxxxxxxxx'
> Subject: RE: [aspectj-users] staticinitializer
> 
> Hi Ron,
> 
> Thanks for your response. I actually knew how to obtain the r-value, but
> it's the l-value that I'm unsure about: what expression can I enter to refer
> to each of A, B, and C (from my example) at the same time? Keep in mind, my
> goal is to write one line of code that causes A.x = "A", that causes B.x =
> "B", and that causes C.x = "C".
> 
> Any ideas?
> 
> Jake
> 
> On Wed, 5 Feb 2003, DiFrango, Ron wrote:
> 
> > Jake,
> >
> > You should be able to use:
> >
> >       thisJoinPointStaticPart.getSignature().getDeclaringType()
> >
> > This should give the java.lang.Class of the current class that the
> > join point is executing within.
> >
> > Hope this helps.
> >
> > Ron
> >
> > -----Original Message-----
> > From: Jake Whitehill [mailto:jrw@xxxxxxxx]
> > Sent: Wednesday, February 05, 2003 2:08 PM
> > To: aspectj-users@xxxxxxxxxxx
> > Subject: [aspectj-users] staticinitializer
> >
> >
> > Hi,
> >
> > I'm trying to implement the following:
> >
> > I have three classes A, B, and C. In all three classes the static
> > variable x appears. I want to use advice to write static-initializer
> > code for A, B, and C which initializes the value of x to some
> > class-dependent value. For instance, I'd really like to have:
> >
> > A.x = "A", B.x = "B", and C.x = "C"
> >
> > But I don't know how to refer to the actual class being initialized
> > from within the static initializer.
> >
> > Any suggestions?
> >
> > Thanks
> > Jake
> >
> >
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > http://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> > **********************************************************************
> > ****
> > The information transmitted herewith is sensitive information intended
> only
> > for use by the individual or entity to which it is addressed. If the
> reader
> > of this message is not the intended recipient, you are hereby notified
> that
> > any review, retransmission, dissemination, distribution, copying or other
> > use of, or taking of any action in reliance upon this information is
> > strictly prohibited. If you have received this communication in error,
> > please contact the sender and delete the material from your computer.
> > _______________________________________________
> > 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
> 
> **************************************************************************
> The information transmitted herewith is sensitive information intended only
> for use by the individual or entity to which it is addressed. If the reader
> of this message is not the intended recipient, you are hereby notified that
> any review, retransmission, dissemination, distribution, copying or other
> use of, or taking of any action in reliance upon this information is
> strictly prohibited. If you have received this communication in error,
> please contact the sender and delete the material from your computer.
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top