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  
20  package org.apache.bcel.classfile;
21  
22  import java.io.DataInput;
23  import java.io.IOException;
24  
25  import org.apache.bcel.Const;
26  import org.apache.bcel.util.Args;
27  import org.apache.commons.lang3.ArrayUtils;
28  
29  /**
30   * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from the <em>Attribute.readAttribute()</em>
31   * method.
32   *
33   * <pre>
34   * Deprecated_attribute {
35   *     u2 attribute_name_index;
36   *     u4 attribute_length;
37   * }
38   * </pre>
39   *
40   * @see Attribute
41   * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.15">JVM Specification: The Deprecated Attribute</a>
42   */
43  public final class Deprecated extends Attribute {
44  
45      /**
46       * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical copy.
47       *
48       * @param c Source to copy.
49       */
50      public Deprecated(final Deprecated c) {
51          this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
52      }
53  
54      /**
55       * Constructs a Deprecated attribute.
56       *
57       * @param nameIndex    Index in constant pool to CONSTANT_Utf8.
58       * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
59       * @param bytes        Attribute contents.
60       * @param constantPool Array of constants.
61       * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.15">JVM Specification: The Deprecated Attribute</a>
62       */
63      public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
64          super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
65      }
66  
67      /**
68       * Constructs object from input stream.
69       *
70       * @param nameIndex    Index in constant pool to CONSTANT_Utf8.
71       * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
72       * @param input        Input stream.
73       * @param constantPool Array of constants.
74       * @throws IOException if an I/O error occurs.
75       * @see <a href="https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7.15">JVM Specification: The Deprecated Attribute</a>
76       */
77      Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
78          this(nameIndex, length, (byte[]) null, constantPool);
79      }
80  
81      /**
82       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. That is, 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(final Visitor v) {
89          v.visitDeprecated(this);
90      }
91  
92      /**
93       * @return deep copy of this attribute.
94       */
95      @Override
96      public Attribute copy(final ConstantPool constantPool) {
97          final Deprecated c = (Deprecated) clone();
98          c.setConstantPool(constantPool);
99          return c;
100     }
101 
102     /**
103      * Gets the data bytes.
104      *
105      * @return data bytes.
106      */
107     public byte[] getBytes() {
108         return ArrayUtils.EMPTY_BYTE_ARRAY;
109     }
110 
111     /**
112      * Sets the data bytes.
113      *
114      * @param bytes the raw bytes that represents this byte array.
115      */
116     public void setBytes(final byte[] bytes) {
117         if (bytes != null) {
118             Args.require0(bytes.length, "Deprecated attribute length");
119         }
120     }
121 
122     /**
123      * @return attribute name.
124      */
125     @Override
126     public String toString() {
127         return Const.getAttributeName(Const.ATTR_DEPRECATED) + ": true";
128     }
129 }