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 */ 18 package org.apache.bcel.classfile; 19 20 import java.io.DataInput; 21 import java.io.DataOutputStream; 22 import java.io.IOException; 23 24 import org.apache.bcel.Const; 25 26 /** 27 * This class is derived from <em>Attribute</em> and denotes that this is a 28 * deprecated method. 29 * It is instantiated from the <em>Attribute.readAttribute()</em> method. 30 * 31 * @version $Id$ 32 * @see Attribute 33 */ 34 public final class Deprecated extends Attribute { 35 36 private byte[] bytes; 37 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 */ Deprecated(final Deprecated c)43 public Deprecated(final Deprecated c) { 44 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 45 } 46 47 48 /** 49 * @param name_index Index in constant pool to CONSTANT_Utf8 50 * @param length Content length in bytes 51 * @param bytes Attribute contents 52 * @param constant_pool Array of constants 53 */ Deprecated(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool)54 public Deprecated(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) { 55 super(Const.ATTR_DEPRECATED, name_index, length, constant_pool); 56 this.bytes = bytes; 57 } 58 59 60 /** 61 * Construct object from input stream. 62 * 63 * @param name_index Index in constant pool to CONSTANT_Utf8 64 * @param length Content length in bytes 65 * @param input Input stream 66 * @param constant_pool Array of constants 67 * @throws IOException 68 */ Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool)69 Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) 70 throws IOException { 71 this(name_index, length, (byte[]) null, constant_pool); 72 if (length > 0) { 73 bytes = new byte[length]; 74 input.readFully(bytes); 75 System.err.println("Deprecated attribute with length > 0"); 76 } 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 @Override accept( final Visitor v )88 public void accept( final Visitor v ) { 89 v.visitDeprecated(this); 90 } 91 92 93 /** 94 * Dump source file attribute to file stream in binary format. 95 * 96 * @param file Output file stream 97 * @throws IOException 98 */ 99 @Override dump( final DataOutputStream file )100 public final void dump( final DataOutputStream file ) throws IOException { 101 super.dump(file); 102 if (super.getLength() > 0) { 103 file.write(bytes, 0, super.getLength()); 104 } 105 } 106 107 108 /** 109 * @return data bytes. 110 */ getBytes()111 public final byte[] getBytes() { 112 return bytes; 113 } 114 115 116 /** 117 * @param bytes the raw bytes that represents this byte array 118 */ setBytes( final byte[] bytes )119 public final void setBytes( final byte[] bytes ) { 120 this.bytes = bytes; 121 } 122 123 124 /** 125 * @return attribute name 126 */ 127 @Override toString()128 public final String toString() { 129 return Const.getAttributeName(Const.ATTR_DEPRECATED); 130 } 131 132 133 /** 134 * @return deep copy of this attribute 135 */ 136 @Override copy( final ConstantPool _constant_pool )137 public Attribute copy( final ConstantPool _constant_pool ) { 138 final 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.setConstantPool(_constant_pool); 144 return c; 145 } 146 } 147