KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
41  * The EnclosingMethod attribute is an optional fixed-length attribute in
42  * the attributes table of the ClassFile structure. A class must have an
43  * EnclosingMethod attribute if and only if it is a local class or an
44  * anonymous class. A class may have no more than one EnclosingMethod attribute.
45  *
46  * The EnclosingMethod attribute has the following format:
47  * <pre>
48  * EnclosingMethod_attribute {
49  * u2 attribute_name_index;
50  * u4 attribute_length;
51  * u2 class_index
52  * u2 method_index;
53  * }
54  * </pre>
55  * The items of the EnclosingMethod_attribute structure are as follows:
56  * <dl>
57  * <dt>attribute_name_index</dt>
58  * <dd>The value of the attribute_name_index item must be a valid index
59  * into the constant_pool table. The constant_pool entry at that index
60  * must be a CONSTANT_Utf8_info structure representing the
61  * string "EnclosingMethod".</dd>
62  * <dt>attribute_length</dt>
63  * <dd>The value of the attribute_length item is four.</dd>
64  * <dt>class_index</dt>
65  * <dd>The value of the class_index item must be a valid index into the
66  * constant_pool table. The constant_pool entry at that index must be a
67  * CONSTANT_Class_info structure representing the
68  * innermost class that encloses the declaration of the current class.</dd>
69  * <dt>method_index</dt>
70  * <dd>If the current class is not immediately enclosed by a method or
71  * constructor, then the value of the method_index item must be zero.
72  * Otherwise, the value of the method_index item must be a valid
73  * index into the constant_pool table. The constant_pool entry at that
74  * index must be a CONSTANT_NameAndType_info structure
75  * representing a the name and type of a method in the class
76  * referenced by the class_index attribute above. It is the
77  * responsibility of the Java compiler to ensure that the method
78  * identified via the method_index is indeed the closest lexically
79  * enclosing method of the class that contains this EnclosingMethod
80  * attribute.</dd>
81  * </dl>
82  *
83  * @author Eugene Kuleshov
84  */

85
86 public class EnclosingMethodAttribute extends Attribute {
87   
88   public String JavaDoc owner;
89   public String JavaDoc name;
90   public String JavaDoc desc;
91
92   public EnclosingMethodAttribute () {
93     super("EnclosingMethod");
94   }
95   
96   public EnclosingMethodAttribute (String JavaDoc owner, String JavaDoc name, String JavaDoc desc) {
97     this();
98     this.owner = owner;
99     this.name = name;
100     this.desc = desc;
101   }
102
103   protected Attribute read (ClassReader cr, int off,
104                             int len, char[] buf, int codeOff, Label[] labels) {
105     // CONSTANT_Class_info
106
String JavaDoc o = cr.readClass( off, buf);
107     // CONSTANT_NameAndType_info (skip CONSTANT_NameAndType tag)
108
int index = cr.getItem(cr.readUnsignedShort(off + 2));
109     String JavaDoc n = null;
110     String JavaDoc d = null;
111     if( index!=0) {
112         n = cr.readUTF8(index, buf);
113         d = cr.readUTF8(index + 2, buf);
114     }
115     return new EnclosingMethodAttribute( o, n, d);
116   }
117
118   protected ByteVector write (ClassWriter cw, byte[] code,
119                               int len, int maxStack, int maxLocals) {
120     return new ByteVector().putShort(cw.newClass(owner))
121       .putShort( name==null || desc==null ? 0 : cw.newNameType(name, desc));
122   }
123
124   public String JavaDoc toString () {
125     return new StringBuffer JavaDoc("owner:").append( owner)
126       .append(" name:").append(name)
127       .append(" desc:").append(desc).toString();
128   }
129 }
130
131
Popular Tags