1 19 20 package jode.bytecode; 21 import java.io.DataInputStream ; 22 import java.io.DataOutputStream ; 23 import java.io.IOException ; 24 import java.lang.reflect.Modifier ; 25 26 public class MethodInfo extends BinaryInfo { 27 28 ClassInfo clazzInfo; 29 30 int modifier; 31 String name; 32 String typeSig; 33 34 BytecodeInfo bytecode; 35 String [] exceptions; 36 boolean syntheticFlag; 37 boolean deprecatedFlag; 38 39 public MethodInfo(ClassInfo ci) { 40 clazzInfo = ci; 41 } 42 43 public MethodInfo(ClassInfo ci, 44 String name, String typeSig, int modifier) { 45 this.clazzInfo = ci; 46 this.name = name; 47 this.typeSig = typeSig; 48 this.modifier = modifier; 49 } 50 51 protected void readAttribute(String name, int length, ConstantPool cp, 52 DataInputStream input, 53 int howMuch) throws IOException { 54 if ((howMuch & KNOWNATTRIBS) != 0 && name.equals("Code")) { 55 bytecode = new BytecodeInfo(this); 56 bytecode.read(cp, input); 57 } else if (name.equals("Exceptions")) { 58 int count = input.readUnsignedShort(); 59 exceptions = new String [count]; 60 for (int i=0; i< count; i++) 61 exceptions[i] = cp.getClassName(input.readUnsignedShort()); 62 if (length != 2 * (count + 1)) 63 throw new ClassFormatException 64 ("Exceptions attribute has wrong length"); 65 } else if (name.equals("Synthetic")) { 66 syntheticFlag = true; 67 if (length != 0) 68 throw new ClassFormatException 69 ("Synthetic attribute has wrong length"); 70 } else if (name.equals("Deprecated")) { 71 deprecatedFlag = true; 72 if (length != 0) 73 throw new ClassFormatException 74 ("Deprecated attribute has wrong length"); 75 } else 76 super.readAttribute(name, length, cp, input, howMuch); 77 } 78 79 public void read(ConstantPool constantPool, 80 DataInputStream input, int howMuch) throws IOException { 81 modifier = input.readUnsignedShort(); 82 name = constantPool.getUTF8(input.readUnsignedShort()); 83 typeSig = constantPool.getUTF8(input.readUnsignedShort()); 84 readAttributes(constantPool, input, howMuch); 85 } 86 87 public void reserveSmallConstants(GrowableConstantPool gcp) { 88 if (bytecode != null) 89 bytecode.reserveSmallConstants(gcp); 90 } 91 92 public void prepareWriting(GrowableConstantPool gcp) { 93 gcp.putUTF8(name); 94 gcp.putUTF8(typeSig); 95 if (bytecode != null) { 96 gcp.putUTF8("Code"); 97 bytecode.prepareWriting(gcp); 98 } 99 if (exceptions != null) { 100 gcp.putUTF8("Exceptions"); 101 for (int i=0; i< exceptions.length; i++) 102 gcp.putClassName(exceptions[i]); 103 } 104 if (syntheticFlag) 105 gcp.putUTF8("Synthetic"); 106 if (deprecatedFlag) 107 gcp.putUTF8("Deprecated"); 108 prepareAttributes(gcp); 109 } 110 111 protected int getKnownAttributeCount() { 112 int count = 0; 113 if (bytecode != null) 114 count++; 115 if (exceptions != null) 116 count++; 117 if (syntheticFlag) 118 count++; 119 if (deprecatedFlag) 120 count++; 121 return count; 122 } 123 124 public void writeKnownAttributes(GrowableConstantPool gcp, 125 DataOutputStream output) 126 throws IOException { 127 if (bytecode != null) { 128 output.writeShort(gcp.putUTF8("Code")); 129 output.writeInt(bytecode.getSize()); 130 bytecode.write(gcp, output); 131 } 132 if (exceptions != null) { 133 int count = exceptions.length; 134 output.writeShort(gcp.putUTF8("Exceptions")); 135 output.writeInt(2 + count * 2); 136 output.writeShort(count); 137 for (int i=0; i< count; i++) 138 output.writeShort(gcp.putClassName(exceptions[i])); 139 } 140 if (syntheticFlag) { 141 output.writeShort(gcp.putUTF8("Synthetic")); 142 output.writeInt(0); 143 } 144 if (deprecatedFlag) { 145 output.writeShort(gcp.putUTF8("Deprecated")); 146 output.writeInt(0); 147 } 148 } 149 150 public void write(GrowableConstantPool constantPool, 151 DataOutputStream output) throws IOException { 152 output.writeShort(modifier); 153 output.writeShort(constantPool.putUTF8(name)); 154 output.writeShort(constantPool.putUTF8(typeSig)); 155 writeAttributes(constantPool, output); 156 } 157 158 public void dropInfo(int howMuch) { 159 if ((howMuch & KNOWNATTRIBS) != 0) { 160 bytecode = null; 161 exceptions = null; 162 } 163 if (bytecode != null) 164 bytecode.dropInfo(howMuch); 165 super.dropInfo(howMuch); 166 } 167 168 public ClassInfo getClazzInfo() { 169 return clazzInfo; 170 } 171 172 public String getName() { 173 return name; 174 } 175 176 public String getType() { 177 return typeSig; 178 } 179 180 public int getModifiers() { 181 return modifier; 182 } 183 184 public boolean isStatic() { 185 return Modifier.isStatic(modifier); 186 } 187 188 public boolean isSynthetic() { 189 return syntheticFlag; 190 } 191 192 public boolean isDeprecated() { 193 return deprecatedFlag; 194 } 195 196 public BytecodeInfo getBytecode() { 197 return bytecode; 198 } 199 200 public String [] getExceptions() { 201 return exceptions; 202 } 203 204 public void setName(String newName) { 205 name = newName; 206 } 207 208 public void setType(String newType) { 209 typeSig = newType; 210 } 211 212 public void setModifiers(int newModifier) { 213 modifier = newModifier; 214 } 215 216 public void setSynthetic(boolean flag) { 217 syntheticFlag = flag; 218 } 219 220 public void setDeprecated(boolean flag) { 221 deprecatedFlag = flag; 222 } 223 224 public void setBytecode(BytecodeInfo newBytecode) { 225 clazzInfo.loadInfo(KNOWNATTRIBS); 226 bytecode = newBytecode; 227 } 228 229 public void setExceptions(String [] newExceptions) { 230 clazzInfo.loadInfo(KNOWNATTRIBS); 231 exceptions = newExceptions; 232 } 233 234 public String toString() { 235 return "Method "+Modifier.toString(modifier)+" "+ 236 typeSig + " " + clazzInfo.getName() + "."+ name; 237 } 238 } 239 | Popular Tags |