001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.bcel.classfile;
018
019import java.io.DataInput;
020import java.io.DataOutputStream;
021import java.io.IOException;
022
023import org.apache.bcel.Const;
024import org.apache.bcel.util.Args;
025
026/**
027 * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from
028 * the <em>Attribute.readAttribute()</em> method.
029 *
030 * @see Attribute
031 */
032public final class Deprecated extends Attribute {
033
034    private byte[] bytes;
035
036    /**
037     * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
038     * physical copy.
039     *
040     * @param c Source to copy.
041     */
042    public Deprecated(final Deprecated c) {
043        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
044    }
045
046    /**
047     * @param nameIndex Index in constant pool to CONSTANT_Utf8
048     * @param length Content length in bytes
049     * @param bytes Attribute contents
050     * @param constantPool Array of constants
051     */
052    public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
053        super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
054        this.bytes = bytes;
055    }
056
057    /**
058     * Constructs object from input stream.
059     *
060     * @param nameIndex Index in constant pool to CONSTANT_Utf8
061     * @param length Content length in bytes
062     * @param input Input stream
063     * @param constantPool Array of constants
064     * @throws IOException if an I/O error occurs.
065     */
066    Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
067        this(nameIndex, length, (byte[]) null, constantPool);
068        if (length > 0) {
069            bytes = new byte[length];
070            input.readFully(bytes);
071            println("Deprecated attribute with length > 0");
072        }
073    }
074
075    /**
076     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
077     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
078     *
079     * @param v Visitor object
080     */
081    @Override
082    public void accept(final Visitor v) {
083        v.visitDeprecated(this);
084    }
085
086    /**
087     * @return deep copy of this attribute
088     */
089    @Override
090    public Attribute copy(final ConstantPool constantPool) {
091        final Deprecated c = (Deprecated) clone();
092        if (bytes != null) {
093            c.bytes = bytes.clone();
094        }
095        c.setConstantPool(constantPool);
096        return c;
097    }
098
099    /**
100     * Dump source file attribute to file stream in binary format.
101     *
102     * @param file Output file stream
103     * @throws IOException if an I/O error occurs.
104     */
105    @Override
106    public void dump(final DataOutputStream file) throws IOException {
107        super.dump(file);
108        if (super.getLength() > 0) {
109            file.write(bytes, 0, super.getLength());
110        }
111    }
112
113    /**
114     * @return data bytes.
115     */
116    public byte[] getBytes() {
117        return bytes;
118    }
119
120    /**
121     * @param bytes the raw bytes that represents this byte array
122     */
123    public void setBytes(final byte[] bytes) {
124        this.bytes = bytes;
125    }
126
127    /**
128     * @return attribute name
129     */
130    @Override
131    public String toString() {
132        return Const.getAttributeName(Const.ATTR_DEPRECATED) + ": true";
133    }
134}