~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to classfile/src/org/netbeans/modules/classfile/EnclosingMethod.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * EnclosingMethod.java
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
5
 *
 
6
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
7
 *
 
8
 * The contents of this file are subject to the terms of either the GNU
 
9
 * General Public License Version 2 only ("GPL") or the Common
 
10
 * Development and Distribution License("CDDL") (collectively, the
 
11
 * "License"). You may not use this file except in compliance with the
 
12
 * License. You can obtain a copy of the License at
 
13
 * http://www.netbeans.org/cddl-gplv2.html
 
14
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
15
 * specific language governing permissions and limitations under the
 
16
 * License.  When distributing the software, include this License Header
 
17
 * Notice in each file and include the License file at
 
18
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
19
 * particular file as subject to the "Classpath" exception as provided
 
20
 * by Sun in the GPL Version 2 section of the License file that
 
21
 * accompanied this code. If applicable, add the following below the
 
22
 * License Header, with the fields enclosed by brackets [] replaced by
 
23
 * your own identifying information:
 
24
 * "Portions Copyrighted [year] [name of copyright owner]"
 
25
 *
 
26
 * Contributor(s):
 
27
 *
 
28
 * The Original Software is NetBeans. The Initial Developer of the Original
 
29
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
30
 * Microsystems, Inc. All Rights Reserved.
 
31
 *
 
32
 * If you wish your version of this file to be governed by only the CDDL
 
33
 * or only the GPL Version 2, indicate your decision by adding
 
34
 * "[Contributor] elects to include this software in this distribution
 
35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
36
 * single choice of license, a recipient has the option to distribute
 
37
 * your version of this file under either the CDDL, the GPL Version 2 or
 
38
 * to extend the choice of license to its licensees as provided above.
 
39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
40
 * Version 2 license, then the option applies only if the new code is
 
41
 * made subject to such option by the copyright holder.
 
42
 *
 
43
 * Contributor(s): Thomas Ball
 
44
 *
 
45
 * Version: $Revision: 1.4 $
 
46
 */
 
47
 
 
48
package org.netbeans.modules.classfile;
 
49
 
 
50
/**
 
51
 * A class representing the enclosing method of an inner class.  An
 
52
 * enclosing method is similar to a CPMethodInfo type, but differs
 
53
 * in two respects.  First, the classfile stores this information in
 
54
 * an "EnclosingMethod" attribute, rather than in the constant pool
 
55
 * Second, an enclosing method attribute may not actually have a
 
56
 * method reference (only a class reference).  This is because the
 
57
 * inner class is defined in an init block instead of an actual
 
58
 * method.  
 
59
 *
 
60
 * @see org.netbeans.modules.classfile.ClassFile#getEnclosingMethod
 
61
 * @author Thomas Ball
 
62
 */
 
63
public final class EnclosingMethod {
 
64
    final CPClassInfo classInfo;
 
65
    final CPNameAndTypeInfo methodInfo;
 
66
 
 
67
    EnclosingMethod(ConstantPool pool, CPClassInfo classInfo, int iMethod) {
 
68
        this.classInfo = classInfo;
 
69
        methodInfo = iMethod > 0 ? (CPNameAndTypeInfo)pool.get(iMethod) : null;
 
70
    }
 
71
 
 
72
    public ClassName getClassName() {
 
73
        return classInfo.getClassName();
 
74
    }
 
75
 
 
76
    /**
 
77
     * Returns the constant pool entry for the enclosing class.
 
78
     */
 
79
    public CPClassInfo getClassInfo() {
 
80
        return classInfo;
 
81
    }
 
82
 
 
83
    /**
 
84
     * Returns whether the enclosing method attribute describes a method
 
85
     * the inner class is defined within.  If false, then the inner
 
86
     * class was defined in an init block (or statement) in the class,
 
87
     * outside of any method or constructor bodies.
 
88
     */
 
89
    public boolean hasMethod() {
 
90
        return methodInfo != null;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Returns the constant pool entry for the enclosing method, or
 
95
     * null if the inner class was defined outside of any method or
 
96
     * constructor bodies.
 
97
     *
 
98
     * Note: a CPNameAndTypeInfo instance is returned because the method
 
99
     * is external to the enclosed class.  Do not attempt to cast it to a
 
100
     * CPMethodInfo type, which is an internal method structure.
 
101
     */
 
102
    public CPNameAndTypeInfo getMethodInfo() {
 
103
        return methodInfo;
 
104
    }
 
105
 
 
106
    public String toString() {
 
107
        String methodString = methodInfo != null 
 
108
            ? methodInfo.toString() : "<no method>";
 
109
        return "enclosing method: class=" + getClassName() + //NOI18N
 
110
            ", method=" + methodString;   //NOI18N
 
111
    }
 
112
}