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 */
019package org.apache.bcel.classfile;
020
021import java.io.DataInput;
022import java.io.DataOutputStream;
023import java.io.IOException;
024
025import org.apache.bcel.Const;
026import org.apache.bcel.util.Args;
027
028/**
029 * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from
030 * the <em>Attribute.readAttribute()</em> method.
031 *
032 * @see Attribute
033 */
034public final class Deprecated extends Attribute {
035
036    private byte[] bytes;
037
038    /**
039     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
040     * physical copy.
041     *
042     * @param c Source to copy.
043     */
044    public Deprecated(final Deprecated c) {
045        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
046    }
047
048    /**
049     * @param nameIndex Index in constant pool to CONSTANT_Utf8
050     * @param length Content length in bytes
051     * @param bytes Attribute contents
052     * @param constantPool Array of constants
053     */
054    public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
055        super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
056        this.bytes = bytes;
057    }
058
059    /**
060     * Constructs object from input stream.
061     *
062     * @param nameIndex Index in constant pool to CONSTANT_Utf8
063     * @param length Content length in bytes
064     * @param input Input stream
065     * @param constantPool Array of constants
066     * @throws IOException if an I/O error occurs.
067     */
068    Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
069        this(nameIndex, length, (byte[]) null, constantPool);
070        if (length > 0) {
071            bytes = new byte[length];
072            input.readFully(bytes);
073            println("Deprecated attribute with length > 0");
074        }
075    }
076
077    /**
078     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
079     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
080     *
081     * @param v Visitor object
082     */
083    @Override
084    public void accept(final Visitor v) {
085        v.visitDeprecated(this);
086    }
087
088    /**
089     * @return deep copy of this attribute
090     */
091    @Override
092    public Attribute copy(final ConstantPool constantPool) {
093        final Deprecated c = (Deprecated) clone();
094        if (bytes != null) {
095            c.bytes = bytes.clone();
096        }
097        c.setConstantPool(constantPool);
098        return c;
099    }
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}