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     */
018    package org.apache.bcel.classfile;
019    
020    import java.io.DataInputStream;
021    import java.io.DataOutputStream;
022    import java.io.IOException;
023    import org.apache.bcel.Constants;
024    
025    /**
026     * This class is derived from <em>Attribute</em> and denotes that this is a
027     * deprecated method.
028     * It is instantiated from the <em>Attribute.readAttribute()</em> method.
029     *
030     * @version $Id: Deprecated.java 1152072 2011-07-29 01:54:05Z dbrosius $
031     * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
032     * @see     Attribute
033     */
034    public final class Deprecated extends Attribute {
035    
036        private static final long serialVersionUID = -2242528405240201000L;
037        private byte[] bytes;
038    
039    
040        /**
041         * Initialize from another object. Note that both objects use the same
042         * references (shallow copy). Use clone() for a physical copy.
043         */
044        public Deprecated(Deprecated c) {
045            this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
046        }
047    
048    
049        /**
050         * @param name_index Index in constant pool to CONSTANT_Utf8
051         * @param length Content length in bytes
052         * @param bytes Attribute contents
053         * @param constant_pool Array of constants
054         */
055        public Deprecated(int name_index, int length, byte[] bytes, ConstantPool constant_pool) {
056            super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool);
057            this.bytes = bytes;
058        }
059    
060    
061        /**
062         * Construct object from file stream.
063         * @param name_index Index in constant pool to CONSTANT_Utf8
064         * @param length Content length in bytes
065         * @param file Input stream
066         * @param constant_pool Array of constants
067         * @throws IOException
068         */
069        Deprecated(int name_index, int length, DataInputStream file, 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                file.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( 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( DataOutputStream file ) throws IOException {
101            super.dump(file);
102            if (length > 0) {
103                file.write(bytes, 0, length);
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( byte[] bytes ) {
120            this.bytes = bytes;
121        }
122    
123    
124        /**
125         * @return attribute name
126         */
127        @Override
128        public final String toString() {
129            return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED];
130        }
131    
132    
133        /**
134         * @return deep copy of this attribute
135         */
136        @Override
137        public Attribute copy( ConstantPool _constant_pool ) {
138            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.constant_pool = _constant_pool;
144            return c;
145        }
146    }