Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] Binary-weaving method annotation into a class in a jar not working - is this a bug?

Thank you Andy that clears things up.
It would be nice if AspectJ throws a warning.

Monal

On 9/26/06, Andy Clement <andrew.clement@xxxxxxxxx> wrote:
As I've put in the bugzilla report.  The 'bug' is that AspectJ allowed
you to specify 'declare @method' targetting a type that is not a Java5
type.  I suspect junit.jar was built at < Java5 level, and so it's
classes do not support annotations.  Because annotations are captured
as regular attributes, the declared annotation does get attached to
the method in TestCase (as we can see through looking at the bytecode)
and it is still a well formed class.  However, reflection is
recognizing it isn't a Java5 type so cannot have annotations.  AspectJ
does not currently auto-promote the level of a class if you attach
Java5 features to it.

Andy.

On 25/09/06, Monal Daxini <monaldax@xxxxxxxxx> wrote:
> Hi,
>
> I added the before() advice to MyTestAspect (lines 7 - 9) with
> junit.jar on the classpath. I added a call to the InheritAnnot method
> to call junit.framework.TestCase setUp method (super.setUp). Now line
> 8 executes and the message displayed. Previously I was using
> reflection (see InheritAnnot class below) to verify that the
> annotation was applied to the TestCase.setUp method, and this test
> failed in detecting the presence of the annotation.
>
> So, now the bug is clear - Introduced annnotation is available when
> the method executes but the same annotation information is not
> available  via reflection using the Method.getAnnotations() call. I
> will go ahead and log a bug.
>
> Note: MyAnnotaiton has RetentionPolicy.RUNTIME
>
> import junit.framework.TestCase;
> 1   public aspect MyTestAspect {
> 2           @MyAnnotation
> 3           public static void TestCase.testCaseSetUp() {
> 4           }
> 5           // declare @method : * TestCase.testCaseSetUp() : @MyAnnotation;
> 6           declare @method : * TestCase.setUp() : @MyAnnotation;
> 7           before() : execution(@MyAnnotation * TestCase.setUp()) {
> 8                   System.err.println(" ** setup here: " + thisJoinPoint);
> 9           }
> 10   }
>
> Wes: Of the top of my head the one difference that I can see is that
> super.jar is created using the aspectj compiler and junit.jar is
> created using jdk's jar command.
>
> Monal
>
> On 9/25/06, Wes <wes@xxxxxxxxxxxxxx> wrote:
> > It sounds like a bug worth reporting.
> >
> >    http://dev.eclipse.org/bugs/enter_bug.cgi?product=AspectJ
> >
> > I reproduced it on the command-line with junit.jar, but not using
> > the simple test case below.  Perhaps someone could isolate the
> > difference...
> >
> > Wes
> >
> >  src $ cat t/Super.java
> >
> > package t;
> >
> > public class Super {
> >   public void setUp() {}
> > }
> >  src $ cat t/TC.java
> > package t;
> >
> > public class TC extends Super {
> >         public static void main(String[] a) { new TC().setUp(); }
> > }
> > aspect MyTestAspect {
> >   public @interface MyAnnotation {}
> >   declare @method : void Super.setUp() : @MyAnnotation;
> >   before() : execution(@MyAnnotation void *()) {
> >     System.err.println("here: " + thisJoinPoint);
> >   }
> > }
> >
> >
> >  src $ aspectj-1.5 -1.5 t/Super.java -outjar super.jar
> >  src $ aspectj-1.5 -1.5 t/TC.java -injars super.jar -outjar app.jar
> >  src $ j5 -classpath "app.jar;$CLASSPATH" t.TC
> > here: execution(void t.Super.setUp())
> >
> >
> > > ------------Original Message------------
> > > From: "Monal Daxini" <monaldax@xxxxxxxxx>
> > > To: aspectj-users@xxxxxxxxxxx
> > > Date: Sun, Sep-24-2006 9:37 PM
> > > Subject: [aspectj-users] Binary-weaving method annotation into a class in a jar not working - is this a bug?
> > >
> > > Hi,
> > >
> > > Environment:
> > > AspectJ 1.5.2, AspectJ Eclipse Plugin 1.4, Eclipse 3.2, Sun JDK 1.5
> > >
> > > I am trying to add my custom annotation @MyAnnotation on the
> > > junit.framework.TestCase.setUp() method. I have tried both Load Time
> > > Weaving and compile time weaving but I was unsuccessful in weaving the
> > > annotation. However, I was able to add a new method into the
> > > junit.framework.TestCase. Details as below...
> > >
> > > import junit.framework.TestCase;
> > > 1   public aspect MyTestAspect {
> > > 2           @MyAnnotation
> > > 3           public static void TestCase.testCaseSetUp() {
> > > 4           }
> > > 5           // declare @method : * TestCase.testCaseSetUp() :
> > > @MyAnnotation;
> > > 6           declare @method : * TestCase.setUp() : @MyAnnotation;
> > > 7       }
> > > 8   }
> > >
> > > Line number 3 adds a method into the TestCase class (see the test code
> > > below that uses reflection to verify this). However it does not add
> > > the annotation on line 1 to the testCaseSetUp method. Now commenting
> > > line 2 and un-commenting line 5 does not add the annotation either.
> > > This true even when testCaseSetUp method is non-static. Along the same
> > > lines line 6 does not add the annotation to the TestCase.setUp method.
> > > I added junit.jar to AspectJ inpath, I also attached the junit source,
> > > but no luck. I also tried using an expanded version of the junit.jar
> > > classes.
> > >
> > > INTERESTING: I then added the junit source directly as a source folder
> > > to my project, and then it works - testCaseSetUp() method is added to
> > > TestCase along with the @MyAnnotation. Commenting line 2 and
> > > uncommenting line 3 also results in the @MyAnnotation being
> > > introduced.
> > >
> > > So is there a bug currently that prevents the @MyAnnotation from being
> > > introduced while binary-weaving stand alone classes or classes in a
> > > jar? If not, could you please tell me what I need to do to make it
> > > work so that I can weave my custom method-level annotations in a class
> > > file.
> > >
> > > // Aspect verification class
> > > public class InheritAnnot extends TestCase {
> > >
> > >        public static void main(String[] args) throws Throwable {
> > >                InheritAnnot ia = new InheritAnnot();
> > >                Class curClazz = ia.getClass();
> > >                do {
> > >                        System.out.println("Current Class: " +
> > > curClazz.getName());
> > >                        Method[] ms = curClazz.getDeclaredMethods();
> > >                        for(Method m : ms) {
> > >
> > > if(m.getName().equalsIgnoreCase("testCaseSetUp")) {
> > >                                        System.out.println("    ----
> > > Method Name: " + m.getName());
> > >                                        m.invoke(curClazz, new
> > > Object[0]);
> > >                                }
> > >                                Annotation[] mannots =
> > > m.getAnnotations();
> > >                                for(Annotation ma : mannots) {
> > >                                        System.out.println("@ Found
> > > annotation - " + ma
> > >
> > >         + " in Class: " + curClazz.getName()
> > >
> > >         + " on method: " + m.getName());
> > >                                }
> > >                        }
> > >                } while((curClazz = curClazz.getSuperclass()) != null);
> > >        }
> > > }
> > >
> > > Thank you
> > > Monal
> > > _______________________________________________
> > > aspectj-users mailing list
> > > aspectj-users@xxxxxxxxxxx
> > > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> > >
> >
> > _______________________________________________
> > aspectj-users mailing list
> > aspectj-users@xxxxxxxxxxx
> > https://dev.eclipse.org/mailman/listinfo/aspectj-users
> >
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users



Back to the top