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.

Attribute.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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.DataInputStream;
  56. import java.io.DataOutputStream;
  57. import java.io.IOException;
  58. import java.io.Serializable;
  59. import org.aspectj.apache.bcel.Constants;
  60. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisAnnos;
  61. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisParamAnnos;
  62. import org.aspectj.apache.bcel.classfile.annotation.RuntimeInvisTypeAnnos;
  63. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisAnnos;
  64. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisParamAnnos;
  65. import org.aspectj.apache.bcel.classfile.annotation.RuntimeVisTypeAnnos;
  66. /**
  67. * Abstract super class for <em>Attribute</em> objects. Currently the <em>ConstantValue</em>, <em>SourceFile</em>, <em>Code</em>,
  68. * <em>Exceptiontable</em>, <em>LineNumberTable</em>, <em>LocalVariableTable</em>, <em>InnerClasses</em> and <em>Synthetic</em>
  69. * attributes are supported. The <em>Unknown</em> attribute stands for non-standard-attributes.
  70. *
  71. * @version $Id: Attribute.java,v 1.9 2009/12/09 18:01:31 aclement Exp $
  72. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  73. * @see ConstantValue
  74. * @see SourceFile
  75. * @see Code
  76. * @see Unknown
  77. * @see ExceptionTable
  78. * @see LineNumberTable
  79. * @see LocalVariableTable
  80. * @see InnerClasses
  81. * @see Synthetic
  82. * @see Deprecated
  83. * @see Signature
  84. */
  85. public abstract class Attribute implements Cloneable, Node, Serializable {
  86. public final static Attribute[] NoAttributes = new Attribute[0];
  87. protected byte tag; // Tag to distinguish subclasses
  88. protected int nameIndex;
  89. protected int length;
  90. protected ConstantPool cpool;
  91. protected Attribute(byte tag, int nameIndex, int length, ConstantPool cpool) {
  92. this.tag = tag;
  93. this.nameIndex = nameIndex;
  94. this.length = length;
  95. this.cpool = cpool;
  96. }
  97. public void dump(DataOutputStream file) throws IOException {
  98. file.writeShort(nameIndex);
  99. file.writeInt(length);
  100. }
  101. // OPTIMIZE how about just reading them in and storing them until we need to decode what they really are?
  102. public static final Attribute readAttribute(DataInputStream file, ConstantPool cpool) throws IOException {
  103. byte tag = Constants.ATTR_UNKNOWN;
  104. int idx = file.readUnsignedShort();
  105. String name = cpool.getConstantUtf8(idx).getValue();
  106. int len = file.readInt();
  107. // Compare strings to find known attribute
  108. for (byte i = 0; i < Constants.KNOWN_ATTRIBUTES; i++) {
  109. if (name.equals(Constants.ATTRIBUTE_NAMES[i])) {
  110. tag = i;
  111. break;
  112. }
  113. }
  114. switch (tag) {
  115. case Constants.ATTR_UNKNOWN:
  116. return new Unknown(idx, len, file, cpool);
  117. case Constants.ATTR_CONSTANT_VALUE:
  118. return new ConstantValue(idx, len, file, cpool);
  119. case Constants.ATTR_SOURCE_FILE:
  120. return new SourceFile(idx, len, file, cpool);
  121. case Constants.ATTR_CODE:
  122. return new Code(idx, len, file, cpool);
  123. case Constants.ATTR_EXCEPTIONS:
  124. return new ExceptionTable(idx, len, file, cpool);
  125. case Constants.ATTR_LINE_NUMBER_TABLE:
  126. return new LineNumberTable(idx, len, file, cpool);
  127. case Constants.ATTR_LOCAL_VARIABLE_TABLE:
  128. return new LocalVariableTable(idx, len, file, cpool);
  129. case Constants.ATTR_INNER_CLASSES:
  130. return new InnerClasses(idx, len, file, cpool);
  131. case Constants.ATTR_SYNTHETIC:
  132. return new Synthetic(idx, len, file, cpool);
  133. case Constants.ATTR_DEPRECATED:
  134. return new Deprecated(idx, len, file, cpool);
  135. case Constants.ATTR_SIGNATURE:
  136. return new Signature(idx, len, file, cpool);
  137. case Constants.ATTR_STACK_MAP:
  138. return new StackMap(idx, len, file, cpool);
  139. case Constants.ATTR_RUNTIME_VISIBLE_ANNOTATIONS:
  140. return new RuntimeVisAnnos(idx, len, file, cpool);
  141. case Constants.ATTR_RUNTIME_INVISIBLE_ANNOTATIONS:
  142. return new RuntimeInvisAnnos(idx, len, file, cpool);
  143. case Constants.ATTR_RUNTIME_VISIBLE_PARAMETER_ANNOTATIONS:
  144. return new RuntimeVisParamAnnos(idx, len, file, cpool);
  145. case Constants.ATTR_RUNTIME_INVISIBLE_PARAMETER_ANNOTATIONS:
  146. return new RuntimeInvisParamAnnos(idx, len, file, cpool);
  147. case Constants.ATTR_ANNOTATION_DEFAULT:
  148. return new AnnotationDefault(idx, len, file, cpool);
  149. case Constants.ATTR_LOCAL_VARIABLE_TYPE_TABLE:
  150. return new LocalVariableTypeTable(idx, len, file, cpool);
  151. case Constants.ATTR_ENCLOSING_METHOD:
  152. return new EnclosingMethod(idx, len, file, cpool);
  153. case Constants.ATTR_BOOTSTRAPMETHODS:
  154. return new BootstrapMethods(idx,len,file,cpool);
  155. case Constants.ATTR_RUNTIME_VISIBLE_TYPE_ANNOTATIONS:
  156. return new RuntimeVisTypeAnnos(idx, len, file, cpool);
  157. case Constants.ATTR_RUNTIME_INVISIBLE_TYPE_ANNOTATIONS:
  158. return new RuntimeInvisTypeAnnos(idx, len, file, cpool);
  159. case Constants.ATTR_METHOD_PARAMETERS:
  160. return new MethodParameters(idx, len, file, cpool);
  161. case Constants.ATTR_MODULE:
  162. return new Module(idx, len, file, cpool);
  163. case Constants.ATTR_MODULE_PACKAGES:
  164. return new ModulePackages(idx, len, file, cpool);
  165. case Constants.ATTR_MODULE_MAIN_CLASS:
  166. return new ModuleMainClass(idx, len, file, cpool);
  167. case Constants.ATTR_NEST_HOST:
  168. return new NestHost(idx, len, file, cpool);
  169. case Constants.ATTR_NEST_MEMBERS:
  170. return new NestMembers(idx, len, file, cpool);
  171. default:
  172. throw new IllegalStateException();
  173. }
  174. }
  175. public String getName() {
  176. return cpool.getConstantUtf8(nameIndex).getValue();
  177. }
  178. public final int getLength() {
  179. return length;
  180. }
  181. public final int getNameIndex() {
  182. return nameIndex;
  183. }
  184. public final byte getTag() {
  185. return tag;
  186. }
  187. public final ConstantPool getConstantPool() {
  188. return cpool;
  189. }
  190. @Override
  191. public String toString() {
  192. return Constants.ATTRIBUTE_NAMES[tag];
  193. }
  194. @Override
  195. public abstract void accept(ClassVisitor v);
  196. }