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 */ 018package org.apache.bcel.classfile; 019 020import java.io.DataInput; 021import java.io.DataOutputStream; 022import java.io.IOException; 023 024import org.apache.bcel.Const; 025 026/** 027 * This class is derived from <em>Attribute</em> and denotes that this is a deprecated method. It is instantiated from 028 * the <em>Attribute.readAttribute()</em> method. 029 * 030 * @see Attribute 031 */ 032public final class Deprecated extends Attribute { 033 034 private byte[] bytes; 035 036 /** 037 * Initialize from another object. Note that both objects use the same references (shallow copy). Use clone() for a 038 * physical copy. 039 */ 040 public Deprecated(final Deprecated c) { 041 this(c.getNameIndex(), c.getLength(), c.getBytes(), c.getConstantPool()); 042 } 043 044 /** 045 * @param name_index Index in constant pool to CONSTANT_Utf8 046 * @param length Content length in bytes 047 * @param bytes Attribute contents 048 * @param constant_pool Array of constants 049 */ 050 public Deprecated(final int name_index, final int length, final byte[] bytes, final ConstantPool constant_pool) { 051 super(Const.ATTR_DEPRECATED, name_index, length, constant_pool); 052 this.bytes = bytes; 053 } 054 055 /** 056 * Construct object from input stream. 057 * 058 * @param name_index Index in constant pool to CONSTANT_Utf8 059 * @param length Content length in bytes 060 * @param input Input stream 061 * @param constant_pool Array of constants 062 * @throws IOException if an I/O error occurs. 063 */ 064 Deprecated(final int name_index, final int length, final DataInput input, final ConstantPool constant_pool) throws IOException { 065 this(name_index, length, (byte[]) null, constant_pool); 066 if (length > 0) { 067 bytes = new byte[length]; 068 input.readFully(bytes); 069 println("Deprecated attribute with length > 0"); 070 } 071 } 072 073 /** 074 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 075 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 076 * 077 * @param v Visitor object 078 */ 079 @Override 080 public void accept(final Visitor v) { 081 v.visitDeprecated(this); 082 } 083 084 /** 085 * @return deep copy of this attribute 086 */ 087 @Override 088 public Attribute copy(final ConstantPool constantPool) { 089 final Deprecated c = (Deprecated) clone(); 090 if (bytes != null) { 091 c.bytes = new byte[bytes.length]; 092 System.arraycopy(bytes, 0, c.bytes, 0, bytes.length); 093 } 094 c.setConstantPool(constantPool); 095 return c; 096 } 097 098 /** 099 * Dump source file attribute to file stream in binary format. 100 * 101 * @param file Output file stream 102 * @throws IOException if an I/O error occurs. 103 */ 104 @Override 105 public void dump(final DataOutputStream file) throws IOException { 106 super.dump(file); 107 if (super.getLength() > 0) { 108 file.write(bytes, 0, super.getLength()); 109 } 110 } 111 112 /** 113 * @return data bytes. 114 */ 115 public byte[] getBytes() { 116 return bytes; 117 } 118 119 /** 120 * @param bytes the raw bytes that represents this byte array 121 */ 122 public void setBytes(final byte[] bytes) { 123 this.bytes = bytes; 124 } 125 126 /** 127 * @return attribute name 128 */ 129 @Override 130 public String toString() { 131 return Const.getAttributeName(Const.ATTR_DEPRECATED) + ": true"; 132 } 133}