KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CPFieldMethodInfo.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.4 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 /**
29  * The base class for field, method, and interface method constant pool types.
30  *
31  * @author Thomas Ball
32  */

33 abstract class CPFieldMethodInfo extends CPNameAndTypeInfo {
34     int iClass;
35     int iNameAndType;
36
37     CPFieldMethodInfo(ConstantPool pool,int iClass,int iNameAndType) {
38         super(pool);
39         this.iClass = iClass;
40         this.iNameAndType = iNameAndType;
41     }
42
43     public final int getClassID() {
44         return iClass;
45     }
46
47     public final int getFieldID() {
48         return iNameAndType;
49     }
50
51     public final ClassName getClassName() {
52         return ClassName.getClassName(
53             ((CPName)pool.cpEntries[iClass]).getName());
54     }
55
56     void setClassNameIndex(int index) {
57     iClass = index;
58     }
59
60     public final String JavaDoc getFieldName() {
61     return ((CPNameAndTypeInfo)pool.cpEntries[iNameAndType]).getName();
62     }
63
64     public String JavaDoc toString() {
65         return getClass().getName() + ": class=" + getClassName() + //NOI18N
66
", name=" + getName() + ", descriptor=" + getDescriptor(); //NOI18N
67
}
68     
69     public final String JavaDoc getSignature() {
70         return getSignature(getDescriptor(), true);
71     }
72     
73     static String JavaDoc getSignature(String JavaDoc s, boolean fullName) {
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         int arrays = 0;
76         int i = 0;
77         while (i < s.length()) {
78             char ch = s.charAt(i++);
79             switch (ch) {
80                 case 'B': sb.append("byte"); continue; //NOI18N
81
case 'C': sb.append("char"); continue; //NOI18N
82
case 'D': sb.append("double"); continue; //NOI18N
83
case 'F': sb.append("float"); continue; //NOI18N
84
case 'I': sb.append("int"); continue; //NOI18N
85
case 'J': sb.append("long"); continue; //NOI18N
86
case 'S': sb.append("short"); continue; //NOI18N
87
case 'Z': sb.append("boolean"); continue; //NOI18N
88
case 'V': sb.append("void"); continue; //NOI18N
89

90                 case 'L':
91                     int l = s.indexOf(';');
92                     String JavaDoc cls = s.substring(1, l).replace('/', '.');
93                     if (!fullName) {
94                         int idx = cls.lastIndexOf('.');
95                         if (idx >= 0)
96                             cls = cls.substring(idx+1);
97                     }
98                     sb.append(cls);
99                     i = l + 1;
100                     continue;
101                 
102                 case '[':
103                     arrays++;
104                     continue;
105                 default:
106                     break; // invalid character
107
}
108         }
109         while (arrays-- > 0)
110             sb.append("[]");
111         return sb.toString();
112     }
113     
114     void resolve(CPEntry[] cpEntries) {
115         // Read in NameAndTypeInfo values.
116
CPNameAndTypeInfo nati = (CPNameAndTypeInfo)cpEntries[iNameAndType];
117         setNameIndex(nati.iName);
118         setDescriptorIndex(nati.iDesc);
119     }
120 }
121
Popular Tags