KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > bcel > internal > verifier > statics > StringRepresentation


1 package com.sun.org.apache.bcel.internal.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 com.sun.org.apache.bcel.internal.classfile.*;
58 import com.sun.org.apache.bcel.internal.classfile.Deprecated; // Use _this_ one!
59

60 /**
61  * BCEL's Node classes (those from the classfile API that <B>accept()</B> Visitor
62  * instances) have <B>toString()</B> methods that were not designed to be robust,
63  * this gap is closed by this class.
64  * When performing class file verification, it may be useful to output which
65  * entity (e.g. a <B>Code</B> instance) is not satisfying the verifier's
66  * constraints, but in this case it could be possible for the <B>toString()</B>
67  * method to throw a RuntimeException.
68  * A (new StringRepresentation(Node n)).toString() never throws any exception.
69  * Note that this class also serves as a placeholder for more sophisticated message
70  * handling in future versions of JustIce.
71  *
72  * @version $Id: StringRepresentation.java,v 1.1.1.1 2001/10/29 20:00:37 jvanzyl Exp $
73  * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
74  */

75 public class StringRepresentation extends com.sun.org.apache.bcel.internal.classfile.EmptyVisitor implements Visitor{
76     /** The string representation, created by a visitXXX() method, output by toString(). */
77     private String JavaDoc tostring;
78     /**
79      * Creates a new StringRepresentation object which is the representation of n.
80      *
81      * @see #toString()
82      */

83     public StringRepresentation(Node n){
84         n.accept(this);
85     }
86     /**
87      * Returns the String representation.
88      */

89     public String JavaDoc toString(){
90         return tostring;
91     }
92     /**
93      * Returns the String representation of the Node object obj;
94      * this is obj.toString() if it does not throw any RuntimeException,
95      * or else it is a string derived only from obj's class name.
96      */

97     private String JavaDoc toString(Node obj){
98         String JavaDoc ret;
99         try{
100             ret = obj.toString();
101         }
102         catch(RuntimeException JavaDoc e){
103             String JavaDoc s = obj.getClass().getName();
104             s = s.substring(s.lastIndexOf(".")+1);
105             ret = "<<"+s+">>";
106         }
107         return ret;
108     }
109     ////////////////////////////////
110
// Visitor methods start here //
111
////////////////////////////////
112
// We don't of course need to call some default implementation:
113
// e.g. we could also simply output "Code" instead of a possibly
114
// lengthy Code attribute's toString().
115
public void visitCode(Code obj){
116         //tostring = toString(obj);
117
tostring = "<CODE>"; // We don't need real code outputs.
118
}
119     public void visitCodeException(CodeException obj){
120         tostring = toString(obj);
121     }
122     public void visitConstantClass(ConstantClass obj){
123         tostring = toString(obj);
124     }
125     public void visitConstantDouble(ConstantDouble obj){
126         tostring = toString(obj);
127     }
128     public void visitConstantFieldref(ConstantFieldref obj){
129         tostring = toString(obj);
130     }
131     public void visitConstantFloat(ConstantFloat obj){
132         tostring = toString(obj);
133     }
134     public void visitConstantInteger(ConstantInteger obj){
135         tostring = toString(obj);
136     }
137     public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj){
138         tostring = toString(obj);
139     }
140     public void visitConstantLong(ConstantLong obj){
141         tostring = toString(obj);
142     }
143     public void visitConstantMethodref(ConstantMethodref obj){
144         tostring = toString(obj);
145     }
146     public void visitConstantNameAndType(ConstantNameAndType obj){
147         tostring = toString(obj);
148     }
149     public void visitConstantPool(ConstantPool obj){
150         tostring = toString(obj);
151     }
152     public void visitConstantString(ConstantString obj){
153         tostring = toString(obj);
154     }
155     public void visitConstantUtf8(ConstantUtf8 obj){
156         tostring = toString(obj);
157     }
158     public void visitConstantValue(ConstantValue obj){
159         tostring = toString(obj);
160     }
161     public void visitDeprecated(Deprecated JavaDoc obj){
162         tostring = toString(obj);
163     }
164     public void visitExceptionTable(ExceptionTable obj){
165         tostring = toString(obj);
166     }
167     public void visitField(Field obj){
168         tostring = toString(obj);
169     }
170     public void visitInnerClass(InnerClass obj){
171         tostring = toString(obj);
172     }
173     public void visitInnerClasses(InnerClasses obj){
174         tostring = toString(obj);
175     }
176     public void visitJavaClass(JavaClass obj){
177         tostring = toString(obj);
178     }
179     public void visitLineNumber(LineNumber obj){
180         tostring = toString(obj);
181     }
182     public void visitLineNumberTable(LineNumberTable obj){
183         tostring = "<LineNumberTable: "+toString(obj)+">";
184     }
185     public void visitLocalVariable(LocalVariable obj){
186         tostring = toString(obj);
187     }
188     public void visitLocalVariableTable(LocalVariableTable obj){
189         tostring = "<LocalVariableTable: "+toString(obj)+">";
190     }
191     public void visitMethod(Method obj){
192         tostring = toString(obj);
193     }
194     public void visitSourceFile(SourceFile obj){
195         tostring = toString(obj);
196     }
197     public void visitSynthetic(Synthetic obj){
198         tostring = toString(obj);
199     }
200     public void visitUnknown(Unknown obj){
201         tostring = toString(obj);
202     }
203 }
204
Popular Tags