KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > attrs > SourceDebugExtensionAttribute


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
31 package oracle.toplink.libraries.asm.attrs;
32
33 import oracle.toplink.libraries.asm.Attribute;
34 import oracle.toplink.libraries.asm.ByteVector;
35 import oracle.toplink.libraries.asm.ClassReader;
36 import oracle.toplink.libraries.asm.ClassWriter;
37 import oracle.toplink.libraries.asm.Label;
38
39 /**
40  * The SourceDebugExtension attribute is an optional attribute defined in JSR-045
41  * in the attributes table of the ClassFile structure. There can be no more than one
42  * SourceDebugExtension attribute in the attributes table of a given ClassFile
43  * structure. The SourceDebugExtension attribute has the following format:
44  * <pre>
45  * SourceDebugExtension_attribute {
46  * u2 attribute_name_index;
47  * u4 attribute_length;
48  * u1 debug_extension[attribute_length];
49  * }
50  * </pre>
51  * The items of the SourceDebugExtension_attribute structure are as follows:
52  * <dl>
53  * <dt>attribute_name_index</dt>
54  * <dd>The value of the attribute_name_index item must be a valid index into the
55  * constant_pool table. The constant_pool entry at that index must be a
56  * CONSTANT_Utf8_info structure representing the string "SourceDebugExtension".</dd>
57  * <dt>attribute_length</dt>
58  * <dd>The value of the attribute_length item indicates the length of
59  * the attribute, excluding the initial six bytes. The value of the
60  * attribute_length item is thus the number of bytes in the debug_extension[]
61  * item.</dd>
62  * <dt>debug_extension[]</dt>
63  * <dd>The debug_extension array holds a string, which must be in UTF-8 format.
64  * There is no terminating zero byte. The string in the debug_extension item
65  * will be interpreted as extended debugging information. The content of this
66  * string has no semantic effect on the Java Virtual Machine.</dd>
67  * </dl>
68  *
69  * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=45">JSR-045: Debugging
70  * Support for Other Languages</a>
71  *
72  * @author Eugene Kuleshov
73  */

74
75 public class SourceDebugExtensionAttribute extends Attribute {
76
77   public String JavaDoc debugExtension;
78
79   public SourceDebugExtensionAttribute () {
80     super("SourceDebugExtension");
81   }
82
83   public SourceDebugExtensionAttribute (String JavaDoc debugExtension) {
84     this();
85     this.debugExtension = debugExtension;
86   }
87
88   protected Attribute read (ClassReader cr, int off,
89                             int len, char[] buf, int codeOff, Label[] labels) {
90     return new SourceDebugExtensionAttribute(readUTF8(cr, off, len));
91   }
92
93   protected ByteVector write (ClassWriter cw, byte[] code,
94                               int len, int maxStack, int maxLocals) {
95     byte[] b = putUTF8(debugExtension);
96     return new ByteVector().putByteArray(b, 0, b.length);
97   }
98
99   private String JavaDoc readUTF8 (ClassReader cr, int index, int utfLen) {
100     int endIndex = index + utfLen;
101     byte[] b = cr.b;
102     char[] buf = new char[utfLen];
103     int strLen = 0;
104     int c, d, e;
105     while (index < endIndex) {
106       c = b[index++] & 0xFF;
107       switch (c >> 4) {
108         case 0:
109         case 1:
110         case 2:
111         case 3:
112         case 4:
113         case 5:
114         case 6:
115         case 7:
116           // 0xxxxxxx
117
buf[strLen++] = (char)c;
118           break;
119
120         case 12:
121         case 13:
122           // 110x xxxx 10xx xxxx
123
d = b[index++];
124           buf[strLen++] = (char)(((c & 0x1F) << 6) | (d & 0x3F));
125           break;
126
127         default:
128           // 1110 xxxx 10xx xxxx 10xx xxxx
129
d = b[index++];
130           e = b[index++];
131           buf[strLen++] = (char)(((c & 0x0F) << 12) | ((d & 0x3F) << 6) | (e & 0x3F));
132           break;
133       }
134     }
135
136     return new String JavaDoc(buf, 0, strLen);
137   }
138
139   private byte[] putUTF8 (String JavaDoc s) {
140     int charLength = s.length();
141     int byteLength = 0;
142     for (int i = 0; i < charLength; ++i) {
143       char c = s.charAt(i);
144       if (c >= '\001' && c <= '\177') {
145         byteLength++;
146       } else if (c > '\u07FF') {
147         byteLength += 3;
148       } else {
149         byteLength += 2;
150       }
151     }
152     /*if (byteLength > 65535) {
153       throw new IllegalArgumentException();
154     }*/

155     byte[] data = new byte[byteLength];
156     for (int i = 0; i < charLength;) {
157       char c = s.charAt(i);
158       if (c >= '\001' && c <= '\177') {
159         data[i++] = (byte)c;
160       } else if (c > '\u07FF') {
161         data[i++] = (byte)(0xE0 | c >> 12 & 0xF);
162         data[i++] = (byte)(0x80 | c >> 6 & 0x3F);
163         data[i++] = (byte)(0x80 | c & 0x3F);
164       } else {
165         data[i++] = (byte)(0xC0 | c >> 6 & 0x1F);
166         data[i++] = (byte)(0x80 | c & 0x3F);
167       }
168     }
169     return data;
170   }
171
172   public String JavaDoc toString () {
173     return debugExtension;
174   }
175 }
176
Popular Tags