Source code

    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 * @see     Attribute
    032 */
    033public final class Deprecated extends Attribute {
    034
    035    private byte[] bytes;
    036
    037
    038    /**
    039     * Initialize from another object. Note that both objects use the same
    040     * references (shallow copy). Use clone() for a physical copy.
    041     */
    042    public Deprecated(final Deprecated c) {
    043        this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
    044    }
    045
    046
    047    /**
    048     * @param name_index Index in constant pool to CONSTANT_Utf8
    049     * @param length Content length in bytes
    050     * @param bytes Attribute contents
    051     * @param constant_pool Array of constants
    052     */
    053    public Deprecated(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) {
    054        super(Const.ATTR_DEPRECATED, name_index, length, constant_pool);
    055        this.bytes = bytes;
    056    }
    057
    058
    059    /**
    060     * Construct object from input stream.
    061     *
    062     * @param name_index Index in constant pool to CONSTANT_Utf8
    063     * @param length Content length in bytes
    064     * @param input Input stream
    065     * @param constant_pool Array of constants
    066     * @throws IOException
    067     */
    068    Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)
    069            throws IOException {
    070        this(name_index, length, (byte[]) null, constant_pool);
    071        if (length > 0) {
    072            bytes = new byte[length];
    073            input.readFully(bytes);
    074            println("Deprecated attribute with length > 0");
    075        }
    076    }
    077
    078
    079    /**
    080     * Called by objects that are traversing the nodes of the tree implicitely
    081     * defined by the contents of a Java class. I.e., the hierarchy of methods,
    082     * fields, attributes, etc. spawns a tree of objects.
    083     *
    084     * @param v Visitor object
    085     */
    086    @Override
    087    public void accept( final Visitor v ) {
    088        v.visitDeprecated(this);
    089    }
    090
    091
    092    /**
    093     * Dump source file attribute to file stream in binary format.
    094     *
    095     * @param file Output file stream
    096     * @throws IOException
    097     */
    098    @Override
    099    public void dump( final DataOutputStream file ) throws IOException {
    100        super.dump(file);
    101        if (super.getLength() > 0) {
    102            file.write(bytes, 0, super.getLength());
    103        }
    104    }
    105
    106
    107    /**
    108     * @return data bytes.
    109     */
    110    public byte[] getBytes() {
    111        return bytes;
    112    }
    113
    114
    115    /**
    116     * @param bytes the raw bytes that represents this byte array
    117     */
    118    public void setBytes( final byte[] bytes ) {
    119        this.bytes = bytes;
    120    }
    121
    122
    123    /**
    124     * @return attribute name
    125     */
    126    @Override
    127    public String toString() {
    128        return Const.getAttributeName(Const.ATTR_DEPRECATED);
    129    }
    130
    131
    132    /**
    133     * @return deep copy of this attribute
    134     */
    135    @Override
    136    public Attribute copy( final ConstantPool _constant_pool ) {
    137        final Deprecated c = (Deprecated) clone();
    138        if (bytes != null) {
    139            c.bytes = new byte[bytes.length];
    140            System.arraycopy(bytes, 0, c.bytes, 0, bytes.length);
    141        }
    142        c.setConstantPool(_constant_pool);
    143        return c;
    144    }
    145}