KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > apache > bcel > verifier > statics > StringRepresentation


1 package org.aspectj.apache.bcel.verifier.statics;
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.aspectj.apache.bcel.classfile.*;
58 import org.aspectj.apache.bcel.classfile.Deprecated;
59 import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleAnnotations;
60 import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisibleParameterAnnotations;
61 import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleAnnotations;
62 import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisibleParameterAnnotations;
63
64 /**
65  * BCEL's Node classes (those from the classfile API that <B>accept()</B>
66  * Visitor instances) have <B>toString()</B> methods that were not designed to
67  * be robust, this gap is closed by this class. When performing class file
68  * verification, it may be useful to output which entity (e.g. a <B>Code</B>
69  * instance) is not satisfying the verifier's constraints, but in this case it
70  * could be possible for the <B>toString()</B> method to throw a
71  * RuntimeException. A (new StringRepresentation(Node n)).toString() never
72  * throws any exception. Note that this class also serves as a placeholder for
73  * more sophisticated message handling in future versions of JustIce.
74  *
75  * @version $Id: StringRepresentation.java,v 1.1.2.1 2005/09/16 07:19:39
76  * ebruneton Exp $
77  * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
78  */

79 public class StringRepresentation extends
80         org.apache.bcel.classfile.EmptyVisitor implements Visitor
81 {
82     /**
83      * The string representation, created by a visitXXX() method, output by
84      * toString().
85      */

86     private String JavaDoc tostring;
87
88     /**
89      * The node we ask for its string representation. Not really needed; only
90      * for debug output.
91      */

92     // private Node n;
93
/**
94      * Creates a new StringRepresentation object which is the representation of
95      * n.
96      *
97      * @param n
98      *
99      * @see #toString()
100      */

101     public StringRepresentation(final Node n) {
102         // this.n = n;
103
n.accept(this); // assign a string representation to field 'tostring' if
104
// we know n's class.
105
}
106
107     /**
108      * Returns the String representation.
109      *
110      * @return TODO
111      */

112     public String JavaDoc toString() {
113         // The run-time check below is needed because we don't want to omit
114
// inheritance
115
// of "EmptyVisitor" and provide a thousand empty methods.
116
// However, in terms of performance this would be a better idea.
117
// If some new "Node" is defined in BCEL (such as some concrete
118
// "Attribute"), we
119
// want to know that this class has also to be adapted.
120
if (tostring == null) {
121             tostring = ""; // throw new AssertionViolatedException("Please
122
// adapt '"+getClass()+"' to deal with objects of
123
// class '"+n.getClass()+"'.");
124
}
125         return tostring;
126     }
127
128     /**
129      * Returns the String representation of the Node object obj; this is
130      * obj.toString() if it does not throw any RuntimeException, or else it is a
131      * string derived only from obj's class name.
132      *
133      * @param obj
134      * @return TODO
135      */

136     private String JavaDoc toString(final Node obj) {
137         String JavaDoc ret;
138         try {
139             ret = obj.toString();
140         } catch (RuntimeException JavaDoc e) {
141             String JavaDoc s = obj.getClass().getName();
142             s = s.substring(s.lastIndexOf(".") + 1);
143             ret = "<<" + s + ">>";
144         } catch (ClassFormatError JavaDoc e) { /*
145                                          * BCEL can be harsh e.g. trying to
146                                          * convert the "signature" of a
147                                          * ReturnaddressType LocalVariable
148                                          * (shouldn't occur, but people do crazy
149                                          * things)
150                                          */

151             String JavaDoc s = obj.getClass().getName();
152             s = s.substring(s.lastIndexOf(".") + 1);
153             ret = "<<" + s + ">>";
154         }
155         return ret;
156     }
157
158     // //////////////////////////////
159
// Visitor methods start here //
160
// //////////////////////////////
161
// We don't of course need to call some default implementation:
162
// e.g. we could also simply output "Code" instead of a possibly
163
// lengthy Code attribute's toString().
164
public void visitCode(final Code obj) {
165         // tostring = toString(obj);
166
tostring = "<CODE>"; // We don't need real code outputs.
167
}
168
169     public void visitCodeException(final CodeException obj) {
170         tostring = toString(obj);
171     }
172
173     public void visitConstantClass(final ConstantClass obj) {
174         tostring = toString(obj);
175     }
176
177     public void visitConstantDouble(final ConstantDouble obj) {
178         tostring = toString(obj);
179     }
180
181     public void visitConstantFieldref(final ConstantFieldref obj) {
182         tostring = toString(obj);
183     }
184
185     public void visitConstantFloat(final ConstantFloat obj) {
186         tostring = toString(obj);
187     }
188
189     public void visitConstantInteger(final ConstantInteger obj) {
190         tostring = toString(obj);
191     }
192
193     public void visitConstantInterfaceMethodref(
194         final ConstantInterfaceMethodref obj)
195     {
196         tostring = toString(obj);
197     }
198
199     public void visitConstantLong(final ConstantLong obj) {
200         tostring = toString(obj);
201     }
202
203     public void visitConstantMethodref(final ConstantMethodref obj) {
204         tostring = toString(obj);
205     }
206
207     public void visitConstantNameAndType(final ConstantNameAndType obj) {
208         tostring = toString(obj);
209     }
210
211     public void visitConstantPool(final ConstantPool obj) {
212         tostring = toString(obj);
213     }
214
215     public void visitConstantString(final ConstantString obj) {
216         tostring = toString(obj);
217     }
218
219     public void visitConstantUtf8(final ConstantUtf8 obj) {
220         tostring = toString(obj);
221     }
222
223     public void visitConstantValue(final ConstantValue obj) {
224         tostring = toString(obj);
225     }
226
227     public void visitDeprecated(final Deprecated JavaDoc obj) {
228         tostring = toString(obj);
229     }
230
231     public void visitExceptionTable(final ExceptionTable obj) {
232         tostring = toString(obj);
233     }
234
235     public void visitField(final Field obj) {
236         tostring = toString(obj);
237     }
238
239     public void visitInnerClass(final InnerClass obj) {
240         tostring = toString(obj);
241     }
242
243     public void visitInnerClasses(final InnerClasses obj) {
244         tostring = toString(obj);
245     }
246
247     public void visitJavaClass(final JavaClass obj) {
248         tostring = toString(obj);
249     }
250
251     public void visitLineNumber(final LineNumber obj) {
252         tostring = toString(obj);
253     }
254
255     public void visitLineNumberTable(final LineNumberTable obj) {
256         tostring = "<LineNumberTable: " + toString(obj) + ">";
257     }
258
259     public void visitLocalVariable(final LocalVariable obj) {
260         tostring = toString(obj);
261     }
262
263     public void visitLocalVariableTable(final LocalVariableTable obj) {
264         tostring = "<LocalVariableTable: " + toString(obj) + ">";
265     }
266
267     public void visitMethod(final Method obj) {
268         tostring = toString(obj);
269     }
270
271     public void visitSignature(final Signature obj) {
272         tostring = toString(obj);
273     }
274
275     public void visitSourceFile(final SourceFile obj) {
276         tostring = toString(obj);
277     }
278
279     public void visitStackMap(final StackMap obj) {
280         tostring = toString(obj);
281     }
282
283     public void visitSynthetic(final Synthetic obj) {
284         tostring = toString(obj);
285     }
286
287     public void visitUnknown(final Unknown obj) {
288         tostring = toString(obj);
289     }
290
291     public void visitStackMapEntry(final StackMapEntry arg0) {
292         tostring = "";
293     }
294
295     public void visitEnclosingMethod(final EnclosingMethod arg0) {
296         tostring = "";
297     }
298
299     public void visitRuntimeVisibleAnnotations(
300         final RuntimeVisibleAnnotations arg0)
301     {
302         tostring = "";
303     }
304
305     public void visitRuntimeInvisibleAnnotations(
306         final RuntimeInvisibleAnnotations arg0)
307     {
308         tostring = "";
309     }
310
311     public void visitRuntimeVisibleParameterAnnotations(
312         final RuntimeVisibleParameterAnnotations arg0)
313     {
314         tostring = "";
315     }
316
317     public void visitRuntimeInvisibleParameterAnnotations(
318         final RuntimeInvisibleParameterAnnotations arg0)
319     {
320         tostring = "";
321     }
322
323     public void visitAnnotationDefault(final AnnotationDefault arg0) {
324         tostring = "";
325     }
326
327     public void visitLocalVariableTypeTable(final LocalVariableTypeTable arg0) {
328         tostring = "";
329     }
330 }
331
Popular Tags