KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > classfile > EnclosingMethod


1 /*
2  * EnclosingMethod.java
3  *
4  * The contents of this file are subject to the terms of the Common Development
5  * and Distribution License (the License). You may not use this file except in
6  * compliance with the License.
7  *
8  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
9  * or http://www.netbeans.org/cddl.txt.
10  *
11  * When distributing Covered Code, include this CDDL Header Notice in each file
12  * and include the License file at http://www.netbeans.org/cddl.txt.
13  * If applicable, add the following below the CDDL Header, with the fields
14  * enclosed by brackets [] replaced by your own identifying information:
15  * "Portions Copyrighted [year] [name of copyright owner]"
16  *
17  * The Original Software is NetBeans. The Initial Developer of the Original
18  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
19  * Microsystems, Inc. All Rights Reserved.
20  *
21  * Contributor(s): Thomas Ball
22  *
23  * Version: $Revision: 1.3 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 /**
29  * A class representing the enclosing method of an inner class. An
30  * enclosing method is similar to a CPMethodInfo type, but differs
31  * in two respects. First, the classfile stores this information in
32  * an "EnclosingMethod" attribute, rather than in the constant pool
33  * Second, an enclosing method attribute may not actually have a
34  * method reference (only a class reference). This is because the
35  * inner class is defined in an init block instead of an actual
36  * method.
37  *
38  * @see org.netbeans.modules.classfile.ClassFile#getEnclosingMethod
39  * @author Thomas Ball
40  */

41 public final class EnclosingMethod {
42     final CPClassInfo classInfo;
43     final CPNameAndTypeInfo methodInfo;
44
45     EnclosingMethod(ConstantPool pool, CPClassInfo classInfo, int iMethod) {
46     this.classInfo = classInfo;
47     methodInfo = iMethod > 0 ? (CPNameAndTypeInfo)pool.get(iMethod) : null;
48     }
49
50     public ClassName getClassName() {
51         return classInfo.getClassName();
52     }
53
54     /**
55      * Returns the constant pool entry for the enclosing class.
56      */

57     public CPClassInfo getClassInfo() {
58     return classInfo;
59     }
60
61     /**
62      * Returns whether the enclosing method attribute describes a method
63      * the inner class is defined within. If false, then the inner
64      * class was defined in an init block (or statement) in the class,
65      * outside of any method or constructor bodies.
66      */

67     public boolean hasMethod() {
68     return methodInfo != null;
69     }
70
71     /**
72      * Returns the constant pool entry for the enclosing method, or
73      * null if the inner class was defined outside of any method or
74      * constructor bodies.
75      *
76      * Note: a CPNameAndTypeInfo instance is returned because the method
77      * is external to the enclosed class. Do not attempt to cast it to a
78      * CPMethodInfo type, which is an internal method structure.
79      */

80     public CPNameAndTypeInfo getMethodInfo() {
81     return methodInfo;
82     }
83
84     public String JavaDoc toString() {
85     String JavaDoc methodString = methodInfo != null
86         ? methodInfo.toString() : "<no method>";
87         return "enclosing method: class=" + getClassName() + //NOI18N
88
", method=" + methodString; //NOI18N
89
}
90 }
91
Popular Tags