KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Variable.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.17 $
24  */

25
26 package org.netbeans.modules.classfile;
27
28 import java.io.*;
29 import java.util.*;
30
31 /**
32  * A representation of a parameter to a method declaration. A parameter
33  * will not have a name, unless the classfile is compiled with local
34  * variable tables (if not, then the name is an empty string).
35  * A final modifier on a parameter is never reported,
36  * since that modifier is not stored in a classfile.
37  *
38  * @author Thomas Ball
39  */

40 public final class Parameter extends Field {
41
42     static Parameter[] makeParams(Method method) {
43     List<Parameter> paramList = new ArrayList<Parameter>();
44         for (Iterator<Parameter> it = new ParamIterator(method); it.hasNext();)
45             paramList.add(it.next());
46         return paramList.toArray(new Parameter[paramList.size()]);
47     }
48
49     private static Parameter createParameter (String JavaDoc name, String JavaDoc type, ClassFile classFile,
50             DataInputStream visibleAnnotations, DataInputStream invisibleAnnotations) {
51         return new Parameter (name, type, classFile,
52             visibleAnnotations, invisibleAnnotations);
53     }
54     
55     /** Creates new Parameter */
56     private Parameter(String JavaDoc name, String JavaDoc type, ClassFile classFile,
57             DataInputStream visibleAnnotations, DataInputStream invisibleAnnotations) {
58         super(name, type, classFile);
59         loadParameterAnnotations(visibleAnnotations, invisibleAnnotations);
60     }
61     
62     private void loadParameterAnnotations(DataInputStream visible, DataInputStream invisible) {
63         super.loadAnnotations();
64         if (annotations == null && (visible != null || invisible != null))
65             annotations = new HashMap<ClassName,Annotation>(2);
66         try {
67             if (visible != null && visible.available() > 0)
68                 Annotation.load(visible, classFile.getConstantPool(), true, annotations);
69         } catch (IOException e) {
70             throw new InvalidClassFileAttributeException("invalid RuntimeVisibleParameterAnnotations attribute", e);
71         }
72         try {
73             if (invisible != null && invisible.available() > 0)
74                 Annotation.load(invisible, classFile.getConstantPool(), false, annotations);
75         } catch (IOException e) {
76             throw new InvalidClassFileAttributeException("invalid RuntimeInvisibleParameterAnnotations attribute", e);
77         }
78     }
79
80     /**
81      * Return a string in the form "<type> <name>". Class types
82      * are shown in a "short" form; i.e. "Object" instead of
83      * "java.lang.Object"j.
84      *
85      * @return string describing the variable and its type.
86      */

87     public final String JavaDoc getDeclaration() {
88     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
89     sb.append(CPFieldMethodInfo.getSignature(getDescriptor(), false));
90     String JavaDoc name = getName();
91     if (name != null) {
92         sb.append(' ');
93         sb.append(name);
94     }
95     return sb.toString();
96     }
97     
98     public String JavaDoc toString() {
99         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("name=");
100     sb.append(getName());
101         sb.append(" type="); //NOI18N
102
sb.append(getDescriptor());
103     if (getTypeSignature() != null) {
104         sb.append(", signature="); //NOI18N
105
sb.append(typeSignature);
106     }
107         loadAnnotations();
108     if (annotations.size() > 0) {
109         Iterator iter = annotations.values().iterator();
110         sb.append(", annotations={ ");
111         while (iter.hasNext()) {
112         sb.append(iter.next().toString());
113         if (iter.hasNext())
114             sb.append(", ");
115         }
116         sb.append(" }");
117     }
118         return sb.toString();
119     }
120
121     private static class ParamIterator implements Iterator<Parameter> {
122         ClassFile classFile;
123         String JavaDoc signature;
124         LocalVariableTableEntry[] localVars;
125
126         /** the current local variable array position */
127         int ivar;
128
129         /** the current character in the type signature */
130         int isig;
131         
132         /** annotation attributes */
133         DataInputStream visibleAnnotations;
134         DataInputStream invisibleAnnotations;
135         
136         /**
137          * @param method
138          */

139         ParamIterator(Method method) {
140             classFile = method.getClassFile();
141             signature = method.getDescriptor();
142             assert signature.charAt(0) == '(';
143             isig = 1; // skip '('
144
ivar = method.isStatic() ? 0 : 1;
145         Code code = method.getCode();
146             localVars = code != null ?
147         code.getLocalVariableTable() :
148         new LocalVariableTableEntry[0];
149             AttributeMap attrs = method.getAttributes();
150             try {
151                 visibleAnnotations =
152                     getParamAttr(attrs, "RuntimeVisibleParameterAnnotations"); //NOI18N
153
} catch (IOException e) {
154                 throw new InvalidClassFileAttributeException("invalid RuntimeVisibleParameterAnnotations attribute", e);
155             }
156             try {
157                 invisibleAnnotations =
158                     getParamAttr(attrs, "RuntimeInvisibleParameterAnnotations"); //NOI18N
159
} catch (IOException e) {
160                 throw new InvalidClassFileAttributeException("invalid RuntimeInvisibleParameterAnnotations attribute", e);
161             }
162         }
163         
164         private DataInputStream getParamAttr(AttributeMap attrs, String JavaDoc name) throws IOException {
165             DataInputStream in = attrs.getStream(name);
166             if (in != null)
167                 in.readByte(); // skip the redundant parameters number
168
return in;
169         }
170         
171         public boolean hasNext() {
172             return signature.charAt(isig) != ')';
173         }
174         
175         public Parameter next() {
176             if (hasNext()) {
177         String JavaDoc name = "";
178         for (int i = 0; i < localVars.length; i++) {
179             LocalVariableTableEntry lvte = localVars[i];
180             // only parameters have a startPC of zero
181
if (lvte.index == ivar && lvte.startPC == 0) {
182             name = localVars[i].getName();
183             break;
184             }
185         }
186                 ivar++;
187                 int sigStart = isig;
188                 while (isig < signature.length()) {
189                     char ch = signature.charAt(isig);
190                     switch (ch) {
191                         case '[':
192                             isig++;
193                             break;
194                         case 'B':
195                         case 'C':
196                         case 'F':
197                         case 'I':
198                         case 'S':
199                         case 'Z':
200                         case 'V': {
201                             String JavaDoc type = signature.substring(sigStart, ++isig);
202                             return Parameter.createParameter(name, type, classFile,
203                                     visibleAnnotations, invisibleAnnotations);
204                         }
205                         case 'D':
206                         case 'J': {
207                             ivar++; // longs and doubles take two slots
208
String JavaDoc type = signature.substring(sigStart, ++isig);
209                             return Parameter.createParameter(name, type, classFile,
210                                     visibleAnnotations, invisibleAnnotations);
211                         }
212                         case 'L': {
213                             int end = signature.indexOf(';', isig) + 1;
214                             String JavaDoc type = signature.substring(isig, end);
215                             isig = end;
216                             return Parameter.createParameter(name, type, classFile,
217                                     visibleAnnotations, invisibleAnnotations);
218                         }
219
220                     }
221                 }
222             }
223             throw new NoSuchElementException();
224         }
225         
226         public void remove() {
227             throw new UnsupportedOperationException JavaDoc();
228         }
229     }
230 }
231
Popular Tags