You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringRepresentation.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package org.aspectj.apache.bcel.verifier.statics;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (https://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <https://www.apache.org/>.
  54. */
  55. import org.aspectj.apache.bcel.classfile.AnnotationDefault;
  56. import org.aspectj.apache.bcel.classfile.Code;
  57. import org.aspectj.apache.bcel.classfile.CodeException;
  58. import org.aspectj.apache.bcel.classfile.ConstantClass;
  59. import org.aspectj.apache.bcel.classfile.ConstantDouble;
  60. import org.aspectj.apache.bcel.classfile.ConstantFieldref;
  61. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  62. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  63. import org.aspectj.apache.bcel.classfile.ConstantInterfaceMethodref;
  64. import org.aspectj.apache.bcel.classfile.ConstantLong;
  65. import org.aspectj.apache.bcel.classfile.ConstantMethodref;
  66. import org.aspectj.apache.bcel.classfile.ConstantNameAndType;
  67. import org.aspectj.apache.bcel.classfile.ConstantPool;
  68. import org.aspectj.apache.bcel.classfile.ConstantString;
  69. import org.aspectj.apache.bcel.classfile.ConstantUtf8;
  70. import org.aspectj.apache.bcel.classfile.ConstantValue;
  71. import org.aspectj.apache.bcel.classfile.Deprecated;
  72. import org.aspectj.apache.bcel.classfile.ExceptionTable;
  73. import org.aspectj.apache.bcel.classfile.Field;
  74. import org.aspectj.apache.bcel.classfile.InnerClass;
  75. import org.aspectj.apache.bcel.classfile.InnerClasses;
  76. import org.aspectj.apache.bcel.classfile.JavaClass;
  77. import org.aspectj.apache.bcel.classfile.LineNumber;
  78. import org.aspectj.apache.bcel.classfile.LineNumberTable;
  79. import org.aspectj.apache.bcel.classfile.LocalVariable;
  80. import org.aspectj.apache.bcel.classfile.LocalVariableTable;
  81. import org.aspectj.apache.bcel.classfile.LocalVariableTypeTable;
  82. import org.aspectj.apache.bcel.classfile.Method;
  83. import org.aspectj.apache.bcel.classfile.Node;
  84. import org.aspectj.apache.bcel.classfile.Signature;
  85. import org.aspectj.apache.bcel.classfile.SourceFile;
  86. import org.aspectj.apache.bcel.classfile.StackMap;
  87. import org.aspectj.apache.bcel.classfile.Synthetic;
  88. import org.aspectj.apache.bcel.classfile.Unknown;
  89. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos;
  90. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos;
  91. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos;
  92. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos;
  93. import org.aspectj.apache.bcel.verifier.exc.AssertionViolatedException;
  94. /**
  95. * BCEL's Node classes (those from the classfile API that <B>accept()</B> Visitor
  96. * instances) have <B>toString()</B> methods that were not designed to be robust,
  97. * this gap is closed by this class.
  98. * When performing class file verification, it may be useful to output which
  99. * entity (e.g. a <B>Code</B> instance) is not satisfying the verifier's
  100. * constraints, but in this case it could be possible for the <B>toString()</B>
  101. * method to throw a RuntimeException.
  102. * A (new StringRepresentation(Node n)).toString() never throws any exception.
  103. * Note that this class also serves as a placeholder for more sophisticated message
  104. * handling in future versions of JustIce.
  105. *
  106. * @version $Id: StringRepresentation.java,v 1.4 2009/09/15 19:40:22 aclement Exp $
  107. * @author <A HREF="https://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
  108. */
  109. public class StringRepresentation extends org.aspectj.apache.bcel.verifier.EmptyClassVisitor{
  110. /** The string representation, created by a visitXXX() method, output by toString(). */
  111. private String tostring;
  112. /** The node we ask for its string representation. Not really needed; only for debug output. */
  113. private Node n;
  114. /**
  115. * Creates a new StringRepresentation object which is the representation of n.
  116. *
  117. * @see #toString()
  118. */
  119. public StringRepresentation(Node n){
  120. this.n = n;
  121. n.accept(this); // assign a string representation to field 'tostring' if we know n's class.
  122. }
  123. /**
  124. * Returns the String representation.
  125. */
  126. public String toString(){
  127. // The run-time check below is needed because we don't want to omit inheritance
  128. // of "EmptyVisitor" and provide a thousand empty methods.
  129. // However, in terms of performance this would be a better idea.
  130. // If some new "Node" is defined in BCEL (such as some concrete "Attribute"), we
  131. // want to know that this class has also to be adapted.
  132. if (tostring == null) throw new AssertionViolatedException("Please adapt '"+getClass()+"' to deal with objects of class '"+n.getClass()+"'.");
  133. return tostring;
  134. }
  135. /**
  136. * Returns the String representation of the Node object obj;
  137. * this is obj.toString() if it does not throw any RuntimeException,
  138. * or else it is a string derived only from obj's class name.
  139. */
  140. private String toString(Node obj){
  141. String ret;
  142. try{
  143. ret = obj.toString();
  144. }
  145. catch(RuntimeException e){
  146. String s = obj.getClass().getName();
  147. s = s.substring(s.lastIndexOf(".")+1);
  148. ret = "<<"+s+">>";
  149. }
  150. 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) */
  151. String s = obj.getClass().getName();
  152. s = s.substring(s.lastIndexOf(".")+1);
  153. ret = "<<"+s+">>";
  154. }
  155. return ret;
  156. }
  157. ////////////////////////////////
  158. // Visitor methods start here //
  159. ////////////////////////////////
  160. // We don't of course need to call some default implementation:
  161. // e.g. we could also simply output "Code" instead of a possibly
  162. // lengthy Code attribute's toString().
  163. public void visitCode(Code obj){
  164. //tostring = toString(obj);
  165. tostring = "<CODE>"; // We don't need real code outputs.
  166. }
  167. public void visitCodeException(CodeException obj){
  168. tostring = toString(obj);
  169. }
  170. public void visitConstantClass(ConstantClass obj){
  171. tostring = toString(obj);
  172. }
  173. public void visitConstantDouble(ConstantDouble obj){
  174. tostring = toString(obj);
  175. }
  176. public void visitConstantFieldref(ConstantFieldref obj){
  177. tostring = toString(obj);
  178. }
  179. public void visitConstantFloat(ConstantFloat obj){
  180. tostring = toString(obj);
  181. }
  182. public void visitConstantInteger(ConstantInteger obj){
  183. tostring = toString(obj);
  184. }
  185. public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj){
  186. tostring = toString(obj);
  187. }
  188. public void visitConstantLong(ConstantLong obj){
  189. tostring = toString(obj);
  190. }
  191. public void visitConstantMethodref(ConstantMethodref obj){
  192. tostring = toString(obj);
  193. }
  194. public void visitConstantNameAndType(ConstantNameAndType obj){
  195. tostring = toString(obj);
  196. }
  197. public void visitConstantPool(ConstantPool obj){
  198. tostring = toString(obj);
  199. }
  200. public void visitConstantString(ConstantString obj){
  201. tostring = toString(obj);
  202. }
  203. public void visitConstantUtf8(ConstantUtf8 obj){
  204. tostring = toString(obj);
  205. }
  206. public void visitConstantValue(ConstantValue obj){
  207. tostring = toString(obj);
  208. }
  209. public void visitDeprecated(Deprecated obj){
  210. tostring = toString(obj);
  211. }
  212. public void visitExceptionTable(ExceptionTable obj){
  213. tostring = toString(obj);
  214. }
  215. public void visitField(Field obj){
  216. tostring = toString(obj);
  217. }
  218. public void visitInnerClass(InnerClass obj){
  219. tostring = toString(obj);
  220. }
  221. public void visitInnerClasses(InnerClasses obj){
  222. tostring = toString(obj);
  223. }
  224. public void visitJavaClass(JavaClass obj){
  225. tostring = toString(obj);
  226. }
  227. public void visitLineNumber(LineNumber obj){
  228. tostring = toString(obj);
  229. }
  230. public void visitLineNumberTable(LineNumberTable obj){
  231. tostring = "<LineNumberTable: "+toString(obj)+">";
  232. }
  233. public void visitLocalVariable(LocalVariable obj){
  234. tostring = toString(obj);
  235. }
  236. public void visitLocalVariableTable(LocalVariableTable obj){
  237. tostring = "<LocalVariableTable: "+toString(obj)+">";
  238. }
  239. public void visitMethod(Method obj){
  240. tostring = toString(obj);
  241. }
  242. public void visitSignature(Signature obj){
  243. tostring = toString(obj);
  244. }
  245. public void visitSourceFile(SourceFile obj){
  246. tostring = toString(obj);
  247. }
  248. public void visitStackMap(StackMap obj){
  249. tostring = toString(obj);
  250. }
  251. public void visitSynthetic(Synthetic obj){
  252. tostring = toString(obj);
  253. }
  254. public void visitUnknown(Unknown obj){
  255. tostring = toString(obj);
  256. }
  257. public void visitRuntimeVisibleAnnotations(RuntimeVisAnnos obj) {tostring = toString(obj);}
  258. public void visitRuntimeInvisibleAnnotations(RuntimeInvisAnnos obj) {tostring = toString(obj);}
  259. public void visitRuntimeVisibleParameterAnnotations(RuntimeVisParamAnnos obj) {tostring = toString(obj);}
  260. public void visitRuntimeInvisibleParameterAnnotations(RuntimeInvisParamAnnos obj) {tostring = toString(obj);}
  261. public void visitAnnotationDefault(AnnotationDefault obj) {tostring = toString(obj);}
  262. public void visitLocalVariableTypeTable(LocalVariableTypeTable obj) {tostring = toString(obj);}
  263. }