1 17 package org.apache.bcel.classfile; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.DataInputStream ; 21 import java.io.DataOutputStream ; 22 import java.io.IOException ; 23 import org.apache.bcel.Constants; 24 25 33 public final class Signature extends Attribute { 34 35 private int signature_index; 36 37 38 42 public Signature(Signature c) { 43 this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool()); 44 } 45 46 47 55 Signature(int name_index, int length, DataInputStream file, ConstantPool constant_pool) 56 throws IOException { 57 this(name_index, length, file.readUnsignedShort(), constant_pool); 58 } 59 60 61 67 public Signature(int name_index, int length, int signature_index, ConstantPool constant_pool) { 68 super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool); 69 this.signature_index = signature_index; 70 } 71 72 73 80 public void accept( Visitor v ) { 81 v.visitSignature(this); 83 } 84 85 86 92 public final void dump( DataOutputStream file ) throws IOException { 93 super.dump(file); 94 file.writeShort(signature_index); 95 } 96 97 98 101 public final int getSignatureIndex() { 102 return signature_index; 103 } 104 105 106 109 public final void setSignatureIndex( int signature_index ) { 110 this.signature_index = signature_index; 111 } 112 113 114 117 public final String getSignature() { 118 ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(signature_index, 119 Constants.CONSTANT_Utf8); 120 return c.getBytes(); 121 } 122 123 126 private static final class MyByteArrayInputStream extends ByteArrayInputStream { 127 128 MyByteArrayInputStream(String data) { 129 super(data.getBytes()); 130 } 131 132 133 final int mark() { 134 return pos; 135 } 136 137 138 final String getData() { 139 return new String (buf); 140 } 141 142 143 final void reset( int p ) { 144 pos = p; 145 } 146 147 148 final void unread() { 149 if (pos > 0) { 150 pos--; 151 } 152 } 153 } 154 155 156 private static boolean identStart( int ch ) { 157 return ch == 'T' || ch == 'L'; 158 } 159 160 161 private static final void matchIdent( MyByteArrayInputStream in, StringBuffer buf ) { 162 int ch; 163 if ((ch = in.read()) == -1) { 164 throw new RuntimeException ("Illegal signature: " + in.getData() 165 + " no ident, reaching EOF"); 166 } 167 if (!identStart(ch)) { 169 StringBuffer buf2 = new StringBuffer (); 170 int count = 1; 171 while (Character.isJavaIdentifierPart((char) ch)) { 172 buf2.append((char) ch); 173 count++; 174 ch = in.read(); 175 } 176 if (ch == ':') { in.skip("Ljava/lang/Object".length()); 178 buf.append(buf2); 179 ch = in.read(); 180 in.unread(); 181 } else { 183 for (int i = 0; i < count; i++) { 184 in.unread(); 185 } 186 } 187 return; 188 } 189 StringBuffer buf2 = new StringBuffer (); 190 ch = in.read(); 191 do { 192 buf2.append((char) ch); 193 ch = in.read(); 194 } while ((ch != -1) && (Character.isJavaIdentifierPart((char) ch) || (ch == '/'))); 196 buf.append(buf2.toString().replace('/', '.')); 197 if (ch != -1) { 199 in.unread(); 200 } 201 } 202 203 204 private static final void matchGJIdent( MyByteArrayInputStream in, StringBuffer buf ) { 205 int ch; 206 matchIdent(in, buf); 207 ch = in.read(); 208 if ((ch == '<') || ch == '(') { buf.append((char) ch); 211 matchGJIdent(in, buf); 212 while (((ch = in.read()) != '>') && (ch != ')')) { if (ch == -1) { 214 throw new RuntimeException ("Illegal signature: " + in.getData() 215 + " reaching EOF"); 216 } 217 buf.append(", "); 219 in.unread(); 220 matchGJIdent(in, buf); } 222 buf.append((char) ch); 224 } else { 225 in.unread(); 226 } 227 ch = in.read(); 228 if (identStart(ch)) { 229 in.unread(); 230 matchGJIdent(in, buf); 231 } else if (ch == ')') { 232 in.unread(); 233 return; 234 } else if (ch != ';') { 235 throw new RuntimeException ("Illegal signature: " + in.getData() + " read " + (char) ch); 236 } 237 } 238 239 240 public static String translate( String s ) { 241 StringBuffer buf = new StringBuffer (); 243 matchGJIdent(new MyByteArrayInputStream(s), buf); 244 return buf.toString(); 245 } 246 247 248 public static final boolean isFormalParameterList( String s ) { 249 return s.startsWith("<") && (s.indexOf(':') > 0); 250 } 251 252 253 public static final boolean isActualParameterList( String s ) { 254 return s.startsWith("L") && s.endsWith(">;"); 255 } 256 257 258 261 public final String toString() { 262 String s = getSignature(); 263 return "Signature(" + s + ")"; 264 } 265 266 267 270 public Attribute copy( ConstantPool _constant_pool ) { 271 return (Signature) clone(); 272 } 273 } 274 | Popular Tags |