001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.bcel.classfile;
021
022import java.io.DataInput;
023import java.io.IOException;
024
025import org.apache.bcel.Const;
026import org.apache.bcel.util.Args;
027import org.apache.commons.lang3.ArrayUtils;
028
029/**
030 * 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>
031 * method.
032 *
033 * <pre>
034 * Deprecated_attribute {
035 *     u2 attribute_name_index;
036 *     u4 attribute_length;
037 * }
038 * </pre>
039 *
040 * @see Attribute
041 * @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>
042 */
043public final class Deprecated extends Attribute {
044
045    /**
046     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a physical copy.
047     *
048     * @param c Source to copy.
049     */
050    public Deprecated(final Deprecated c) {
051        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
052    }
053
054    /**
055     * Constructs a Deprecated attribute.
056     *
057     * @param nameIndex    Index in constant pool to CONSTANT_Utf8.
058     * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
059     * @param bytes        Attribute contents.
060     * @param constantPool Array of constants.
061     * @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>
062     */
063    public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
064        super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
065    }
066
067    /**
068     * Constructs object from input stream.
069     *
070     * @param nameIndex    Index in constant pool to CONSTANT_Utf8.
071     * @param length       JVM Specification: "The value of the attribute_length item must be zero.".
072     * @param input        Input stream.
073     * @param constantPool Array of constants.
074     * @throws IOException if an I/O error occurs.
075     * @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>
076     */
077    Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
078        this(nameIndex, length, (byte[]) null, constantPool);
079    }
080
081    /**
082     * 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,
083     * fields, attributes, etc. spawns a tree of objects.
084     *
085     * @param v Visitor object.
086     */
087    @Override
088    public void accept(final Visitor v) {
089        v.visitDeprecated(this);
090    }
091
092    /**
093     * @return deep copy of this attribute.
094     */
095    @Override
096    public Attribute copy(final ConstantPool constantPool) {
097        final Deprecated c = (Deprecated) clone();
098        c.setConstantPool(constantPool);
099        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}