    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 *
    017 */
    018package org.apache.bcel.classfile;
    019
    020import java.io.DataInput;
    021import java.io.DataOutputStream;
    022import java.io.IOException;
    023
    024import org.apache.bcel.Const;
    025
    026/**
    027 * This class is derived from <em>Attribute</em> and denotes that this is a
    028 * deprecated method.
    029 * It is instantiated from the <em>Attribute.readAttribute()</em> method.
    030 *
    031 * @version $Id$
    032 * @see     Attribute
    033 */
    034public final class Deprecated extends Attribute {
    035
    036    private byte[] bytes;
    037
    038
    039    /**
    040     * Initialize from another object. Note that both objects use the same
    041     * references (shallow copy). Use clone() for a physical copy.
    042     */
    043    public Deprecated(final Deprecated c) {
    044        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
    045    }
    046
    047
    048    /**
    049     * @param name_index Index in constant pool to CONSTANT_Utf8
    050     * @param length Content length in bytes
    051     * @param bytes Attribute contents
    052     * @param constant_pool Array of constants
    053     */
    054    public Deprecated(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
    055        super(Const.ATTR_DEPRECATED, name_index, length, constant_pool);
    056        this.bytes = bytes;
    057    }
    058
    059
    060    /**
    061     * Construct object from input stream.
    062     *
    063     * @param name_index Index in constant pool to CONSTANT_Utf8
    064     * @param length Content length in bytes
    065     * @param input Input stream
    066     * @param constant_pool Array of constants
    067     * @throws IOException
    068     */
    069    Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
    070            throws IOException {
    071        this(name_index, length, (byte[]) null, constant_pool);
    072        if (length > 0) {
    073            bytes = new byte[length];
    074            input.readFully(bytes);
    075            System.err.println("Deprecated attribute with length > 0");
    076        }
    077    }
    078
    079
    080    /**
    081     * Called by objects that are traversing the nodes of the tree implicitely
    082     * defined by the contents of a Java class. I.e., 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    /**
    094     * Dump source file attribute to file stream in binary format.
    095     *
    096     * @param file Output file stream
    097     * @throws IOException
    098     */
    099    @Override
    100    public final void dump( final DataOutputStream file ) throws IOException {
    101        super.dump(file);
    102        if (super.getLength() > 0) {
    103            file.write(bytes, 0, super.getLength());
    104        }
    105    }
    106
    107
    108    /**
    109     * @return data bytes.
    110     */
    111    public final byte[] getBytes() {
    112        return bytes;
    113    }
    114
    115
    116    /**
    117     * @param bytes the raw bytes that represents this byte array
    118     */
    119    public final void setBytes( final byte[] bytes ) {
    120        this.bytes = bytes;
    121    }
    122
    123
    124    /**
    125     * @return attribute name
    126     */
    127    @Override
    128    public final String toString() {
    129        return Const.getAttributeName(Const.ATTR_DEPRECATED);
    130    }
    131
    132
    133    /**
    134     * @return deep copy of this attribute
    135     */
    136    @Override
    137    public Attribute copy( final ConstantPool _constant_pool ) {
    138        final Deprecated c = (Deprecated) clone();
    139        if (bytes != null) {
    140            c.bytes = new byte[bytes.length];
    141            System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
    142        }
    143        c.setConstantPool(_constant_pool);
    144        return c;
    145    }
    146}