1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * ASM: a very small and fast Java bytecode manipulation framework
  32  * Copyright (c) 2000-2011 INRIA, France Telecom
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  * 1. Redistributions of source code must retain the above copyright
  39  *    notice, this list of conditions and the following disclaimer.
  40  * 2. Redistributions in binary form must reproduce the above copyright
  41  *    notice, this list of conditions and the following disclaimer in the
  42  *    documentation and/or other materials provided with the distribution.
  43  * 3. Neither the name of the copyright holders nor the names of its
  44  *    contributors may be used to endorse or promote products derived from
  45  *    this software without specific prior written permission.
  46  *
  47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  57  * THE POSSIBILITY OF SUCH DAMAGE.
  58  */
  59 package jdk.internal.org.objectweb.asm;
  60 
  61 /**
  62  * A non standard class, field, method or code attribute.
  63  *
  64  * @author Eric Bruneton
  65  * @author Eugene Kuleshov
  66  */
  67 public class Attribute {
  68 
  69     /**
  70      * The type of this attribute.
  71      */
  72     public final String type;
  73 
  74     /**
  75      * The raw value of this attribute, used only for unknown attributes.
  76      */
  77     byte[] value;
  78 
  79     /**
  80      * The next attribute in this attribute list. May be <tt>null</tt>.
  81      */
  82     Attribute next;
  83 
  84     /**
  85      * Constructs a new empty attribute.
  86      *
  87      * @param type
  88      *            the type of the attribute.
  89      */
  90     protected Attribute(final String type) {
  91         this.type = type;
  92     }
  93 
  94     /**
  95      * Returns <tt>true</tt> if this type of attribute is unknown. The default
  96      * implementation of this method always returns <tt>true</tt>.
  97      *
  98      * @return <tt>true</tt> if this type of attribute is unknown.
  99      */
 100     public boolean isUnknown() {
 101         return true;
 102     }
 103 
 104     /**
 105      * Returns <tt>true</tt> if this type of attribute is a code attribute.
 106      *
 107      * @return <tt>true</tt> if this type of attribute is a code attribute.
 108      */
 109     public boolean isCodeAttribute() {
 110         return false;
 111     }
 112 
 113     /**
 114      * Returns the labels corresponding to this attribute.
 115      *
 116      * @return the labels corresponding to this attribute, or <tt>null</tt> if
 117      *         this attribute is not a code attribute that contains labels.
 118      */
 119     protected Label[] getLabels() {
 120         return null;
 121     }
 122 
 123     /**
 124      * Reads a {@link #type type} attribute. This method must return a
 125      * <i>new</i> {@link Attribute} object, of type {@link #type type},
 126      * corresponding to the <tt>len</tt> bytes starting at the given offset, in
 127      * the given class reader.
 128      *
 129      * @param cr
 130      *            the class that contains the attribute to be read.
 131      * @param off
 132      *            index of the first byte of the attribute's content in
 133      *            {@link ClassReader#b cr.b}. The 6 attribute header bytes,
 134      *            containing the type and the length of the attribute, are not
 135      *            taken into account here.
 136      * @param len
 137      *            the length of the attribute's content.
 138      * @param buf
 139      *            buffer to be used to call {@link ClassReader#readUTF8
 140      *            readUTF8}, {@link ClassReader#readClass(int,char[]) readClass}
 141      *            or {@link ClassReader#readConst readConst}.
 142      * @param codeOff
 143      *            index of the first byte of code's attribute content in
 144      *            {@link ClassReader#b cr.b}, or -1 if the attribute to be read
 145      *            is not a code attribute. The 6 attribute header bytes,
 146      *            containing the type and the length of the attribute, are not
 147      *            taken into account here.
 148      * @param labels
 149      *            the labels of the method's code, or <tt>null</tt> if the
 150      *            attribute to be read is not a code attribute.
 151      * @return a <i>new</i> {@link Attribute} object corresponding to the given
 152      *         bytes.
 153      */
 154     protected Attribute read(final ClassReader cr, final int off,
 155             final int len, final char[] buf, final int codeOff,
 156             final Label[] labels) {
 157         Attribute attr = new Attribute(type);
 158         attr.value = new byte[len];
 159         System.arraycopy(cr.b, off, attr.value, 0, len);
 160         return attr;
 161     }
 162 
 163     /**
 164      * Returns the byte array form of this attribute.
 165      *
 166      * @param cw
 167      *            the class to which this attribute must be added. This
 168      *            parameter can be used to add to the constant pool of this
 169      *            class the items that corresponds to this attribute.
 170      * @param code
 171      *            the bytecode of the method corresponding to this code
 172      *            attribute, or <tt>null</tt> if this attribute is not a code
 173      *            attributes.
 174      * @param len
 175      *            the length of the bytecode of the method corresponding to this
 176      *            code attribute, or <tt>null</tt> if this attribute is not a
 177      *            code attribute.
 178      * @param maxStack
 179      *            the maximum stack size of the method corresponding to this
 180      *            code attribute, or -1 if this attribute is not a code
 181      *            attribute.
 182      * @param maxLocals
 183      *            the maximum number of local variables of the method
 184      *            corresponding to this code attribute, or -1 if this attribute
 185      *            is not a code attribute.
 186      * @return the byte array form of this attribute.
 187      */
 188     protected ByteVector write(final ClassWriter cw, final byte[] code,
 189             final int len, final int maxStack, final int maxLocals) {
 190         ByteVector v = new ByteVector();
 191         v.data = value;
 192         v.length = value.length;
 193         return v;
 194     }
 195 
 196     /**
 197      * Returns the length of the attribute list that begins with this attribute.
 198      *
 199      * @return the length of the attribute list that begins with this attribute.
 200      */
 201     final int getCount() {
 202         int count = 0;
 203         Attribute attr = this;
 204         while (attr != null) {
 205             count += 1;
 206             attr = attr.next;
 207         }
 208         return count;
 209     }
 210 
 211     /**
 212      * Returns the size of all the attributes in this attribute list.
 213      *
 214      * @param cw
 215      *            the class writer to be used to convert the attributes into
 216      *            byte arrays, with the {@link #write write} method.
 217      * @param code
 218      *            the bytecode of the method corresponding to these code
 219      *            attributes, or <tt>null</tt> if these attributes are not code
 220      *            attributes.
 221      * @param len
 222      *            the length of the bytecode of the method corresponding to
 223      *            these code attributes, or <tt>null</tt> if these attributes
 224      *            are not code attributes.
 225      * @param maxStack
 226      *            the maximum stack size of the method corresponding to these
 227      *            code attributes, or -1 if these attributes are not code
 228      *            attributes.
 229      * @param maxLocals
 230      *            the maximum number of local variables of the method
 231      *            corresponding to these code attributes, or -1 if these
 232      *            attributes are not code attributes.
 233      * @return the size of all the attributes in this attribute list. This size
 234      *         includes the size of the attribute headers.
 235      */
 236     final int getSize(final ClassWriter cw, final byte[] code, final int len,
 237             final int maxStack, final int maxLocals) {
 238         Attribute attr = this;
 239         int size = 0;
 240         while (attr != null) {
 241             cw.newUTF8(attr.type);
 242             size += attr.write(cw, code, len, maxStack, maxLocals).length + 6;
 243             attr = attr.next;
 244         }
 245         return size;
 246     }
 247 
 248     /**
 249      * Writes all the attributes of this attribute list in the given byte
 250      * vector.
 251      *
 252      * @param cw
 253      *            the class writer to be used to convert the attributes into
 254      *            byte arrays, with the {@link #write write} method.
 255      * @param code
 256      *            the bytecode of the method corresponding to these code
 257      *            attributes, or <tt>null</tt> if these attributes are not code
 258      *            attributes.
 259      * @param len
 260      *            the length of the bytecode of the method corresponding to
 261      *            these code attributes, or <tt>null</tt> if these attributes
 262      *            are not code attributes.
 263      * @param maxStack
 264      *            the maximum stack size of the method corresponding to these
 265      *            code attributes, or -1 if these attributes are not code
 266      *            attributes.
 267      * @param maxLocals
 268      *            the maximum number of local variables of the method
 269      *            corresponding to these code attributes, or -1 if these
 270      *            attributes are not code attributes.
 271      * @param out
 272      *            where the attributes must be written.
 273      */
 274     final void put(final ClassWriter cw, final byte[] code, final int len,
 275             final int maxStack, final int maxLocals, final ByteVector out) {
 276         Attribute attr = this;
 277         while (attr != null) {
 278             ByteVector b = attr.write(cw, code, len, maxStack, maxLocals);
 279             out.putShort(cw.newUTF8(attr.type)).putInt(b.length);
 280             out.putByteArray(b.data, 0, b.length);
 281             attr = attr.next;
 282         }
 283     }
 284 }