1 8 package test.annotation; 9 10 import org.codehaus.aspectwerkz.transform.AspectWerkzPreProcessor; 11 import org.codehaus.aspectwerkz.annotation.instrumentation.asm.AsmAnnotationHelper; 12 import org.objectweb.asm.ClassReader; 13 import org.objectweb.asm.attrs.Attributes; 14 import junit.framework.TestCase; 15 16 import java.util.List ; 17 import java.util.ArrayList ; 18 import java.io.ByteArrayOutputStream ; 19 import java.io.InputStream ; 20 21 32 public class AnnotationCopyTest extends TestCase { 33 34 public void testWeaveAndReadnnotation() throws Throwable { 35 ClassLoader classLoader = this.getClass().getClassLoader(); 36 37 InputStream is = classLoader.getResourceAsStream("test/annotation/AnnotationTest.class"); 39 ByteArrayOutputStream os = new ByteArrayOutputStream (); 40 for (int b = is.read(); b != -1; b = is.read()) { 41 os.write(b); 42 } 43 byte[] bytes = os.toByteArray(); 44 45 AspectWerkzPreProcessor awpp = new AspectWerkzPreProcessor(); 47 awpp.initialize(); 48 byte[] weaved = awpp.preProcess("test.annotation.AnnotationTest", bytes, classLoader); 49 50 List annotations = new ArrayList (); 52 ClassReader asmReader = new ClassReader(weaved); 53 asmReader.accept( 54 new AsmAnnotationHelper.MethodAnnotationExtractor(annotations, "publicMethod", "()V", classLoader), 55 Attributes.getDefaultAttributes(), 56 true 57 ); 58 assertEquals(2, annotations.size()); 59 } 60 61 public static void main(String [] args) { 62 junit.textui.TestRunner.run(suite()); 63 } 64 65 public static junit.framework.Test suite() { 66 return new junit.framework.TestSuite(AnnotationCopyTest.class); 67 } 68 69 70 } 71 | Popular Tags |