Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

BootstrapMethods.java 8.5KB

12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
12 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. import java.io.ByteArrayInputStream;
  56. import java.io.DataInputStream;
  57. import java.io.DataOutputStream;
  58. import java.io.IOException;
  59. import org.aspectj.apache.bcel.Constants;
  60. /**
  61. * Represents the BootstrapMethods attribute in Java 7 classes.
  62. *
  63. * @author Andy Clement
  64. */
  65. public final class BootstrapMethods extends Attribute {
  66. // if 'isInPackedState' then this data needs unpacking
  67. private boolean isInPackedState = false;
  68. private byte[] data; // discarded once unpacked
  69. private int numBootstrapMethods;
  70. private BootstrapMethod[] bootstrapMethods;
  71. public BootstrapMethods(BootstrapMethods c) {
  72. this(c.getNameIndex(), c.getLength(), c.getBootstrapMethods(), c.getConstantPool());
  73. }
  74. public BootstrapMethods(int nameIndex, int length, BootstrapMethod[] lineNumberTable, ConstantPool constantPool) {
  75. super(Constants.ATTR_BOOTSTRAPMETHODS, nameIndex, length, constantPool);
  76. setBootstrapMethods(lineNumberTable);
  77. isInPackedState = false;
  78. }
  79. public final void setBootstrapMethods(BootstrapMethod[] bootstrapMethods) {
  80. this.data = null;
  81. this.isInPackedState = false;
  82. this.bootstrapMethods = bootstrapMethods;
  83. this.numBootstrapMethods = bootstrapMethods==null?0:bootstrapMethods.length;
  84. }
  85. BootstrapMethods(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException {
  86. this(name_index, length, (BootstrapMethod[])null, constant_pool);
  87. data = new byte[length];
  88. file.readFully(data);
  89. isInPackedState = true;
  90. }
  91. public static class BootstrapMethod {
  92. private int bootstrapMethodRef;
  93. private int[] bootstrapArguments;
  94. BootstrapMethod(DataInputStream file) throws IOException {
  95. this(file.readUnsignedShort(), readBootstrapArguments(file));
  96. }
  97. private static int[] readBootstrapArguments(DataInputStream dis) throws IOException {
  98. int numBootstrapMethods = dis.readUnsignedShort();
  99. int[] bootstrapArguments = new int[numBootstrapMethods];
  100. for (int i=0;i<numBootstrapMethods;i++) {
  101. bootstrapArguments[i] = dis.readUnsignedShort();
  102. }
  103. return bootstrapArguments;
  104. }
  105. public BootstrapMethod(int bootstrapMethodRef, int[] bootstrapArguments) {
  106. this.bootstrapMethodRef = bootstrapMethodRef;
  107. this.bootstrapArguments = bootstrapArguments;
  108. }
  109. public int getBootstrapMethodRef() {
  110. return bootstrapMethodRef;
  111. }
  112. public int[] getBootstrapArguments() {
  113. return bootstrapArguments;
  114. }
  115. public final void dump(DataOutputStream file) throws IOException {
  116. file.writeShort(bootstrapMethodRef);
  117. int len = bootstrapArguments.length;
  118. file.writeShort(len);
  119. for (int bootstrapArgument : bootstrapArguments) {
  120. file.writeShort(bootstrapArgument);
  121. }
  122. }
  123. public final int getLength() {
  124. return 2 /*bootstrapMethodRef*/+
  125. 2 /*number of arguments*/+
  126. 2 * bootstrapArguments.length;
  127. }
  128. }
  129. // Unpacks the byte array into the table
  130. private void unpack() {
  131. if (isInPackedState) {
  132. try {
  133. ByteArrayInputStream bs = new ByteArrayInputStream(data);
  134. DataInputStream dis = new DataInputStream(bs);
  135. numBootstrapMethods = dis.readUnsignedShort();
  136. bootstrapMethods = new BootstrapMethod[numBootstrapMethods];
  137. for (int i = 0; i < numBootstrapMethods; i++) {
  138. bootstrapMethods[i] = new BootstrapMethod(dis);
  139. }
  140. dis.close();
  141. data = null; // throw it away now
  142. } catch (IOException e) {
  143. throw new RuntimeException("Unpacking of LineNumberTable attribute failed");
  144. }
  145. isInPackedState = false;
  146. }
  147. }
  148. /**
  149. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  150. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  151. *
  152. * @param v Visitor object
  153. */
  154. @Override
  155. public void accept(ClassVisitor v) {
  156. unpack();
  157. v.visitBootstrapMethods(this);
  158. }
  159. /**
  160. * Dump line number table attribute to file stream in binary format.
  161. *
  162. * @param file Output file stream
  163. * @throws IOException
  164. */
  165. @Override
  166. public final void dump(DataOutputStream file) throws IOException {
  167. super.dump(file);
  168. if (isInPackedState) {
  169. file.write(data);
  170. } else {
  171. int blen = bootstrapMethods.length;
  172. file.writeShort(blen);
  173. for (BootstrapMethod bootstrapMethod : bootstrapMethods) {
  174. bootstrapMethod.dump(file);
  175. }
  176. }
  177. }
  178. public final BootstrapMethod[] getBootstrapMethods() {
  179. unpack();
  180. return bootstrapMethods;
  181. }
  182. /**
  183. * @return String representation.
  184. */
  185. @Override
  186. public final String toString() {
  187. unpack();
  188. StringBuilder buf = new StringBuilder();
  189. StringBuilder line = new StringBuilder();
  190. for (int i = 0; i < numBootstrapMethods; i++) {
  191. BootstrapMethod bm = bootstrapMethods[i];
  192. line.append("BootstrapMethod[").append(i).append("]:");
  193. int ref = bm.getBootstrapMethodRef();
  194. ConstantMethodHandle mh = (ConstantMethodHandle)getConstantPool().getConstant(ref);
  195. line.append("#"+ref+":");
  196. line.append(ConstantMethodHandle.kindToString(mh.getReferenceKind()));
  197. line.append(" ").append(getConstantPool().getConstant(mh.getReferenceIndex()));
  198. int [] args = bm.getBootstrapArguments();
  199. line.append(" argcount:").append(args==null?0:args.length).append(" ");
  200. if (args!=null) {
  201. for (int arg : args) {
  202. line.append(arg).append("(").append(getConstantPool().getConstant(arg)).append(") ");
  203. }
  204. }
  205. if (i < numBootstrapMethods - 1) {
  206. line.append(", ");
  207. }
  208. if (line.length() > 72) {
  209. line.append('\n');
  210. buf.append(line);
  211. line.setLength(0);
  212. }
  213. }
  214. buf.append(line);
  215. return buf.toString();
  216. }
  217. /**
  218. * @return deep copy of this attribute
  219. */
  220. // @Override
  221. // public Attribute copy(ConstantPool constant_pool) {
  222. // unpack();
  223. // LineNumberTable newTable = (LineNumberTable) clone();
  224. // newTable.table = new LineNumber[tableLength];
  225. // for (int i = 0; i < tableLength; i++) {
  226. // newTable.table[i] = table[i].copy();
  227. // }
  228. // newTable.cpool = constant_pool;
  229. // return newTable;
  230. // }
  231. public final int getNumBootstrapMethods () {
  232. unpack();
  233. return bootstrapMethods.length;
  234. }
  235. }