1   package org.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.apache.bcel.classfile.*;
58  import org.apache.bcel.verifier.exc.*;
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.5 2002/08/05 17:31:32 enver Exp $
73   * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
74   */
75  public class StringRepresentation extends org.apache.bcel.classfile.EmptyVisitor implements Visitor{
76  	/*** The string representation, created by a visitXXX() method, output by toString(). */
77  	private String tostring;
78    /*** The node we ask for its string representation. Not really needed; only for debug output. */
79    private Node n;
80  	/***
81  	 * Creates a new StringRepresentation object which is the representation of n.
82  	 *
83  	 * @see #toString()
84  	 */
85  	public StringRepresentation(Node n){
86  		this.n = n;
87  		n.accept(this); // assign a string representation to field 'tostring' if we know n's class.
88  	}
89  	/***
90  	 * Returns the String representation.
91  	 */
92  	public String toString(){
93      // The run-time check below is needed because we don't want to omit inheritance
94      // of "EmptyVisitor" and provide a thousand empty methods.
95      // However, in terms of performance this would be a better idea.
96      // If some new "Node" is defined in BCEL (such as some concrete "Attribute"), we
97      // want to know that this class has also to be adapted.
98      if (tostring == null) throw new AssertionViolatedException("Please adapt '"+getClass()+"' to deal with objects of class '"+n.getClass()+"'.");
99  		return tostring;
100 	}
101 	/***
102 	 * Returns the String representation of the Node object obj;
103 	 * this is obj.toString() if it does not throw any RuntimeException,
104 	 * or else it is a string derived only from obj's class name.
105 	 */
106 	private String toString(Node obj){
107 		String ret;
108 		try{
109 			ret = obj.toString();
110 		}
111 		catch(RuntimeException e){
112 			String s = obj.getClass().getName();
113 			s = s.substring(s.lastIndexOf(".")+1);
114 			ret = "<<"+s+">>";
115     }
116     catch(ClassFormatError e){ /* BCEL can be harsh e.g. trying to convert the "signature" of a ReturnaddressType LocalVariable (shouldn't occur, but people do crazy things) */
117       String s = obj.getClass().getName();
118       s = s.substring(s.lastIndexOf(".")+1);
119       ret = "<<"+s+">>";
120 		}
121 		return ret;
122 	}
123 	////////////////////////////////
124 	// Visitor methods start here //
125 	////////////////////////////////
126 	// We don't of course need to call some default implementation:
127 	// e.g. we could also simply output "Code" instead of a possibly
128 	// lengthy Code attribute's toString().
129 	public void visitCode(Code obj){
130 		//tostring = toString(obj);
131 		tostring = "<CODE>"; // We don't need real code outputs.
132 	}
133 	public void visitCodeException(CodeException obj){
134 		tostring = toString(obj);
135 	}
136 	public void visitConstantClass(ConstantClass obj){
137 		tostring = toString(obj);
138 	}
139 	public void visitConstantDouble(ConstantDouble obj){
140 		tostring = toString(obj);
141 	}
142 	public void visitConstantFieldref(ConstantFieldref obj){
143 		tostring = toString(obj);
144 	}
145 	public void visitConstantFloat(ConstantFloat obj){
146 		tostring = toString(obj);
147 	}
148 	public void visitConstantInteger(ConstantInteger obj){
149 		tostring = toString(obj);
150 	}
151 	public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj){
152 		tostring = toString(obj);
153 	}
154 	public void visitConstantLong(ConstantLong obj){
155 		tostring = toString(obj);
156 	}
157 	public void visitConstantMethodref(ConstantMethodref obj){
158 		tostring = toString(obj);
159 	}
160 	public void visitConstantNameAndType(ConstantNameAndType obj){
161 		tostring = toString(obj);
162 	}
163  	public void visitConstantPool(ConstantPool obj){
164 		tostring = toString(obj);
165  	}
166 	public void visitConstantString(ConstantString obj){
167 		tostring = toString(obj);
168 	}
169 	public void visitConstantUtf8(ConstantUtf8 obj){
170 		tostring = toString(obj);
171 	}
172 	public void visitConstantValue(ConstantValue obj){
173 		tostring = toString(obj);
174 	}
175 	public void visitDeprecated(Deprecated obj){
176 		tostring = toString(obj);
177 	}
178 	public void visitExceptionTable(ExceptionTable obj){
179 		tostring = toString(obj);
180 	}
181 	public void visitField(Field obj){
182 		tostring = toString(obj);
183 	}
184 	public void visitInnerClass(InnerClass obj){
185 		tostring = toString(obj);
186 	}
187 	public void visitInnerClasses(InnerClasses obj){
188 		tostring = toString(obj);
189 	}
190 	public void visitJavaClass(JavaClass obj){
191 		tostring = toString(obj);
192 	}
193 	public void visitLineNumber(LineNumber obj){
194 		tostring = toString(obj);
195 	}
196 	public void visitLineNumberTable(LineNumberTable obj){
197 		tostring = "<LineNumberTable: "+toString(obj)+">";
198 	}
199 	public void visitLocalVariable(LocalVariable obj){
200 		tostring = toString(obj);
201 	}
202 	public void visitLocalVariableTable(LocalVariableTable obj){
203 		tostring = "<LocalVariableTable: "+toString(obj)+">";
204 	}
205 	public void visitMethod(Method obj){
206 		tostring = toString(obj);
207 	}
208   public void visitSignature(Signature obj){
209     tostring = toString(obj);
210   }
211 	public void visitSourceFile(SourceFile obj){
212 		tostring = toString(obj);
213 	} 
214   public void visitStackMap(StackMap obj){
215     tostring = toString(obj);
216   }
217 	public void visitSynthetic(Synthetic obj){
218 		tostring = toString(obj);
219 	} 
220 	public void visitUnknown(Unknown obj){
221 		tostring = toString(obj);
222 	}
223 }
This page was automatically generated by Maven