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.

ClassParser.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 (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 java.io.BufferedInputStream;
  56. import java.io.ByteArrayInputStream;
  57. import java.io.DataInputStream;
  58. import java.io.FileInputStream;
  59. import java.io.IOException;
  60. import java.io.InputStream;
  61. import org.aspectj.apache.bcel.Constants;
  62. /**
  63. * Wrapper class that parses a given Java .class file. The method <A
  64. * href ="#parse">parse</A> returns a <A href ="JavaClass.html">
  65. * JavaClass</A> object on success. When an I/O error or an
  66. * inconsistency occurs an appropiate exception is propagated back to
  67. * the caller.
  68. *
  69. * The structure and the names comply, except for a few conveniences,
  70. * exactly with the <A href="ftp://java.sun.com/docs/specs/vmspec.ps">
  71. * JVM specification 1.0</a>. See this paper for
  72. * further details about the structure of a bytecode file.
  73. *
  74. * @version $Id: ClassParser.java,v 1.6 2008/05/30 17:29:14 aclement Exp $
  75. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  76. */
  77. public final class ClassParser {
  78. private DataInputStream file;
  79. private String filename;
  80. private int classnameIndex;
  81. private int superclassnameIndex;
  82. private int major, minor;
  83. private int accessflags;
  84. private int[] interfaceIndices;
  85. private ConstantPool cpool;
  86. private Field[] fields;
  87. private Method[] methods;
  88. private Attribute[] attributes;
  89. private static final int BUFSIZE = 8192;
  90. /** Parse class from the given stream */
  91. public ClassParser(InputStream file, String filename) {
  92. this.filename = filename;
  93. if (file instanceof DataInputStream) this.file = (DataInputStream)file;
  94. else this.file = new DataInputStream(new BufferedInputStream(file,BUFSIZE));
  95. }
  96. public ClassParser(ByteArrayInputStream baos, String filename) {
  97. this.filename = filename;
  98. this.file = new DataInputStream(baos);
  99. }
  100. /** Parse class from given .class file */
  101. public ClassParser(String file_name) throws IOException {
  102. this.filename = file_name;
  103. file = new DataInputStream(new BufferedInputStream(new FileInputStream(file_name),BUFSIZE));
  104. }
  105. /**
  106. * Parse the given Java class file and return an object that represents
  107. * the contained data, i.e., constants, methods, fields and commands.
  108. * A <em>ClassFormatException</em> is raised, if the file is not a valid
  109. * .class file. (This does not include verification of the byte code as it
  110. * is performed by the java interpreter).
  111. */
  112. public JavaClass parse() throws IOException, ClassFormatException {
  113. /****************** Read headers ********************************/
  114. // Check magic tag of class file
  115. readID();
  116. // Get compiler version
  117. readVersion();
  118. /****************** Read constant pool and related **************/
  119. // Read constant pool entries
  120. readConstantPool();
  121. // Get class information
  122. readClassInfo();
  123. // Get interface information, i.e., implemented interfaces
  124. readInterfaces();
  125. /****************** Read class fields and methods ***************/
  126. // Read class fields, i.e., the variables of the class
  127. readFields();
  128. // Read class methods, i.e., the functions in the class
  129. readMethods();
  130. // Read class attributes
  131. readAttributes();
  132. // Read everything of interest, so close the file
  133. file.close();
  134. // Return the information we have gathered in a new object
  135. JavaClass jc= new JavaClass(classnameIndex, superclassnameIndex,
  136. filename, major, minor, accessflags,
  137. cpool, interfaceIndices, fields,
  138. methods, attributes);
  139. return jc;
  140. }
  141. /** Read information about the attributes of the class */
  142. private final void readAttributes() {
  143. attributes = AttributeUtils.readAttributes(file,cpool);
  144. }
  145. /** Read information about the class and its super class */
  146. private final void readClassInfo() throws IOException {
  147. accessflags = file.readUnsignedShort();
  148. /* Interfaces are implicitely abstract, the flag should be set
  149. * according to the JVM specification */
  150. if((accessflags & Constants.ACC_INTERFACE) != 0)
  151. accessflags |= Constants.ACC_ABSTRACT;
  152. // don't police it like this... leave higher level verification code to check it.
  153. // if(((access_flags & Constants.ACC_ABSTRACT) != 0) &&
  154. // ((access_flags & Constants.ACC_FINAL) != 0 ))
  155. // throw new ClassFormatException("Class can't be both final and abstract");
  156. classnameIndex = file.readUnsignedShort();
  157. superclassnameIndex = file.readUnsignedShort();
  158. }
  159. private final void readConstantPool() throws IOException {
  160. try {
  161. cpool = new ConstantPool(file);
  162. } catch (ClassFormatException cfe) {
  163. // add some context if we can
  164. cfe.printStackTrace();
  165. if (filename!=null) {
  166. String newmessage = "File: '"+filename+"': "+cfe.getMessage();
  167. throw new ClassFormatException(newmessage); // this loses the old stack trace but I dont think that matters!
  168. }
  169. throw cfe;
  170. }
  171. }
  172. /** Read information about the fields of the class */
  173. private final void readFields() throws IOException, ClassFormatException {
  174. int fieldCount = file.readUnsignedShort();
  175. if (fieldCount == 0) {
  176. fields = Field.NoFields;
  177. } else {
  178. fields = new Field[fieldCount];
  179. for(int i=0; i < fieldCount; i++)
  180. fields[i] = new Field(file, cpool);
  181. }
  182. }
  183. /** Check whether the header of the file is ok. Of course, this has
  184. * to be the first action on successive file reads */
  185. private final void readID() throws IOException {
  186. int magic = 0xCAFEBABE;
  187. if (file.readInt() != magic)
  188. throw new ClassFormatException(filename + " is not a Java .class file");
  189. }
  190. private static final int[] NO_INTERFACES = new int[0];
  191. /** Read information about the interfaces implemented by this class */
  192. private final void readInterfaces() throws IOException {
  193. int interfacesCount = file.readUnsignedShort();
  194. if (interfacesCount==0) {
  195. interfaceIndices = NO_INTERFACES;
  196. } else {
  197. interfaceIndices = new int[interfacesCount];
  198. for(int i=0; i < interfacesCount; i++)
  199. interfaceIndices[i] = file.readUnsignedShort();
  200. }
  201. }
  202. /** Read information about the methods of the class */
  203. private final void readMethods() throws IOException {
  204. int methodsCount = file.readUnsignedShort();
  205. if (methodsCount==0) {
  206. methods = Method.NoMethods;
  207. } else {
  208. methods = new Method[methodsCount];
  209. for(int i=0; i < methodsCount; i++)
  210. methods[i] = new Method(file, cpool);
  211. }
  212. }
  213. /** Read major and minor version of compiler which created the file */
  214. private final void readVersion() throws IOException {
  215. minor = file.readUnsignedShort();
  216. major = file.readUnsignedShort();
  217. }
  218. }