1 11 package org.eclipse.jdt.internal.compiler.classfmt; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 import org.eclipse.jdt.internal.compiler.codegen.AttributeNamesConstants; 15 16 public class AnnotationMethodInfo extends MethodInfo { 17 protected Object defaultValue = null; 18 19 public static MethodInfo createAnnotationMethod(byte classFileBytes[], int offsets[], int offset) { 20 MethodInfo methodInfo = new MethodInfo(classFileBytes, offsets, offset); 21 int attributesCount = methodInfo.u2At(6); 22 int readOffset = 8; 23 AnnotationInfo[] annotations = null; 24 Object defaultValue = null; 25 for (int i = 0; i < attributesCount; i++) { 26 int utf8Offset = methodInfo.constantPoolOffsets[methodInfo.u2At(readOffset)] - methodInfo.structOffset; 28 char[] attributeName = methodInfo.utf8At(utf8Offset + 3, methodInfo.u2At(utf8Offset + 1)); 29 if (attributeName.length > 0) { 30 switch(attributeName[0]) { 31 case 'A': 32 if (CharOperation.equals(attributeName, AttributeNamesConstants.AnnotationDefaultName)) { 33 AnnotationInfo info = 36 new AnnotationInfo(methodInfo.reference, methodInfo.constantPoolOffsets, readOffset + 6 + methodInfo.structOffset); 37 defaultValue = info.decodeDefaultValue(); 38 } 39 break; 40 case 'S' : 41 if (CharOperation.equals(AttributeNamesConstants.SignatureName, attributeName)) 42 methodInfo.signatureUtf8Offset = methodInfo.constantPoolOffsets[methodInfo.u2At(readOffset + 6)] - methodInfo.structOffset; 43 break; 44 case 'R' : 45 AnnotationInfo[] methodAnnotations = null; 46 if (CharOperation.equals(attributeName, AttributeNamesConstants.RuntimeVisibleAnnotationsName)) { 47 methodAnnotations = decodeMethodAnnotations(readOffset, true, methodInfo); 48 } else if (CharOperation.equals(attributeName, AttributeNamesConstants.RuntimeInvisibleAnnotationsName)) { 49 methodAnnotations = decodeMethodAnnotations(readOffset, false, methodInfo); 50 } 51 if (methodAnnotations != null) { 52 if (annotations == null) { 53 annotations = methodAnnotations; 54 } else { 55 int length = annotations.length; 56 AnnotationInfo[] newAnnotations = new AnnotationInfo[length + methodAnnotations.length]; 57 System.arraycopy(annotations, 0, newAnnotations, 0, length); 58 System.arraycopy(methodAnnotations, 0, newAnnotations, length, methodAnnotations.length); 59 annotations = newAnnotations; 60 } 61 } 62 break; 63 } 64 } 65 readOffset += (6 + methodInfo.u4At(readOffset + 2)); 66 } 67 methodInfo.attributeBytes = readOffset; 68 69 if (defaultValue != null) { 70 if (annotations != null) { 71 return new AnnotationMethodInfoWithAnnotations(methodInfo, defaultValue, annotations); 72 } 73 return new AnnotationMethodInfo(methodInfo, defaultValue); 74 } 75 if (annotations != null) 76 return new MethodInfoWithAnnotations(methodInfo, annotations); 77 return methodInfo; 78 } 79 80 AnnotationMethodInfo(MethodInfo methodInfo, Object defaultValue) { 81 super(methodInfo.reference, methodInfo.constantPoolOffsets, methodInfo.structOffset); 82 this.defaultValue = defaultValue; 83 84 this.accessFlags = methodInfo.accessFlags; 85 this.attributeBytes = methodInfo.attributeBytes; 86 this.descriptor = methodInfo.descriptor; 87 this.exceptionNames = methodInfo.exceptionNames; 88 this.name = methodInfo.name; 89 this.signature = methodInfo.signature; 90 this.signatureUtf8Offset = methodInfo.signatureUtf8Offset; 91 this.tagBits = methodInfo.tagBits; 92 } 93 public Object getDefaultValue() { 94 return this.defaultValue; 95 } 96 protected void toStringContent(StringBuffer buffer) { 97 super.toStringContent(buffer); 98 if (this.defaultValue != null) { 99 buffer.append(" default "); if (this.defaultValue instanceof Object []) { 101 buffer.append('{'); 102 Object [] elements = (Object []) this.defaultValue; 103 for (int i = 0, len = elements.length; i < len; i++) { 104 if (i > 0) 105 buffer.append(", "); buffer.append(elements[i]); 107 } 108 buffer.append('}'); 109 } else { 110 buffer.append(this.defaultValue); 111 } 112 buffer.append('\n'); 113 } 114 } 115 } 116 | Popular Tags |