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.

Unknown.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 org.aspectj.apache.bcel.Constants;
  59. /**
  60. * This class represents a reference to an unknown (i.e., application-specific) attribute of a class. It is instantiated from the
  61. * <em>Attribute.readAttribute()</em> method. Applications that need to read in application-specific attributes should create an <a
  62. * href="./AttributeReader.html">AttributeReader</a> implementation and attach it via <a
  63. * href="./Attribute.html#addAttributeReader(java.lang.String,
  64. * org.aspectj.apache.bcel.classfile.AttributeReader)">Attribute.addAttributeReader</a>.
  65. *
  66. *
  67. * @version $Id: Unknown.java,v 1.6 2009/09/15 19:40:12 aclement Exp $
  68. * @see org.aspectj.apache.bcel.classfile.Attribute
  69. * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  70. */
  71. public final class Unknown extends Attribute {
  72. private byte[] bytes;
  73. private String name;
  74. // evil static - removed by Andy C - no apparent users (4 Mar 06)
  75. // private static HashMap unknown_attributes = new HashMap();
  76. /**
  77. * @return array of unknown attributes, but just one for each kind.
  78. */
  79. // static Unknown[] getUnknownAttributes() {
  80. // Unknown[] unknowns = new Unknown[unknown_attributes.size()];
  81. // Iterator entries = unknown_attributes.values().iterator();
  82. //
  83. // for(int i=0; entries.hasNext(); i++)
  84. // unknowns[i] = (Unknown)entries.next();
  85. //
  86. // unknown_attributes.clear();
  87. // return unknowns;
  88. // }
  89. /**
  90. * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical
  91. * copy.
  92. */
  93. public Unknown(Unknown c) {
  94. this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
  95. }
  96. /**
  97. * Create a non-standard attribute.
  98. *
  99. * @param name_index Index in constant pool
  100. * @param length Content length in bytes
  101. * @param bytes Attribute contents
  102. * @param constant_pool Array of constants
  103. */
  104. public Unknown(int name_index, int length, byte[] bytes, ConstantPool constant_pool) {
  105. super(Constants.ATTR_UNKNOWN, name_index, length, constant_pool);
  106. this.bytes = bytes;
  107. name = ((ConstantUtf8) constant_pool.getConstant(name_index, Constants.CONSTANT_Utf8)).getValue();
  108. // unknown_attributes.put(name, this);
  109. }
  110. /**
  111. * Construct object from file stream.
  112. *
  113. * @param name_index Index in constant pool
  114. * @param length Content length in bytes
  115. * @param file Input stream
  116. * @param constant_pool Array of constants
  117. * @throws IOException
  118. */
  119. Unknown(int name_index, int length, DataInputStream file, ConstantPool constant_pool) throws IOException {
  120. this(name_index, length, (byte[]) null, constant_pool);
  121. if (length > 0) {
  122. bytes = new byte[length];
  123. file.readFully(bytes);
  124. }
  125. }
  126. /**
  127. * Called by objects that are traversing the nodes of the tree implicitely defined by the contents of a Java class. I.e., the
  128. * hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  129. *
  130. * @param v Visitor object
  131. */
  132. @Override
  133. public void accept(ClassVisitor v) {
  134. v.visitUnknown(this);
  135. }
  136. /**
  137. * Dump unknown bytes to file stream.
  138. *
  139. * @param file Output file stream
  140. * @throws IOException
  141. */
  142. @Override
  143. public final void dump(DataOutputStream file) throws IOException {
  144. super.dump(file);
  145. if (length > 0)
  146. file.write(bytes, 0, length);
  147. }
  148. /**
  149. * @return data bytes.
  150. */
  151. public final byte[] getBytes() {
  152. return bytes;
  153. }
  154. /**
  155. * @return name of attribute.
  156. */
  157. @Override
  158. public String getName() {
  159. return name;
  160. }
  161. /**
  162. * @param bytes.
  163. */
  164. public final void setBytes(byte[] bytes) {
  165. this.bytes = bytes;
  166. }
  167. /**
  168. * @return String representation.
  169. */
  170. @Override
  171. public final String toString() {
  172. if (length == 0 || bytes == null)
  173. return "(Unknown attribute " + name + ")";
  174. String hex;
  175. if (length > 10) {
  176. byte[] tmp = new byte[10];
  177. System.arraycopy(bytes, 0, tmp, 0, 10);
  178. hex = Utility.toHexString(tmp) + "... (truncated)";
  179. } else
  180. hex = Utility.toHexString(bytes);
  181. return "(Unknown attribute " + name + ": " + hex + ")";
  182. }
  183. /**
  184. * @return deep copy of this attribute
  185. */
  186. // @Override
  187. // public Attribute copy(ConstantPool constant_pool) {
  188. // Unknown c = (Unknown) clone();
  189. //
  190. // if (bytes != null)
  191. // c.bytes = bytes.clone();
  192. //
  193. // c.cpool = constant_pool;
  194. // return c;
  195. // }
  196. }