View Javadoc

1   /*
2    * Copyright  2000-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License"); 
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License. 
15   *
16   */
17  package org.apache.bcel.classfile;
18  
19  import java.io.DataInputStream;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import org.apache.bcel.Constants;
23  
24  /***
25   * This class is derived from <em>Attribute</em> and denotes that this is a
26   * deprecated method.
27   * It is instantiated from the <em>Attribute.readAttribute()</em> method.
28   *
29   * @version $Id: Deprecated.java 386056 2006-03-15 11:31:56Z tcurdt $
30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31   * @see     Attribute
32   */
33  public final class Deprecated extends Attribute {
34  
35      private byte[] bytes;
36  
37  
38      /***
39       * Initialize from another object. Note that both objects use the same
40       * references (shallow copy). Use clone() for a physical copy.
41       */
42      public Deprecated(Deprecated c) {
43          this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
44      }
45  
46  
47      /***
48       * @param name_index Index in constant pool to CONSTANT_Utf8
49       * @param length Content length in bytes
50       * @param bytes Attribute contents
51       * @param constant_pool Array of constants
52       */
53      public Deprecated(int name_index, int length, byte[] bytes, ConstantPool constant_pool) {
54          super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool);
55          this.bytes = bytes;
56      }
57  
58  
59      /***
60       * Construct object from file stream.
61       * @param name_index Index in constant pool to CONSTANT_Utf8
62       * @param length Content length in bytes
63       * @param file Input stream
64       * @param constant_pool Array of constants
65       * @throws IOException
66       */
67      Deprecated(int name_index, int length, DataInputStream file, ConstantPool constant_pool)
68              throws IOException {
69          this(name_index, length, (byte[]) null, constant_pool);
70          if (length > 0) {
71              bytes = new byte[length];
72              file.readFully(bytes);
73              System.err.println("Deprecated attribute with length > 0");
74          }
75      }
76  
77  
78      /***
79       * Called by objects that are traversing the nodes of the tree implicitely
80       * defined by the contents of a Java class. I.e., the hierarchy of methods,
81       * fields, attributes, etc. spawns a tree of objects.
82       *
83       * @param v Visitor object
84       */
85      public void accept( Visitor v ) {
86          v.visitDeprecated(this);
87      }
88  
89  
90      /***
91       * Dump source file attribute to file stream in binary format.
92       *
93       * @param file Output file stream
94       * @throws IOException
95       */
96      public final void dump( DataOutputStream file ) throws IOException {
97          super.dump(file);
98          if (length > 0) {
99              file.write(bytes, 0, length);
100         }
101     }
102 
103 
104     /***
105      * @return data bytes.
106      */
107     public final byte[] getBytes() {
108         return bytes;
109     }
110 
111 
112     /***
113      * @param bytes the raw bytes that represents this byte array
114      */
115     public final void setBytes( byte[] bytes ) {
116         this.bytes = bytes;
117     }
118 
119 
120     /***
121      * @return attribute name
122      */
123     public final String toString() {
124         return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED];
125     }
126 
127 
128     /***
129      * @return deep copy of this attribute
130      */
131     public Attribute copy( ConstantPool _constant_pool ) {
132         Deprecated c = (Deprecated) clone();
133         if (bytes != null) {
134             c.bytes = new byte[bytes.length];
135             System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
136         }
137         c.constant_pool = _constant_pool;
138         return c;
139     }
140 }