KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > asm > Attribute


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * Contact: Eric.Bruneton@rd.francetelecom.com
31  *
32  * Author: Eric Bruneton
33  */

34
35 package org.logicalcobwebs.asm;
36
37 /**
38  * A non standard class, field, method or code attribute.
39  */

40
41 public class Attribute {
42
43   /**
44    * The type of this attribute.
45    */

46
47   public String JavaDoc type;
48
49   /**
50    * The byte array that contains the value of this attribute. <i>The content of
51    * this array must not be modified, although the array itself can be changed
52    * (i.e. attr.b[...] = ...; is forbidden, but attr.b = ...; is allowed)</i>.
53    */

54
55   public byte[] b;
56
57   /**
58    * Index of the first byte of this attribute in {@link #b b}.
59    */

60
61   public int off;
62
63   /**
64    * Length of this attributes, in bytes.
65    */

66
67   public int len;
68
69   /**
70    * The next attribute in this attribute list. May be <tt>null</tt>.
71    */

72
73   public Attribute next;
74
75   /**
76    * Constructs a new {@link Attribute}.
77    *
78    * @param type the type of the attribute.
79    * @param b byte array that contains the value of the attribute.
80    * @param off index of the first byte of the attribute in <tt>b</tt>.
81    * @param len length of the attributes, in bytes.
82    */

83
84   public Attribute (
85     final String JavaDoc type,
86     final byte[] b,
87     final int off,
88     final int len)
89   {
90     this.type = type;
91     this.b = b;
92     this.off = off;
93     this.len = len;
94   }
95
96   /**
97    * Constructs a new empty attribute.
98    *
99    * @param type the type of the attribute.
100    */

101
102   public Attribute (final String JavaDoc type) {
103     this(type, null, 0, 0);
104   }
105
106   /**
107    * Returns the length of the attribute list that begins with this attribute.
108    *
109    * @return the length of the attribute list that begins with this attribute.
110    */

111
112   final int getCount () {
113     int count = 0;
114     Attribute attr = this;
115     while (attr != null) {
116       count += 1;
117       attr = attr.next;
118     }
119     return count;
120   }
121
122   /**
123    * Returns the size of all the attributes in this attribute list.
124    *
125    * @param cw the class writer to be used to convert the attributes into byte
126    * arrays, with the {@link ClassWriter#writeAttribute writeAttribute}
127    * method.
128    * @return the size of all the attributes in this attribute list. This size
129    * includes the size of the attribute headers.
130    */

131
132   final int getSize (final ClassWriter cw) {
133     int size = 0;
134     Attribute attr = this;
135     while (attr != null) {
136       cw.newUTF8(attr.type);
137       size += cw.writeAttribute(attr).length + 6;
138       attr = attr.next;
139     }
140     return size;
141   }
142
143   /**
144    * Writes all the attributes of this attribute list in the given byte vector.
145    *
146    * @param cw the class writer to be used to convert the attributes into byte
147    * arrays, with the {@link ClassWriter#writeAttribute writeAttribute}
148    * method.
149    * @param out where the attributes must be written.
150    */

151
152   final void put (final ClassWriter cw, final ByteVector out) {
153     Attribute attr = this;
154     while (attr != null) {
155       byte[] b = cw.writeAttribute(attr);
156       out.put2(cw.newUTF8(attr.type)).put4(b.length);
157       out.putByteArray(b, 0, b.length);
158       attr = attr.next;
159     }
160   }
161 }
162
Popular Tags