1 package com.sun.org.apache.bcel.internal.classfile; 2 3 56 57 import com.sun.org.apache.bcel.internal.Constants; 58 import java.io.*; 59 60 68 public final class Signature extends Attribute { 69 private int signature_index; 70 71 75 public Signature(Signature c) { 76 this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool()); 77 } 78 79 87 Signature(int name_index, int length, DataInputStream file, 88 ConstantPool constant_pool) throws IOException 89 { 90 this(name_index, length, file.readUnsignedShort(), constant_pool); 91 } 92 93 99 public Signature(int name_index, int length, int signature_index, 100 ConstantPool constant_pool) 101 { 102 super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool); 103 this.signature_index = signature_index; 104 } 105 106 113 public void accept(Visitor v) { 114 System.err.println("Visiting non-standard Signature object"); 115 } 116 117 123 public final void dump(DataOutputStream file) throws IOException 124 { 125 super.dump(file); 126 file.writeShort(signature_index); 127 } 128 129 132 public final int getSignatureIndex() { return signature_index; } 133 134 137 public final void setSignatureIndex(int signature_index) { 138 this.signature_index = signature_index; 139 } 140 141 144 public final String getSignature() { 145 ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(signature_index, 146 Constants.CONSTANT_Utf8); 147 return c.getBytes(); 148 } 149 150 153 private static final class MyByteArrayInputStream extends ByteArrayInputStream { 154 MyByteArrayInputStream(String data) { super(data.getBytes()); } 155 final int mark() { return pos; } 156 final String getData() { return new String (buf); } 157 final void reset(int p) { pos = p; } 158 final void unread() { if(pos > 0) pos--; } 159 } 160 161 private static boolean identStart(int ch) { 162 return ch == 'T' || ch == 'L'; 163 } 164 165 private static boolean identPart(int ch) { 166 return ch == '/' || ch == ';'; 167 } 168 169 private static final void matchIdent(MyByteArrayInputStream in, StringBuffer buf) { 170 int ch; 171 172 if((ch = in.read()) == -1) 173 throw new RuntimeException ("Illegal signature: " + in.getData() + 174 " no ident, reaching EOF"); 175 176 178 if(!identStart(ch)) { 179 StringBuffer buf2 = new StringBuffer (); 180 181 int count = 1; 182 while(Character.isJavaIdentifierPart((char)ch)) { 183 buf2.append((char)ch); 184 count++; 185 ch = in.read(); 186 } 187 188 if(ch == ':') { in.skip("Ljava/lang/Object".length()); 190 buf.append(buf2); 191 192 ch = in.read(); 193 in.unread(); 194 } else { 196 for(int i=0; i < count; i++) 197 in.unread(); 198 } 199 200 return; 201 } 202 203 StringBuffer buf2 = new StringBuffer (); 204 ch = in.read(); 205 206 do { 207 buf2.append((char)ch); 208 ch = in.read(); 209 211 } while((ch != -1) && (Character.isJavaIdentifierPart((char)ch) || (ch == '/'))); 212 213 buf.append(buf2.toString().replace('/', '.')); 214 215 217 if(ch != -1) 218 in.unread(); 219 } 220 221 private static final void matchGJIdent(MyByteArrayInputStream in, 222 StringBuffer buf) 223 { 224 int ch; 225 226 matchIdent(in, buf); 227 228 ch = in.read(); 229 if((ch == '<') || ch == '(') { buf.append((char)ch); 232 matchGJIdent(in, buf); 233 234 while(((ch = in.read()) != '>') && (ch != ')')) { if(ch == -1) 236 throw new RuntimeException ("Illegal signature: " + in.getData() + 237 " reaching EOF"); 238 239 buf.append(", "); 241 in.unread(); 242 matchGJIdent(in, buf); } 244 245 247 buf.append((char)ch); 248 } else 249 in.unread(); 250 251 ch = in.read(); 252 if(identStart(ch)) { 253 in.unread(); 254 matchGJIdent(in, buf); 255 } else if(ch == ')') { 256 in.unread(); 257 return; 258 } else if(ch != ';') 259 throw new RuntimeException ("Illegal signature: " + in.getData() + " read " + 260 (char)ch); 261 } 262 263 public static String translate(String s) { 264 StringBuffer buf = new StringBuffer (); 266 267 matchGJIdent(new MyByteArrayInputStream(s), buf); 268 269 return buf.toString(); 270 } 271 272 public static final boolean isFormalParameterList(String s) { 273 return s.startsWith("<") && (s.indexOf(':') > 0); 274 } 275 276 public static final boolean isActualParameterList(String s) { 277 return s.startsWith("L") && s.endsWith(">;"); 278 } 279 280 283 public final String toString() { 284 String s = getSignature(); 285 286 return "Signature(" + s + ")"; 287 } 288 289 292 public Attribute copy(ConstantPool constant_pool) { 293 return (Signature)clone(); 294 } 295 } 296 | Popular Tags |