1 52 53 package com.go.trove.classfile; 54 55 import java.util.*; 56 import java.io.*; 57 import java.lang.reflect.Modifier ; 58 59 71 public class MethodInfo { 72 private ClassFile mParent; 73 private ConstantPool mCp; 74 75 private String mName; 76 private MethodDescriptor mDesc; 77 78 private int mAccessFlags; 79 80 private ConstantUTFInfo mNameConstant; 81 private ConstantUTFInfo mDescriptorConstant; 82 83 private List mAttributes = new ArrayList(2); 84 85 private CodeAttr mCode; 86 private ExceptionsAttr mExceptions; 87 88 MethodInfo(ClassFile parent, 89 AccessFlags flags, 90 String name, 91 MethodDescriptor desc) { 92 93 mParent = parent; 94 mCp = parent.getConstantPool(); 95 mName = name; 96 mDesc = desc; 97 98 mAccessFlags = flags.getModifier(); 99 mNameConstant = ConstantUTFInfo.make(mCp, name); 100 mDescriptorConstant = ConstantUTFInfo.make(mCp, desc.toString()); 101 102 if (!flags.isAbstract()) { 103 addAttribute(new CodeAttr(mCp)); 104 } 105 } 106 107 private MethodInfo(ClassFile parent, 108 int flags, 109 ConstantUTFInfo nameConstant, 110 ConstantUTFInfo descConstant) { 111 112 mParent = parent; 113 mCp = parent.getConstantPool(); 114 mName = nameConstant.getValue(); 115 mDesc = MethodDescriptor.parseMethodDesc(descConstant.getValue()); 116 117 mAccessFlags = flags; 118 mNameConstant = nameConstant; 119 mDescriptorConstant = descConstant; 120 } 121 122 125 public ClassFile getClassFile() { 126 return mParent; 127 } 128 129 132 public String getName() { 133 return mName; 134 } 135 136 140 public MethodDescriptor getMethodDescriptor() { 141 return mDesc; 142 } 143 144 147 public AccessFlags getAccessFlags() { 148 return new AccessFlags(mAccessFlags); 149 } 150 151 154 public ConstantUTFInfo getNameConstant() { 155 return mNameConstant; 156 } 157 158 163 public ConstantUTFInfo getDescriptorConstant() { 164 return mDescriptorConstant; 165 } 166 167 170 public String [] getExceptions() { 171 if (mExceptions == null) { 172 return new String [0]; 173 } 174 else { 175 return mExceptions.getExceptions(); 176 } 177 } 178 179 183 public CodeAttr getCodeAttr() { 184 return mCode; 185 } 186 187 public boolean isSynthetic() { 188 for (int i = mAttributes.size(); --i >= 0; ) { 189 Object obj = mAttributes.get(i); 190 if (obj instanceof SyntheticAttr) { 191 return true; 192 } 193 } 194 return false; 195 } 196 197 public boolean isDeprecated() { 198 for (int i = mAttributes.size(); --i >= 0; ) { 199 Object obj = mAttributes.get(i); 200 if (obj instanceof DeprecatedAttr) { 201 return true; 202 } 203 } 204 return false; 205 } 206 207 210 public void addException(String className) { 211 if (mExceptions == null) { 212 addAttribute(new ExceptionsAttr(mCp)); 213 } 214 215 ConstantClassInfo cci = ConstantClassInfo.make(mCp, className); 216 mExceptions.addException(cci); 217 } 218 219 222 public void markSynthetic() { 223 addAttribute(new SyntheticAttr(mCp)); 224 } 225 226 229 public void markDeprecated() { 230 addAttribute(new DeprecatedAttr(mCp)); 231 } 232 233 public void addAttribute(Attribute attr) { 234 if (attr instanceof CodeAttr) { 235 if (mCode != null) { 236 mAttributes.remove(mCode); 237 } 238 mCode = (CodeAttr)attr; 239 } 240 else if (attr instanceof ExceptionsAttr) { 241 if (mExceptions != null) { 242 mAttributes.remove(mExceptions); 243 } 244 mExceptions = (ExceptionsAttr)attr; 245 } 246 247 mAttributes.add(attr); 248 } 249 250 public Attribute[] getAttributes() { 251 Attribute[] attrs = new Attribute[mAttributes.size()]; 252 return (Attribute[])mAttributes.toArray(attrs); 253 } 254 255 258 public int getLength() { 259 int length = 8; 260 261 int size = mAttributes.size(); 262 for (int i=0; i<size; i++) { 263 length += ((Attribute)mAttributes.get(i)).getLength(); 264 } 265 266 return length; 267 } 268 269 public void writeTo(DataOutput dout) throws IOException { 270 dout.writeShort(mAccessFlags); 271 dout.writeShort(mNameConstant.getIndex()); 272 dout.writeShort(mDescriptorConstant.getIndex()); 273 274 int size = mAttributes.size(); 275 dout.writeShort(size); 276 for (int i=0; i<size; i++) { 277 Attribute attr = (Attribute)mAttributes.get(i); 278 attr.writeTo(dout); 279 } 280 } 281 282 public String toString() { 283 String modStr = Modifier.toString(mAccessFlags); 284 if (modStr.length() == 0) { 285 return String.valueOf(mDesc) + ' ' + getName(); 286 } 287 else { 288 return modStr + ' ' + mDesc + ' ' + getName(); 289 } 290 } 291 292 static MethodInfo readFrom(ClassFile parent, 293 DataInput din, 294 AttributeFactory attrFactory) 295 throws IOException 296 { 297 ConstantPool cp = parent.getConstantPool(); 298 299 int flags = din.readUnsignedShort(); 300 int index = din.readUnsignedShort(); 301 ConstantUTFInfo nameConstant = (ConstantUTFInfo)cp.getConstant(index); 302 index = din.readUnsignedShort(); 303 ConstantUTFInfo descConstant = (ConstantUTFInfo)cp.getConstant(index); 304 305 MethodInfo info = new MethodInfo(parent, flags, 306 nameConstant, descConstant); 307 308 int size = din.readUnsignedShort(); 310 for (int i=0; i<size; i++) { 311 info.addAttribute(Attribute.readFrom(cp, din, attrFactory)); 312 } 313 314 return info; 315 } 316 } 317 | Popular Tags |