KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > apache > bcel > classfile > Signature


1 package org.aspectj.apache.bcel.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 org.apache.bcel.Constants;
58 import java.io.*;
59
60 /**
61  * This class is derived from <em>Attribute</em> and represents a reference to
62  * a <href="http://wwwipd.ira.uka.de/~pizza/gj/">GJ</a> attribute.
63  *
64  * @version $Id: Signature.java,v 1.2 2006/07/05 15:22:56 ebruneton 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      * @param c
76      */

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

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     /**
103      * @param name_index Index in constant pool to CONSTANT_Utf8
104      * @param length Content length in bytes
105      * @param constant_pool Array of constants
106      * @param signature_index Index in constant pool to CONSTANT_Utf8
107      */

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     /**
119      * Called by objects that are traversing the nodes of the tree implicitely
120      * defined by the contents of a Java class. I.e., the hierarchy of methods,
121      * fields, attributes, etc. spawns a tree of objects.
122      *
123      * @param v Visitor object
124      */

125     public void accept(final Visitor v) {
126         // System.err.println("Visiting non-standard Signature object");
127
// v.visitSignature(this);
128
}
129
130     /**
131      * Dump source file attribute to file stream in binary format.
132      *
133      * @param file Output file stream
134      * @throws IOException
135      */

136     public final void dump(final DataOutputStream file) throws IOException {
137         super.dump(file);
138         file.writeShort(signature_index);
139     }
140
141     /**
142      * @return Index in constant pool of source file name.
143      */

144     public final int getSignatureIndex() {
145         return signature_index;
146     }
147
148     /**
149      * @param signature_index
150      */

151     public final void setSignatureIndex(final int signature_index) {
152         this.signature_index = signature_index;
153     }
154
155     /**
156      * @return GJ signature.
157      */

158     public final String JavaDoc getSignature() {
159         ConstantUtf8 c = (ConstantUtf8) constant_pool.getConstant(signature_index,
160                 Constants.CONSTANT_Utf8);
161         return c.getBytes();
162     }
163
164     /**
165      * Extends ByteArrayInputStream to make 'unreading' chars possible.
166      */

167     private static final class MyByteArrayInputStream extends
168             ByteArrayInputStream
169     {
170         MyByteArrayInputStream(final String JavaDoc data) {
171             super(data.getBytes());
172         }
173
174         final int mark() {
175             return pos;
176         }
177
178         final String JavaDoc getData() {
179             return new String JavaDoc(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     // private static boolean identPart(int ch) {
198
// return ch == '/' || ch == ';';
199
// }
200

201     private static final void matchIdent(
202         final MyByteArrayInputStream in,
203         final StringBuffer JavaDoc buf)
204     {
205         int ch;
206
207         if ((ch = in.read()) == -1) {
208             throw new RuntimeException JavaDoc("Illegal signature: " + in.getData()
209                     + " no ident, reaching EOF");
210         }
211
212         // System.out.println("return from ident:" + (char)ch);
213

214         if (!identStart(ch)) {
215             StringBuffer JavaDoc buf2 = new StringBuffer JavaDoc();
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 == ':') { // Ok, formal parameter
225
in.skip("Ljava/lang/Object".length());
226                 buf.append(buf2);
227
228                 ch = in.read();
229                 in.unread();
230                 // System.out.println("so far:" + buf2 + ":next:" +(char)ch);
231
} else {
232                 for (int i = 0; i < count; i++) {
233                     in.unread();
234                 }
235             }
236
237             return;
238         }
239
240         StringBuffer JavaDoc buf2 = new StringBuffer JavaDoc();
241         ch = in.read();
242
243         do {
244             buf2.append((char) ch);
245             ch = in.read();
246             // System.out.println("within ident:"+ (char)ch);
247

248         } while (ch != -1
249                 && (Character.isJavaIdentifierPart((char) ch) || ch == '/'));
250
251         buf.append(buf2.toString().replace('/', '.'));
252
253         // System.out.println("regular return ident:"+ (char)ch + ":" + buf2);
254

255         if (ch != -1) {
256             in.unread();
257         }
258     }
259
260     private static final void matchGJIdent(
261         final MyByteArrayInputStream in,
262         final StringBuffer JavaDoc buf)
263     {
264         int ch;
265
266         matchIdent(in, buf);
267
268         ch = in.read();
269         if (ch == '<' || ch == '(') { // Parameterized or method
270
// System.out.println("Enter <");
271
buf.append((char) ch);
272             matchGJIdent(in, buf);
273
274             while ((ch = in.read()) != '>' && ch != ')') { // List of
275
// parameters
276
if (ch == -1) {
277                     throw new RuntimeException JavaDoc("Illegal signature: "
278                             + in.getData() + " reaching EOF");
279                 }
280
281                 // System.out.println("Still no >");
282
buf.append(", ");
283                 in.unread();
284                 matchGJIdent(in, buf); // Recursive call
285
}
286
287             // System.out.println("Exit >");
288

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 JavaDoc("Illegal signature: " + in.getData()
303                     + " read " + (char) ch);
304         }
305     }
306
307     public static String JavaDoc translate(final String JavaDoc s) {
308         // System.out.println("Sig:" + s);
309
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
310
311         matchGJIdent(new MyByteArrayInputStream(s), buf);
312
313         return buf.toString();
314     }
315
316     public static final boolean isFormalParameterList(final String JavaDoc s) {
317         return s.startsWith("<") && s.indexOf(':') > 0;
318     }
319
320     public static final boolean isActualParameterList(final String JavaDoc s) {
321         return s.startsWith("L") && s.endsWith(">;");
322     }
323
324     /**
325      * @return String representation
326      */

327     public final String JavaDoc toString() {
328         String JavaDoc s = getSignature();
329
330         return "Signature(" + s + ")";
331     }
332
333     /**
334      * @param constant_pool
335      * @return deep copy of this attribute
336      */

337     public Attribute copy(final ConstantPool constant_pool) {
338         return (Signature) clone();
339     }
340 }
341
Popular Tags