Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] @DeclareParents - @ManagedParents example

Hi Brian

The article you reference is from waaaay back - that was written during development of AspectJ5 and some changes have since gone in that affect how annotation style syntax works for declare parents.  The AspectJ doc is more up to date (although I just spotted another typo):

http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-itds.html

And from the example there, I have just written:
---8<---
import org.aspectj.lang.annotation.*;
import java.lang.annotation.*;

enum Mood { HAPPY, SAD, JOLLY, GRUMPY }

@Aspect
public class MoodIndicator {

        // this interface can be outside of the aspect
        public interface Moody {
          Mood getMood();
        };

        // this implementation can be outside of the aspect
        public static class MoodyImpl implements Moody {
           private Mood mood = Mood.HAPPY;

           public Mood getMood() {
             return mood;
           }
        }

        // the field type must be the introduced interface. It can't be a class.
        @DeclareParents(value="@MyAnnotation *",defaultImpl= MoodyImpl.class)
        private Moody implementedInterface;

        @Before("execution(* *.*(..)) && this(m)")
        public void feelingMoody(Moody m) {
           System.out.println("I'm feeling " + m.getMood());
        }
}



@MyAnnotation
class Annotated {
}

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface MyAnnotation{
}
---8<---
C:\aspectj1.5.3-dev>ajc -1.5 MoodIndicator.java -showWeaveInfo
Join point 'method-execution(Mood MoodIndicator$MoodyImpl.getMood())' in Type 'MoodIndicator$MoodyImpl' (MoodIndicator.java:19) advised by before advice from 'MoodIndicator' ( MoodIndicator.java:29)

Extending interface set for type 'Annotated' (MoodIndicator.java) to include 'MoodIndicator$Moody' (MoodIndicator.java)

Type 'Annotated' (MoodIndicator.java) has intertyped method from 'MoodIndicator' ( MoodIndicator.java:'Mood MoodIndicator$Moody.getMood()')


I haven't tried it with LTW but can't see why that would be a problem.

Andy.

On 17/08/06, brian.a.yoffe@xxxxxxxxxxxx <brian.a.yoffe@xxxxxxxxxxxx> wrote:

I got no response on this, so I thought I'd ask the question perhaps in a different way.   Since the things that appear to be not working (or that I'm mis-using) are @AspectJ notation in conjunction with load-time weaving, perhaps I should avoid that combination.  So...

1)   Is @AspectJ notation "ripe" for use in production systems?
2)   Is LTW ready for prime-time, when not used with @AspectJ?
3)   Should I be wary of these two new features and stick to compile time weaving and the standard .aj files?

Thanks,
Brian Yoffe



brian.a.yoffe@xxxxxxxxxxxx
Sent by: aspectj-users-bounces@xxxxxxxxxxx

08/15/2006 04:01 PM

Please respond to
aspectj-users@xxxxxxxxxxx

To
aspectj-users@xxxxxxxxxxx
cc

Subject
[aspectj-users] @DeclareParents - @ManagedParents example








Goal:

Introduce an interface on any class that is marked with the MyAnnotation annotation.   I am using LTW


For an example of what I'm trying to do, see this following article by Coyler:   http://www-128.ibm.com/developerworks/java/library/j-aopwork8/ (see @DeclareParents("@ManagedComponent *") )


I can easily declare parents when matching on a class or package.  For example:
@DeclareParents(value="a.b.c.d.Test",defaultImpl=MyImpl.class)


works.   I can capture information by the LTW (I have the -showWeaveInfo option set).


However, if I try to base this on an annotation, it fails.   For example:

@DeclareParents(value="@MyAnnotation *",defaultImpl=MyImpl.class)


or, using fully qualified annotation name:

@DeclareParents(value="@a.b.c.d.MyAnnotation *",defaultImpl=MyImpl.class)


The @MyAnnotation annotation is defined as:

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

public @interface MyAnnotation{

}


Further, the example provided by Mr. Coyler in the article cited above suggests the following syntax:

@DeclareParents("@ManagedComponent *")

   class DefaultLifecycleImpl implements Lifecycle {

     private State state = State.INITIAL;

     public void initialize() {}

     public void start() {}

     public void stop() {}

     public void terminate() {}

     public boolean isBroken() { return state == State.BROKEN; }

     public State getState() { return state; }

   }


This does not compile for me, as @DeclareParents is set for ElementType.FIELD targets only.


I'm not sure what I might be overlooking.   The inter-type introduction I'm attempting works if I use the normal aspectj syntax (i.e. .aj files) and use LTW to weave in the advice.


Thanks in advance for the help,

Brian Yoffe





Compiler/vm:   Sun 1.5.0_06

AspectJ: 1.5.0


This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates.

This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.


This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates.

This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you._______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



This communication is for informational purposes only. It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any comments or statements made herein do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries and affiliates.

This transmission may contain information that is privileged, confidential, legally privileged, and/or exempt from disclosure under applicable law. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of the information contained herein (including any reliance thereon) is STRICTLY PROHIBITED. Although this transmission and any attachments are believed to be free of any virus or other defect that might affect any computer system into which it is received and opened, it is the responsibility of the recipient to ensure that it is virus free and no responsibility is accepted by JPMorgan Chase & Co., its subsidiaries and affiliates, as applicable, for any loss or damage arising in any way from its use. If you received this transmission in error, please immediately contact the sender and destroy the material in its entirety, whether in electronic or hard copy format. Thank you.

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




Back to the top