View Javadoc
1   /*
2    * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /*
27   * This file is available under and governed by the GNU General Public
28   * License version 2 only, as published by the Free Software Foundation.
29   * However, the following notice accompanied the original version of this
30   * file:
31   *
32   * ASM: a very small and fast Java bytecode manipulation framework
33   * Copyright (c) 2000-2007 INRIA, France Telecom
34   * All rights reserved.
35   *
36   * Redistribution and use in source and binary forms, with or without
37   * modification, are permitted provided that the following conditions
38   * are met:
39   * 1. Redistributions of source code must retain the above copyright
40   *    notice, this list of conditions and the following disclaimer.
41   * 2. Redistributions in binary form must reproduce the above copyright
42   *    notice, this list of conditions and the following disclaimer in the
43   *    documentation and/or other materials provided with the distribution.
44   * 3. Neither the name of the copyright holders nor the names of its
45   *    contributors may be used to endorse or promote products derived from
46   *    this software without specific prior written permission.
47   *
48   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
49   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
52   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58   * THE POSSIBILITY OF SUCH DAMAGE.
59   */
60  package com.sun.xml.internal.ws.org.objectweb.asm;
61  
62  /**
63   * A non standard class, field, method or code attribute.
64   *
65   * @author Eric Bruneton
66   * @author Eugene Kuleshov
67   */
68  public class Attribute {
69  
70      /**
71       * The type of this attribute.
72       */
73      public final String type;
74  
75      /**
76       * The raw value of this attribute, used only for unknown attributes.
77       */
78      byte[] value;
79  
80      /**
81       * The next attribute in this attribute list. May be <tt>null</tt>.
82       */
83      Attribute next;
84  
85      /**
86       * Constructs a new empty attribute.
87       *
88       * @param type 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 <i>new</i>
125      * {@link Attribute} object, of type {@link #type type}, corresponding to
126      * the <tt>len</tt> bytes starting at the given offset, in the given class
127      * reader.
128      *
129      * @param cr the class that contains the attribute to be read.
130      * @param off index of the first byte of the attribute's content in {@link
131      *        ClassReader#b cr.b}. The 6 attribute header bytes, containing the
132      *        type and the length of the attribute, are not taken into account
133      *        here.
134      * @param len the length of the attribute's content.
135      * @param buf buffer to be used to call
136      *        {@link ClassReader#readUTF8 readUTF8},
137      *        {@link ClassReader#readClass(int,char[]) readClass} or
138      *        {@link ClassReader#readConst readConst}.
139      * @param codeOff index of the first byte of code's attribute content in
140      *        {@link ClassReader#b cr.b}, or -1 if the attribute to be read is
141      *        not a code attribute. The 6 attribute header bytes, containing the
142      *        type and the length of the attribute, are not taken into account
143      *        here.
144      * @param labels the labels of the method's code, or <tt>null</tt> if the
145      *        attribute to be read is not a code attribute.
146      * @return a <i>new</i> {@link Attribute} object corresponding to the given
147      *         bytes.
148      */
149     protected Attribute read(
150         final ClassReader cr,
151         final int off,
152         final int len,
153         final char[] buf,
154         final int codeOff,
155         final Label[] labels)
156     {
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 the class to which this attribute must be added. This parameter
167      *        can be used to add to the constant pool of this class the items
168      *        that corresponds to this attribute.
169      * @param code the bytecode of the method corresponding to this code
170      *        attribute, or <tt>null</tt> if this attribute is not a code
171      *        attributes.
172      * @param len the length of the bytecode of the method corresponding to this
173      *        code attribute, or <tt>null</tt> if this attribute is not a code
174      *        attribute.
175      * @param maxStack the maximum stack size of the method corresponding to
176      *        this code attribute, or -1 if this attribute is not a code
177      *        attribute.
178      * @param maxLocals the maximum number of local variables of the method
179      *        corresponding to this code attribute, or -1 if this attribute is
180      *        not a code attribute.
181      * @return the byte array form of this attribute.
182      */
183     protected ByteVector write(
184         final ClassWriter cw,
185         final byte[] code,
186         final int len,
187         final int maxStack,
188         final int maxLocals)
189     {
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 the class writer to be used to convert the attributes into byte
215      *        arrays, with the {@link #write write} method.
216      * @param code the bytecode of the method corresponding to these code
217      *        attributes, or <tt>null</tt> if these attributes are not code
218      *        attributes.
219      * @param len the length of the bytecode of the method corresponding to
220      *        these code attributes, or <tt>null</tt> if these attributes are
221      *        not code attributes.
222      * @param maxStack the maximum stack size of the method corresponding to
223      *        these code attributes, or -1 if these attributes are not code
224      *        attributes.
225      * @param maxLocals the maximum number of local variables of the method
226      *        corresponding to these code attributes, or -1 if these attributes
227      *        are not code attributes.
228      * @return the size of all the attributes in this attribute list. This size
229      *         includes the size of the attribute headers.
230      */
231     final int getSize(
232         final ClassWriter cw,
233         final byte[] code,
234         final int len,
235         final int maxStack,
236         final int maxLocals)
237     {
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 the class writer to be used to convert the attributes into byte
253      *        arrays, with the {@link #write write} method.
254      * @param code the bytecode of the method corresponding to these code
255      *        attributes, or <tt>null</tt> if these attributes are not code
256      *        attributes.
257      * @param len the length of the bytecode of the method corresponding to
258      *        these code attributes, or <tt>null</tt> if these attributes are
259      *        not code attributes.
260      * @param maxStack the maximum stack size of the method corresponding to
261      *        these code attributes, or -1 if these attributes are not code
262      *        attributes.
263      * @param maxLocals the maximum number of local variables of the method
264      *        corresponding to these code attributes, or -1 if these attributes
265      *        are not code attributes.
266      * @param out where the attributes must be written.
267      */
268     final void put(
269         final ClassWriter cw,
270         final byte[] code,
271         final int len,
272         final int maxStack,
273         final int maxLocals,
274         final ByteVector out)
275     {
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 }