1 11 package org.eclipse.jdt.internal.core.util; 12 13 import org.eclipse.jdt.core.util.ClassFormatException; 14 import org.eclipse.jdt.core.util.IAttributeNamesConstants; 15 import org.eclipse.jdt.core.util.IClassFileAttribute; 16 import org.eclipse.jdt.core.util.IClassFileReader; 17 import org.eclipse.jdt.core.util.ICodeAttribute; 18 import org.eclipse.jdt.core.util.IConstantPool; 19 import org.eclipse.jdt.core.util.IConstantPoolConstant; 20 import org.eclipse.jdt.core.util.IConstantPoolEntry; 21 import org.eclipse.jdt.core.util.IExceptionAttribute; 22 import org.eclipse.jdt.core.util.IMethodInfo; 23 import org.eclipse.jdt.core.util.IModifierConstants; 24 25 28 public class MethodInfo extends ClassFileStruct implements IMethodInfo { 29 private int accessFlags; 30 private int attributeBytes; 31 private IClassFileAttribute[] attributes; 32 private int attributesCount; 33 private ICodeAttribute codeAttribute; 34 private char[] descriptor; 35 private int descriptorIndex; 36 private IExceptionAttribute exceptionAttribute; 37 private boolean isDeprecated; 38 private boolean isSynthetic; 39 private char[] name; 40 private int nameIndex; 41 42 48 public MethodInfo(byte classFileBytes[], IConstantPool constantPool, int offset, int decodingFlags) 49 throws ClassFormatException { 50 51 boolean no_code_attribute = (decodingFlags & IClassFileReader.METHOD_BODIES) == 0; 52 final int flags = u2At(classFileBytes, 0, offset); 53 this.accessFlags = flags; 54 if ((flags & IModifierConstants.ACC_SYNTHETIC) != 0) { 55 this.isSynthetic = true; 56 } 57 58 this.nameIndex = u2At(classFileBytes, 2, offset); 59 IConstantPoolEntry constantPoolEntry = constantPool.decodeEntry(this.nameIndex); 60 if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) { 61 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY); 62 } 63 this.name = constantPoolEntry.getUtf8Value(); 64 65 this.descriptorIndex = u2At(classFileBytes, 4, offset); 66 constantPoolEntry = constantPool.decodeEntry(this.descriptorIndex); 67 if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) { 68 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY); 69 } 70 this.descriptor = constantPoolEntry.getUtf8Value(); 71 72 this.attributesCount = u2At(classFileBytes, 6, offset); 73 this.attributes = ClassFileAttribute.NO_ATTRIBUTES; 74 if (this.attributesCount != 0) { 75 if (no_code_attribute && !isAbstract() && !isNative()) { 76 if (this.attributesCount != 1) { 77 this.attributes = new IClassFileAttribute[this.attributesCount - 1]; 78 } 79 } else { 80 this.attributes = new IClassFileAttribute[this.attributesCount]; 81 } 82 } 83 int attributesIndex = 0; 84 int readOffset = 8; 85 for (int i = 0; i < this.attributesCount; i++) { 86 constantPoolEntry = constantPool.decodeEntry(u2At(classFileBytes, readOffset, offset)); 87 if (constantPoolEntry.getKind() != IConstantPoolConstant.CONSTANT_Utf8) { 88 throw new ClassFormatException(ClassFormatException.INVALID_CONSTANT_POOL_ENTRY); 89 } 90 char[] attributeName = constantPoolEntry.getUtf8Value(); 91 if (equals(attributeName, IAttributeNamesConstants.DEPRECATED)) { 92 this.isDeprecated = true; 93 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset); 94 } else if (equals(attributeName, IAttributeNamesConstants.SYNTHETIC)) { 95 this.isSynthetic = true; 96 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset); 97 } else if (equals(attributeName, IAttributeNamesConstants.CODE)) { 98 if (!no_code_attribute) { 99 this.codeAttribute = new CodeAttribute(classFileBytes, constantPool, offset + readOffset); 100 this.attributes[attributesIndex++] = this.codeAttribute; 101 } 102 } else if (equals(attributeName, IAttributeNamesConstants.EXCEPTIONS)) { 103 this.exceptionAttribute = new ExceptionAttribute(classFileBytes, constantPool, offset + readOffset); 104 this.attributes[attributesIndex++] = this.exceptionAttribute; 105 } else if (equals(attributeName, IAttributeNamesConstants.SIGNATURE)) { 106 this.attributes[attributesIndex++] = new SignatureAttribute(classFileBytes, constantPool, offset + readOffset); 107 } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_ANNOTATIONS)) { 108 this.attributes[attributesIndex++] = new RuntimeVisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset); 109 } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_ANNOTATIONS)) { 110 this.attributes[attributesIndex++] = new RuntimeInvisibleAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset); 111 } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS)) { 112 this.attributes[attributesIndex++] = new RuntimeVisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset); 113 } else if (equals(attributeName, IAttributeNamesConstants.RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS)) { 114 this.attributes[attributesIndex++] = new RuntimeInvisibleParameterAnnotationsAttribute(classFileBytes, constantPool, offset + readOffset); 115 } else if (equals(attributeName, IAttributeNamesConstants.ANNOTATION_DEFAULT)) { 116 this.attributes[attributesIndex++] = new AnnotationDefaultAttribute(classFileBytes, constantPool, offset + readOffset); 117 } else { 118 this.attributes[attributesIndex++] = new ClassFileAttribute(classFileBytes, constantPool, offset + readOffset); 119 } 120 readOffset += (6 + u4At(classFileBytes, readOffset + 2, offset)); 121 } 122 this.attributeBytes = readOffset; 123 } 124 127 public int getAccessFlags() { 128 return this.accessFlags; 129 } 130 131 134 public int getAttributeCount() { 135 return this.attributesCount; 136 } 137 140 public IClassFileAttribute[] getAttributes() { 141 return this.attributes; 142 } 143 144 147 public ICodeAttribute getCodeAttribute() { 148 return this.codeAttribute; 149 } 150 151 154 public char[] getDescriptor() { 155 return this.descriptor; 156 } 157 158 161 public int getDescriptorIndex() { 162 return this.descriptorIndex; 163 } 164 165 168 public IExceptionAttribute getExceptionAttribute() { 169 return this.exceptionAttribute; 170 } 171 172 175 public char[] getName() { 176 return this.name; 177 } 178 179 182 public int getNameIndex() { 183 return this.nameIndex; 184 } 185 186 private boolean isAbstract() { 187 return (this.accessFlags & IModifierConstants.ACC_ABSTRACT) != 0; 188 } 189 190 193 public boolean isClinit() { 194 return this.name[0] == '<' && this.name.length == 8; } 196 197 200 public boolean isConstructor() { 201 return this.name[0] == '<' && this.name.length == 6; } 203 204 207 public boolean isDeprecated() { 208 return this.isDeprecated; 209 } 210 211 private boolean isNative() { 212 return (this.accessFlags & IModifierConstants.ACC_NATIVE) != 0; 213 } 214 215 218 public boolean isSynthetic() { 219 return this.isSynthetic; 220 } 221 222 int sizeInBytes() { 223 return this.attributeBytes; 224 } 225 } 226 | Popular Tags |