1 package org.aspectj.apache.bcel.classfile; 2 3 56 57 import org.apache.bcel.Constants; 58 import java.io.*; 59 60 68 public final class Signature extends Attribute { 69 private int signature_index; 70 71 77 public Signature(final Signature c) { 78 this(c.getNameIndex(), 79 c.getLength(), 80 c.getSignatureIndex(), 81 c.getConstantPool()); 82 } 83 84 93 Signature( 94 final int name_index, 95 final int length, 96 final DataInputStream file, 97 final ConstantPool constant_pool) throws IOException 98 { 99 this(name_index, length, file.readUnsignedShort(), constant_pool); 100 } 101 102 108 public Signature( 109 final int name_index, 110 final int length, 111 final int signature_index, 112 final ConstantPool constant_pool) 113 { 114 super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool); 115 this.signature_index = signature_index; 116 } 117 118 125 public void accept(final Visitor v) { 126 } 129 130 136 public final void dump(final DataOutputStream file) throws IOException { 137 super.dump(file); 138 file.writeShort(signature_index); 139 } 140 141 144 public final int getSignatureIndex() { 145 return signature_index; 146 } 147 148 151 public final void setSignatureIndex(final int signature_index) { 152 this.signature_index = signature_index; 153 } 154 155 158 public final String getSignature() { 159 ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(signature_index, 160 Constants.CONSTANT_Utf8); 161 return c.getBytes(); 162 } 163 164 167 private static final class MyByteArrayInputStream extends 168 ByteArrayInputStream 169 { 170 MyByteArrayInputStream(final String data) { 171 super(data.getBytes()); 172 } 173 174 final int mark() { 175 return pos; 176 } 177 178 final String getData() { 179 return new String (buf); 180 } 181 182 final void reset(final int p) { 183 pos = p; 184 } 185 186 final void unread() { 187 if (pos > 0) { 188 pos--; 189 } 190 } 191 } 192 193 private static boolean identStart(final int ch) { 194 return ch == 'T' || ch == 'L'; 195 } 196 197 201 private static final void matchIdent( 202 final MyByteArrayInputStream in, 203 final StringBuffer buf) 204 { 205 int ch; 206 207 if ((ch = in.read()) == -1) { 208 throw new RuntimeException ("Illegal signature: " + in.getData() 209 + " no ident, reaching EOF"); 210 } 211 212 214 if (!identStart(ch)) { 215 StringBuffer buf2 = new StringBuffer (); 216 217 int count = 1; 218 while (Character.isJavaIdentifierPart((char) ch)) { 219 buf2.append((char) ch); 220 count++; 221 ch = in.read(); 222 } 223 224 if (ch == ':') { in.skip("Ljava/lang/Object".length()); 226 buf.append(buf2); 227 228 ch = in.read(); 229 in.unread(); 230 } else { 232 for (int i = 0; i < count; i++) { 233 in.unread(); 234 } 235 } 236 237 return; 238 } 239 240 StringBuffer buf2 = new StringBuffer (); 241 ch = in.read(); 242 243 do { 244 buf2.append((char) ch); 245 ch = in.read(); 246 248 } while (ch != -1 249 && (Character.isJavaIdentifierPart((char) ch) || ch == '/')); 250 251 buf.append(buf2.toString().replace('/', '.')); 252 253 255 if (ch != -1) { 256 in.unread(); 257 } 258 } 259 260 private static final void matchGJIdent( 261 final MyByteArrayInputStream in, 262 final StringBuffer buf) 263 { 264 int ch; 265 266 matchIdent(in, buf); 267 268 ch = in.read(); 269 if (ch == '<' || ch == '(') { buf.append((char) ch); 272 matchGJIdent(in, buf); 273 274 while ((ch = in.read()) != '>' && ch != ')') { if (ch == -1) { 277 throw new RuntimeException ("Illegal signature: " 278 + in.getData() + " reaching EOF"); 279 } 280 281 buf.append(", "); 283 in.unread(); 284 matchGJIdent(in, buf); } 286 287 289 buf.append((char) ch); 290 } else { 291 in.unread(); 292 } 293 294 ch = in.read(); 295 if (identStart(ch)) { 296 in.unread(); 297 matchGJIdent(in, buf); 298 } else if (ch == ')') { 299 in.unread(); 300 return; 301 } else if (ch != ';') { 302 throw new RuntimeException ("Illegal signature: " + in.getData() 303 + " read " + (char) ch); 304 } 305 } 306 307 public static String translate(final String s) { 308 StringBuffer buf = new StringBuffer (); 310 311 matchGJIdent(new MyByteArrayInputStream(s), buf); 312 313 return buf.toString(); 314 } 315 316 public static final boolean isFormalParameterList(final String s) { 317 return s.startsWith("<") && s.indexOf(':') > 0; 318 } 319 320 public static final boolean isActualParameterList(final String s) { 321 return s.startsWith("L") && s.endsWith(">;"); 322 } 323 324 327 public final String toString() { 328 String s = getSignature(); 329 330 return "Signature(" + s + ")"; 331 } 332 333 337 public Attribute copy(final ConstantPool constant_pool) { 338 return (Signature) clone(); 339 } 340 } 341 | Popular Tags |