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.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.Const;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and denotes that this is a
28   * deprecated method.
29   * It is instantiated from the <em>Attribute.readAttribute()</em> method.
30   *
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.html#Deprecated">Deprecated(final 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(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
54          super(Const.ATTR_DEPRECATED, name_index, length, constant_pool);
55          this.bytes = bytes;
56      }
57  
58  
59      /**
60       * Construct object from input stream.
61       *
62       * @param name_index Index in constant pool to CONSTANT_Utf8
63       * @param length Content length in bytes
64       * @param input Input stream
65       * @param constant_pool Array of constants
66       * @throws IOException
67       */
68      Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
69              throws IOException {
70          this(name_index, length, (byte[]) null, constant_pool);
71          if (length > 0) {
72              bytes = new byte[length];
73              input.readFully(bytes);
74              println("Deprecated attribute with length > 0");
75          }
76      }
77  
78  
79      /**
80       * Called by objects that are traversing the nodes of the tree implicitely
81       * defined by the contents of a Java class. I.e., the hierarchy of methods,
82       * fields, attributes, etc. spawns a tree of objects.
83       *
84       * @param v Visitor object
85       */
86      @Override
87      public void accept( final Visitor v ) {
88          v.visitDeprecated(this);
89      }
90  
91  
92      /**
93       * Dump source file attribute to file stream in binary format.
94       *
95       * @param file Output file stream
96       * @throws IOException
97       */
98      @Override
99      public void dump( final DataOutputStream file ) throws IOException {
100         super.dump(file);
101         if (super.getLength() > 0) {
102             file.write(bytes, 0, super.getLength());
103         }
104     }
105 
106 
107     /**
108      * @return data bytes.
109      */
110     public byte[] getBytes() {
111         return bytes;
112     }
113 
114 
115     /**
116      * @param bytes the raw bytes that represents this byte array
117      */
118     public void setBytes( final byte[] bytes ) {
119         this.bytes = bytes;
120     }
121 
122 
123     /**
124      * @return attribute name
125      */
126     @Override
127     public String toString() {
128         return Const.getAttributeName(Const.ATTR_DEPRECATED);
129     }
130 
131 
132     /**
133      * @return deep copy of this attribute
134      */
135     @Override
136     public Attribute copy( final ConstantPool _constant_pool ) {
137         final Deprecated"../../../../org/apache/bcel/classfile/Deprecated.html#Deprecated">Deprecated c = (Deprecated) clone();
138         if (bytes != null) {
139             c.bytes = new byte[bytes.length];
140             System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
141         }
142         c.setConstantPool(_constant_pool);
143         return c;
144     }
145 }