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