| 1 30 31 package jbet; 32 import java.util.*; 33 34 51 52 public final class Descriptor { 53 54 public Type [] args = null; public Type ret = null; 57 String stringCache = null; 60 public static final Descriptor Void = new Descriptor ("()V"); 61 public static final Descriptor returnInt = new Descriptor ("()I"); 62 63 65 public Descriptor (Type p1, Type r) { 66 ret = r; 67 args = new Type [1]; 68 args[0] = p1; 69 } 70 71 public Descriptor (Type r) { 72 ret = r; 73 args = new Type [0]; 74 } 75 76 public Descriptor () { } 77 78 79 86 public Descriptor (Descriptor d) { 87 ret = new Type (d.ret); 88 args = new Type [ d.args.length ]; 89 for (int i = 0; i < args.length; i++) 90 args[i] = new Type( d.args[i] ); 91 } 92 93 99 public Descriptor (String str) throws ParseException { 100 stringCache = str; 101 Vector argvec = new Vector(); 102 103 if (str.length() < 3) 104 throw new ParseException ("method descriptor too short"); 105 if (str.charAt(0) != '(') 106 throw new ParseException ("method descriptor doesn't start with \"(\""); 107 108 int i; 109 for (i = 1; str.charAt(i) != ')';) { 110 if (i >= str.length()) 111 throw new ParseException ("unexpected end of string"); 112 Type t = new Type(); 113 i += t.parse(str, i); 114 argvec.addElement(t); 115 } 116 i++; 117 ret = new Type(); 118 ret.parse(str, i); 119 120 args = new Type [ argvec.size() ]; 121 argvec.copyInto (args); 122 123 } 124 125 131 public void relocate(Hashtable subs) { 132 ret.relocate(subs); 133 for (int i = 0; i < args.length; i++) 134 args[i].relocate(subs); 135 } 136 137 145 public Descriptor relocate_new(Hashtable subs) { 146 Descriptor d = new Descriptor(this); 147 d.relocate(subs); 148 return d; 149 } 150 151 152 int numArgs() { return args.length; } 153 154 159 public int count() { 160 int ret = 1; 161 for (int i = 0; i < args.length; i++) 162 ret += args[i].count(); 163 return ret; 164 } 165 166 167 173 public String toString() { 174 if (stringCache == null) { 175 StringBuffer out = new StringBuffer (); 176 out.append ('('); 177 for (int i = 0; i < args.length; i++) 178 out.append ( args[i].toString() ); 179 out.append (')'); 180 out.append (ret.toString()); 181 stringCache = out.toString(); 182 } 183 return stringCache; 184 } 185 186 193 public boolean equals(Object o) { 194 if (o instanceof String ) 195 return toString().equals(o); 196 if (! (o instanceof Descriptor)) 197 return false; 198 Descriptor d = (Descriptor) o ; 199 if (args.length != d.args.length) 200 return false; 201 for (int i = 0; i < args.length; i++) 202 if (! args[i].equals( d.args[i] ) ) 203 return false; 204 if (! ret.equals( d.ret ) ) 205 return false; 206 return true; 207 } 208 209 215 public int hashCode() { 216 int hash = ret.hashCode(); 217 for (int i = 0; i < args.length; i++) 218 hash = hash * 101 + args[i].hashCode(); 219 return hash; 220 } 221 222 public String declaration () { 224 StringBuffer out = new StringBuffer ("("); 225 226 for (int i = 0; i < args.length; i++) { 227 out.append (args[i].declaration()); 228 out.append (" arg"); 229 out.append (i); 230 if (i != args.length - 1) 231 out.append (", "); 232 } 233 234 out.append (")"); 235 return out.toString(); 236 } 237 } 238 239 240 241 | Popular Tags |