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