Deprecated.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.classfile;

  18. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.Const;
  22. import org.apache.bcel.util.Args;

  23. /**
  24.  * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from
  25.  * the <em>Attribute.readAttribute()</em> method.
  26.  *
  27.  * @see Attribute
  28.  */
  29. public final class Deprecated extends Attribute {

  30.     private byte[] bytes;

  31.     /**
  32.      * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
  33.      * physical copy.
  34.      *
  35.      * @param c Source to copy.
  36.      */
  37.     public Deprecated(final Deprecated c) {
  38.         this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
  39.     }

  40.     /**
  41.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  42.      * @param length Content length in bytes
  43.      * @param bytes Attribute contents
  44.      * @param constantPool Array of constants
  45.      */
  46.     public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
  47.         super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
  48.         this.bytes = bytes;
  49.     }

  50.     /**
  51.      * Constructs object from input stream.
  52.      *
  53.      * @param nameIndex Index in constant pool to CONSTANT_Utf8
  54.      * @param length Content length in bytes
  55.      * @param input Input stream
  56.      * @param constantPool Array of constants
  57.      * @throws IOException if an I/O error occurs.
  58.      */
  59.     Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
  60.         this(nameIndex, length, (byte[]) null, constantPool);
  61.         if (length > 0) {
  62.             bytes = new byte[length];
  63.             input.readFully(bytes);
  64.             println("Deprecated attribute with length > 0");
  65.         }
  66.     }

  67.     /**
  68.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  69.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  70.      *
  71.      * @param v Visitor object
  72.      */
  73.     @Override
  74.     public void accept(final Visitor v) {
  75.         v.visitDeprecated(this);
  76.     }

  77.     /**
  78.      * @return deep copy of this attribute
  79.      */
  80.     @Override
  81.     public Attribute copy(final ConstantPool constantPool) {
  82.         final Deprecated c = (Deprecated) clone();
  83.         if (bytes != null) {
  84.             c.bytes = bytes.clone();
  85.         }
  86.         c.setConstantPool(constantPool);
  87.         return c;
  88.     }

  89.     /**
  90.      * Dump source file attribute to file stream in binary format.
  91.      *
  92.      * @param file Output file stream
  93.      * @throws IOException if an I/O error occurs.
  94.      */
  95.     @Override
  96.     public void dump(final DataOutputStream file) throws IOException {
  97.         super.dump(file);
  98.         if (super.getLength() > 0) {
  99.             file.write(bytes, 0, super.getLength());
  100.         }
  101.     }

  102.     /**
  103.      * @return data bytes.
  104.      */
  105.     public byte[] getBytes() {
  106.         return bytes;
  107.     }

  108.     /**
  109.      * @param bytes the raw bytes that represents this byte array
  110.      */
  111.     public void setBytes(final byte[] bytes) {
  112.         this.bytes = bytes;
  113.     }

  114.     /**
  115.      * @return attribute name
  116.      */
  117.     @Override
  118.     public String toString() {
  119.         return Const.getAttributeName(Const.ATTR_DEPRECATED) + ": true";
  120.     }
  121. }