KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > classfile > Signature


1 package com.sun.org.apache.bcel.internal.classfile;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" and
29  * "Apache BCEL" must not be used to endorse or promote products
30  * derived from this software without prior written permission. For
31  * written permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * "Apache BCEL", nor may "Apache" appear in their name, without
35  * prior written permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import com.sun.org.apache.bcel.internal.Constants;
58 import java.io.*;
59
60 /**
61  * This class is derived from <em>Attribute</em> and represents a reference
62  * to a <href="http://wwwipd.ira.uka.de/~pizza/gj/">GJ</a> attribute.
63  *
64  * @version $Id: Signature.java,v 1.1.1.1 2001/10/29 20:00:03 jvanzyl Exp $
65  * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
66  * @see Attribute
67  */

68 public final class Signature extends Attribute {
69   private int signature_index;
70
71   /**
72    * Initialize from another object. Note that both objects use the same
73    * references (shallow copy). Use clone() for a physical copy.
74    */

75   public Signature(Signature c) {
76     this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool());
77   }
78
79   /**
80    * Construct object from file stream.
81    * @param name_index Index in constant pool to CONSTANT_Utf8
82    * @param length Content length in bytes
83    * @param file Input stream
84    * @param constant_pool Array of constants
85    * @throw IOException
86    */

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   /**
94    * @param name_index Index in constant pool to CONSTANT_Utf8
95    * @param length Content length in bytes
96    * @param constant_pool Array of constants
97    * @param Signature_index Index in constant pool to CONSTANT_Utf8
98    */

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   /**
107    * Called by objects that are traversing the nodes of the tree implicitely
108    * defined by the contents of a Java class. I.e., the hierarchy of methods,
109    * fields, attributes, etc. spawns a tree of objects.
110    *
111    * @param v Visitor object
112    */

113    public void accept(Visitor v) {
114      System.err.println("Visiting non-standard Signature object");
115    }
116    
117   /**
118    * Dump source file attribute to file stream in binary format.
119    *
120    * @param file Output file stream
121    * @throw IOException
122    */

123   public final void dump(DataOutputStream file) throws IOException
124   {
125     super.dump(file);
126     file.writeShort(signature_index);
127   }
128
129   /**
130    * @return Index in constant pool of source file name.
131    */

132   public final int getSignatureIndex() { return signature_index; }
133
134   /**
135    * @param Signature_index.
136    */

137   public final void setSignatureIndex(int signature_index) {
138     this.signature_index = signature_index;
139   }
140
141   /**
142    * @return GJ signature.
143    */

144   public final String JavaDoc getSignature() {
145     ConstantUtf8 c = (ConstantUtf8)constant_pool.getConstant(signature_index,
146                                  Constants.CONSTANT_Utf8);
147     return c.getBytes();
148   }
149
150   /**
151    * Extends ByteArrayInputStream to make 'unreading' chars possible.
152    */

153   private static final class MyByteArrayInputStream extends ByteArrayInputStream {
154     MyByteArrayInputStream(String JavaDoc data) { super(data.getBytes()); }
155     final int mark() { return pos; }
156     final String JavaDoc getData() { return new String JavaDoc(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 JavaDoc buf) {
170     int ch;
171
172     if((ch = in.read()) == -1)
173       throw new RuntimeException JavaDoc("Illegal signature: " + in.getData() +
174                  " no ident, reaching EOF");
175
176     //System.out.println("return from ident:" + (char)ch);
177

178     if(!identStart(ch)) {
179       StringBuffer JavaDoc buf2 = new StringBuffer JavaDoc();
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 == ':') { // Ok, formal parameter
189
in.skip("Ljava/lang/Object".length());
190     buf.append(buf2);
191
192         ch = in.read();
193     in.unread();
194     //System.out.println("so far:" + buf2 + ":next:" +(char)ch);
195
} else {
196     for(int i=0; i < count; i++)
197       in.unread();
198       }
199
200       return;
201     }
202
203     StringBuffer JavaDoc buf2 = new StringBuffer JavaDoc();
204     ch = in.read();
205
206     do {
207       buf2.append((char)ch);
208       ch = in.read();
209       //System.out.println("within ident:"+ (char)ch);
210

211     } while((ch != -1) && (Character.isJavaIdentifierPart((char)ch) || (ch == '/')));
212
213     buf.append(buf2.toString().replace('/', '.'));
214
215     //System.out.println("regular return ident:"+ (char)ch + ":" + buf2);
216

217     if(ch != -1)
218       in.unread();
219   }
220
221   private static final void matchGJIdent(MyByteArrayInputStream in,
222                      StringBuffer JavaDoc buf)
223   {
224     int ch;
225
226     matchIdent(in, buf);
227
228     ch = in.read();
229     if((ch == '<') || ch == '(') { // Parameterized or method
230
//System.out.println("Enter <");
231
buf.append((char)ch);
232       matchGJIdent(in, buf);
233       
234       while(((ch = in.read()) != '>') && (ch != ')')) { // List of parameters
235
if(ch == -1)
236       throw new RuntimeException JavaDoc("Illegal signature: " + in.getData() +
237                      " reaching EOF");
238
239     //System.out.println("Still no >");
240
buf.append(", ");
241     in.unread();
242     matchGJIdent(in, buf); // Recursive call
243
}
244
245       //System.out.println("Exit >");
246

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 JavaDoc("Illegal signature: " + in.getData() + " read " +
260                  (char)ch);
261   }
262
263   public static String JavaDoc translate(String JavaDoc s) {
264     //System.out.println("Sig:" + s);
265
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
266
267     matchGJIdent(new MyByteArrayInputStream(s), buf);
268
269     return buf.toString();
270   }
271
272   public static final boolean isFormalParameterList(String JavaDoc s) {
273     return s.startsWith("<") && (s.indexOf(':') > 0);
274   }
275
276   public static final boolean isActualParameterList(String JavaDoc s) {
277     return s.startsWith("L") && s.endsWith(">;");
278   }
279
280   /**
281    * @return String representation
282    */

283   public final String JavaDoc toString() {
284     String JavaDoc s = getSignature();
285
286     return "Signature(" + s + ")";
287   }
288
289   /**
290    * @return deep copy of this attribute
291    */

292   public Attribute copy(ConstantPool constant_pool) {
293     return (Signature)clone();
294   }
295 }
296
Popular Tags