1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 
  25 import  com.sun.org.apache.bcel.internal.Constants;
  26 import  java.io.*;
  27 
  28 /**
  29  * This class is derived from <em>Attribute</em> and denotes that this is a
  30  * deprecated method.
  31  * It is instantiated from the <em>Attribute.readAttribute()</em> method.
  32  *
  33  * @author  <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A>
  34  * @see     Attribute
  35  */
  36 public final class Deprecated extends Attribute {
  37   private byte[] bytes;
  38 
  39   /**
  40    * Initialize from another object. Note that both objects use the same
  41    * references (shallow copy). Use clone() for a physical copy.
  42    */
  43   public Deprecated(Deprecated c) {
  44     this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
  45   }
  46 
  47   /**
  48    * @param name_index Index in constant pool to CONSTANT_Utf8
  49    * @param length Content length in bytes
  50    * @param bytes Attribute contents
  51    * @param constant_pool Array of constants
  52    */
  53   public Deprecated(int name_index, int length, byte[] bytes,
  54                     ConstantPool constant_pool)
  55   {
  56     super(Constants.ATTR_DEPRECATED, name_index, length, constant_pool);
  57     this.bytes = bytes;
  58   }
  59 
  60   /**
  61    * Construct object from file stream.
  62    * @param name_index Index in constant pool to CONSTANT_Utf8
  63    * @param length Content length in bytes
  64    * @param file Input stream
  65    * @param constant_pool Array of constants
  66    * @throws IOException
  67    */
  68   Deprecated(int name_index, int length, DataInputStream file,
  69              ConstantPool constant_pool) throws IOException
  70   {
  71     this(name_index, length, (byte [])null, constant_pool);
  72 
  73     if(length > 0) {
  74       bytes = new byte[length];
  75       file.readFully(bytes);
  76       System.err.println("Deprecated attribute with length > 0");
  77     }
  78   }
  79 
  80   /**
  81    * Called by objects that are traversing the nodes of the tree implicitely
  82    * defined by the contents of a Java class. I.e., the hierarchy of methods,
  83    * fields, attributes, etc. spawns a tree of objects.
  84    *
  85    * @param v Visitor object
  86    */
  87   public void accept(Visitor v) {
  88     v.visitDeprecated(this);
  89   }
  90 
  91   /**
  92    * Dump source file attribute to file stream in binary format.
  93    *
  94    * @param file Output file stream
  95    * @throws IOException
  96    */
  97   public final void dump(DataOutputStream file) throws IOException
  98   {
  99     super.dump(file);
 100 
 101     if(length > 0)
 102       file.write(bytes, 0, length);
 103   }
 104 
 105   /**
 106    * @return data bytes.
 107    */
 108   public final byte[] getBytes() { return bytes; }
 109 
 110   /**
 111    * @param bytes.
 112    */
 113   public final void setBytes(byte[] bytes) {
 114     this.bytes = bytes;
 115   }
 116 
 117   /**
 118    * @return attribute name
 119    */
 120   public final String toString() {
 121     return Constants.ATTRIBUTE_NAMES[Constants.ATTR_DEPRECATED];
 122   }
 123 
 124   /**
 125    * @return deep copy of this attribute
 126    */
 127   public Attribute copy(ConstantPool constant_pool) {
 128     Deprecated c = (Deprecated)clone();
 129 
 130     if(bytes != null)
 131       c.bytes = (byte[])bytes.clone();
 132 
 133     c.constant_pool = constant_pool;
 134     return c;
 135   }
 136 }