KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > bytecode > EnclosingMethodAttribute


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.bytecode;
17
18 import java.io.DataInputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * <code>EnclosingMethod_attribute</code>.
24  */

25 public class EnclosingMethodAttribute extends AttributeInfo {
26     /**
27      * The name of this attribute <code>"EnclosingMethod"</code>.
28      */

29     public static final String JavaDoc tag = "EnclosingMethod";
30
31     EnclosingMethodAttribute(ConstPool cp, int n, DataInputStream JavaDoc in)
32         throws IOException JavaDoc
33     {
34         super(cp, n, in);
35     }
36
37     /**
38      * Constructs an EnclosingMethod attribute.
39      *
40      * @param cp a constant pool table.
41      * @param className the name of the innermost enclosing class.
42      * @param methodName the name of the enclosing method.
43      * @param methodDesc the descriptor of the enclosing method.
44      */

45     public EnclosingMethodAttribute(ConstPool cp, String JavaDoc className,
46                                     String JavaDoc methodName, String JavaDoc methodDesc) {
47         super(cp, tag);
48         int ci = cp.addClassInfo(className);
49         int ni = cp.addNameAndTypeInfo(methodName, methodDesc);
50         byte[] bvalue = new byte[4];
51         bvalue[0] = (byte)(ci >>> 8);
52         bvalue[1] = (byte)ci;
53         bvalue[2] = (byte)(ni >>> 8);
54         bvalue[3] = (byte)ni;
55         set(bvalue);
56     }
57
58     /**
59      * Returns the value of <code>class_index</code>.
60      */

61     public int classIndex() {
62         return ByteArray.readU16bit(get(), 0);
63     }
64
65     /**
66      * Returns the value of <code>method_index</code>.
67      */

68     public int methodIndex() {
69         return ByteArray.readU16bit(get(), 2);
70     }
71
72     /**
73      * Returns the name of the class specified by <code>class_index</code>.
74      */

75     public String JavaDoc className() {
76         return getConstPool().getClassInfo(classIndex());
77     }
78
79     /**
80      * Returns the method name specified by <code>method_index</code>.
81      */

82     public String JavaDoc methodName() {
83         ConstPool cp = getConstPool();
84         int mi = methodIndex();
85         int ni = cp.getNameAndTypeName(mi);
86         return cp.getUtf8Info(ni);
87     }
88
89     /**
90      * Returns the method descriptor specified by <code>method_index</code>.
91      */

92     public String JavaDoc methodDescriptor() {
93         ConstPool cp = getConstPool();
94         int mi = methodIndex();
95         int ti = cp.getNameAndTypeDescriptor(mi);
96         return cp.getUtf8Info(ti);
97     }
98
99     /**
100      * Makes a copy. Class names are replaced according to the
101      * given <code>Map</code> object.
102      *
103      * @param newCp the constant pool table used by the new copy.
104      * @param classnames pairs of replaced and substituted
105      * class names.
106      */

107     public AttributeInfo copy(ConstPool newCp, Map JavaDoc classnames) {
108         return new EnclosingMethodAttribute(newCp, className(),
109                                             methodName(), methodDescriptor());
110     }
111 }
112
Popular Tags