Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Binary weaving method annotation into a class in a jar issue - 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. The interesting part is that I was able to add a new
method into the junit.framework.TestCase class.

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 @Annotation 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 method-level annotations in a class file.

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


Back to the top