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