View Javadoc
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  
19  import java.io.DataInput;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  import org.apache.bcel.Const;
24  import org.apache.bcel.util.Args;
25  
26  /**
27   * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from
28   * the <em>Attribute.readAttribute()</em> method.
29   *
30   * @see Attribute
31   */
32  public final class Deprecated extends Attribute {
33  
34      private byte[] bytes;
35  
36      /**
37       * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a
38       * physical copy.
39       *
40       * @param c Source to copy.
41       */
42      public Deprecated(final Deprecated c) {
43          this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool());
44      }
45  
46      /**
47       * @param nameIndex Index in constant pool to CONSTANT_Utf8
48       * @param length Content length in bytes
49       * @param bytes Attribute contents
50       * @param constantPool Array of constants
51       */
52      public Deprecated(final int nameIndex, final int length, final byte[] bytes, final ConstantPool constantPool) {
53          super(Const.ATTR_DEPRECATED, nameIndex, Args.require0(length, "Deprecated attribute length"), constantPool);
54          this.bytes = bytes;
55      }
56  
57      /**
58       * Constructs object from input stream.
59       *
60       * @param nameIndex Index in constant pool to CONSTANT_Utf8
61       * @param length Content length in bytes
62       * @param input Input stream
63       * @param constantPool Array of constants
64       * @throws IOException if an I/O error occurs.
65       */
66      Deprecated(final int nameIndex, final int length, final DataInput input, final ConstantPool constantPool) throws IOException {
67          this(nameIndex, length, (byte[]) null, constantPool);
68          if (length > 0) {
69              bytes = new byte[length];
70              input.readFully(bytes);
71              println("Deprecated attribute with length > 0");
72          }
73      }
74  
75      /**
76       * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
77       * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
78       *
79       * @param v Visitor object
80       */
81      @Override
82      public void accept(final Visitor v) {
83          v.visitDeprecated(this);
84      }
85  
86      /**
87       * @return deep copy of this attribute
88       */
89      @Override
90      public Attribute copy(final ConstantPool constantPool) {
91          final Deprecated c = (Deprecated) clone();
92          if (bytes != null) {
93              c.bytes = bytes.clone();
94          }
95          c.setConstantPool(constantPool);
96          return c;
97      }
98  
99      /**
100      * Dump source file attribute to file stream in binary format.
101      *
102      * @param file Output file stream
103      * @throws IOException if an I/O error occurs.
104      */
105     @Override
106     public void dump(final DataOutputStream file) throws IOException {
107         super.dump(file);
108         if (super.getLength() > 0) {
109             file.write(bytes, 0, super.getLength());
110         }
111     }
112 
113     /**
114      * @return data bytes.
115      */
116     public byte[] getBytes() {
117         return bytes;
118     }
119 
120     /**
121      * @param bytes the raw bytes that represents this byte array
122      */
123     public void setBytes(final byte[] bytes) {
124         this.bytes = bytes;
125     }
126 
127     /**
128      * @return attribute name
129      */
130     @Override
131     public String toString() {
132         return Const.getAttributeName(Const.ATTR_DEPRECATED) + ": true";
133     }
134 }