1 8 package org.codehaus.aspectwerkz.annotation.instrumentation.asm; 9 10 import org.objectweb.asm.attrs.Annotation; 11 import org.objectweb.asm.attrs.RuntimeInvisibleAnnotations; 12 import org.objectweb.asm.Attribute; 13 import org.codehaus.aspectwerkz.util.Base64; 14 import org.codehaus.aspectwerkz.util.UnbrokenObjectInputStream; 15 import org.codehaus.aspectwerkz.annotation.AnnotationInfo; 16 import org.codehaus.aspectwerkz.exception.WrappedRuntimeException; 17 18 import java.io.ByteArrayInputStream ; 19 20 28 public class CustomAttributeHelper { 29 30 33 private final static String VALUE = "value"; 34 35 41 public static AnnotationInfo extractCustomAnnotation(final Annotation annotation) { 42 byte[] bytes = Base64.decode((String ) ((Object []) annotation.elementValues.get(0))[1]); 43 return extractCustomAnnotation(bytes); 44 } 45 46 52 private static AnnotationInfo extractCustomAnnotation(final byte[] bytes) { 53 try { 54 Object userAnnotation = new UnbrokenObjectInputStream(new ByteArrayInputStream (bytes)).readObject(); 55 if (userAnnotation instanceof AnnotationInfo) { 56 return (AnnotationInfo)userAnnotation; 57 } else { 58 throw new RuntimeException ( 60 "Custom annotation is not wrapped in AnnotationInfo: " + userAnnotation.getClass().getName() + 61 " [" + AnnotationInfo.class.getClassLoader().toString() + " / " + 62 userAnnotation.getClass().getClassLoader().toString() + " / " + 63 Thread.currentThread().getContextClassLoader() 64 ); 65 } 66 } catch (Exception e) { 67 throw new WrappedRuntimeException(e); 68 } 69 } 70 71 77 public static Annotation createCustomAnnotation(final byte[] bytes) { 78 Annotation annotation = new Annotation(); 79 annotation.type = CustomAttribute.TYPE; 80 annotation.add(VALUE, Base64.encodeBytes(bytes)); 81 return annotation; 82 } 83 84 92 public static RuntimeInvisibleAnnotations linkRuntimeInvisibleAnnotations(final Attribute attribute) { 93 RuntimeInvisibleAnnotations runtimeInvisibleAnnotations = null; 94 Attribute lastAttribute = attribute; 95 for (Attribute loop = attribute; loop != null; loop = loop.next) { 96 lastAttribute = loop; 97 if (loop instanceof RuntimeInvisibleAnnotations) { 98 return runtimeInvisibleAnnotations = (RuntimeInvisibleAnnotations) loop; 99 } 100 } 101 runtimeInvisibleAnnotations = new RuntimeInvisibleAnnotations(); 103 runtimeInvisibleAnnotations.next = null; 104 if (attribute != null) { 105 lastAttribute.next = runtimeInvisibleAnnotations; 107 } else { 108 } 110 return runtimeInvisibleAnnotations; 111 } 112 } 113 | Popular Tags |