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.

Signature.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package org.aspectj.apache.bcel.classfile;
  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 (http://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. * <http://www.apache.org/>.
  54. *
  55. * Extended by Adrian Colyer, June 2005 to support unpacking of Signature
  56. * attribute
  57. */
  58. import java.io.ByteArrayInputStream;
  59. import java.io.DataInputStream;
  60. import java.io.DataOutputStream;
  61. import java.io.IOException;
  62. import org.aspectj.apache.bcel.Constants;
  63. /**
  64. * This class is derived from <em>Attribute</em> and represents a reference to a <href="http://wwwipd.ira.uka.de/~pizza/gj/">GJ</a>
  65. * attribute.
  66. *
  67. * @version $Id: Signature.java,v 1.11 2009/09/15 19:40:12 aclement Exp $
  68. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  69. * @see Attribute
  70. */
  71. public final class Signature extends Attribute {
  72. private int signature_index;
  73. /**
  74. * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical
  75. * copy.
  76. */
  77. public Signature(Signature c) {
  78. this(c.getNameIndex(), c.getLength(), c.getSignatureIndex(), c.getConstantPool());
  79. }
  80. /**
  81. * Construct object from file stream.
  82. *
  83. * @param name_index Index in constant pool to CONSTANT_Utf8
  84. * @param length Content length in bytes
  85. * @param file Input stream
  86. * @param constant_pool Array of constants
  87. * @throws IOException
  88. */
  89. Signature(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException {
  90. this(name_index, length, file.readUnsignedShort(), constant_pool);
  91. }
  92. /**
  93. * @param name_index Index in constant pool to CONSTANT_Utf8
  94. * @param length Content length in bytes
  95. * @param constant_pool Array of constants
  96. * @param Signature_index Index in constant pool to CONSTANT_Utf8
  97. */
  98. public Signature(int name_index, int length, int signature_index, ConstantPool constant_pool) {
  99. super(Constants.ATTR_SIGNATURE, name_index, length, constant_pool);
  100. this.signature_index = signature_index;
  101. }
  102. /**
  103. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  104. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  105. *
  106. * @param v Visitor object
  107. */
  108. @Override
  109. public void accept(ClassVisitor v) {
  110. System.err.println("Visiting non-standard Signature object");
  111. v.visitSignature(this);
  112. }
  113. /**
  114. * Dump source file attribute to file stream in binary format.
  115. *
  116. * @param file Output file stream
  117. * @throws IOException
  118. */
  119. @Override
  120. public final void dump(DataOutputStream file) throws IOException {
  121. super.dump(file);
  122. file.writeShort(signature_index);
  123. }
  124. /**
  125. * @return Index in constant pool of source file name.
  126. */
  127. public final int getSignatureIndex() {
  128. return signature_index;
  129. }
  130. /**
  131. * @param Signature_index.
  132. */
  133. public final void setSignatureIndex(int signature_index) {
  134. this.signature_index = signature_index;
  135. }
  136. /**
  137. * @return GJ signature.
  138. */
  139. public final String getSignature() {
  140. ConstantUtf8 c = (ConstantUtf8) cpool.getConstant(signature_index, Constants.CONSTANT_Utf8);
  141. return c.getValue();
  142. }
  143. /**
  144. * Extends ByteArrayInputStream to make 'unreading' chars possible.
  145. */
  146. private static final class MyByteArrayInputStream extends ByteArrayInputStream {
  147. MyByteArrayInputStream(String data) {
  148. super(data.getBytes());
  149. }
  150. final int mark() {
  151. return pos;
  152. }
  153. final String getData() {
  154. return new String(buf);
  155. }
  156. final void reset(int p) {
  157. pos = p;
  158. }
  159. final void unread() {
  160. if (pos > 0)
  161. pos--;
  162. }
  163. }
  164. private static boolean identStart(int ch) {
  165. return ch == 'T' || ch == 'L';
  166. }
  167. private static final void matchIdent(MyByteArrayInputStream in, StringBuffer buf) {
  168. int ch;
  169. if ((ch = in.read()) == -1)
  170. throw new RuntimeException("Illegal signature: " + in.getData() + " no ident, reaching EOF");
  171. // System.out.println("return from ident:" + (char)ch);
  172. if (!identStart(ch)) {
  173. StringBuilder buf2 = new StringBuilder();
  174. int count = 1;
  175. while (Character.isJavaIdentifierPart((char) ch)) {
  176. buf2.append((char) ch);
  177. count++;
  178. ch = in.read();
  179. }
  180. if (ch == ':') { // Ok, formal parameter
  181. in.skip("Ljava/lang/Object".length());
  182. buf.append(buf2);
  183. ch = in.read();
  184. in.unread();
  185. // System.out.println("so far:" + buf2 + ":next:" +(char)ch);
  186. } else {
  187. for (int i = 0; i < count; i++)
  188. in.unread();
  189. }
  190. return;
  191. }
  192. StringBuilder buf2 = new StringBuilder();
  193. ch = in.read();
  194. do {
  195. buf2.append((char) ch);
  196. ch = in.read();
  197. // System.out.println("within ident:"+ (char)ch);
  198. } while ((ch != -1) && (Character.isJavaIdentifierPart((char) ch) || (ch == '/')));
  199. buf.append(buf2.toString().replace('/', '.'));
  200. // System.out.println("regular return ident:"+ (char)ch + ":" + buf2);
  201. if (ch != -1)
  202. in.unread();
  203. }
  204. private static final void matchGJIdent(MyByteArrayInputStream in, StringBuffer buf) {
  205. int ch;
  206. matchIdent(in, buf);
  207. ch = in.read();
  208. if ((ch == '<') || ch == '(') { // Parameterized or method
  209. // System.out.println("Enter <");
  210. buf.append((char) ch);
  211. matchGJIdent(in, buf);
  212. while (((ch = in.read()) != '>') && (ch != ')')) { // List of parameters
  213. if (ch == -1)
  214. throw new RuntimeException("Illegal signature: " + in.getData() + " reaching EOF");
  215. // System.out.println("Still no >");
  216. buf.append(", ");
  217. in.unread();
  218. matchGJIdent(in, buf); // Recursive call
  219. }
  220. // System.out.println("Exit >");
  221. buf.append((char) ch);
  222. } else
  223. in.unread();
  224. ch = in.read();
  225. if (identStart(ch)) {
  226. in.unread();
  227. matchGJIdent(in, buf);
  228. } else if (ch == ')') {
  229. in.unread();
  230. return;
  231. } else if (ch != ';')
  232. throw new RuntimeException("Illegal signature: " + in.getData() + " read " + (char) ch);
  233. }
  234. public static String translate(String s) {
  235. // System.out.println("Sig:" + s);
  236. StringBuffer buf = new StringBuffer();
  237. matchGJIdent(new MyByteArrayInputStream(s), buf);
  238. return buf.toString();
  239. }
  240. public static final boolean isFormalParameterList(String s) {
  241. return s.startsWith("<") && (s.indexOf(':') > 0);
  242. }
  243. public static final boolean isActualParameterList(String s) {
  244. return s.startsWith("L") && s.endsWith(">;");
  245. }
  246. /**
  247. * @return String representation
  248. */
  249. @Override
  250. public final String toString() {
  251. String s = getSignature();
  252. return "Signature(" + s + ")";
  253. }
  254. // /**
  255. // * @return deep copy of this attribute
  256. // */
  257. // @Override
  258. // public Attribute copy(ConstantPool constant_pool) {
  259. // return (Signature) clone();
  260. // }
  261. }