KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > MethodParameter


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

19
20 package org.openide.src;
21
22 import java.util.StringTokenizer JavaDoc;
23
24 /** Describes an argument of a method.
25 *
26 * @author Petr Hamernik, Jaroslav Tulach
27 */

28 public final class MethodParameter extends Object JavaDoc implements java.io.Serializable JavaDoc {
29     /** Name of argument */
30     private String JavaDoc name;
31
32     /** Type of argument */
33     private Type type;
34
35     /** State of final flag */
36     private boolean fin;
37
38     static final long serialVersionUID =-6158959006278766562L;
39     /** Create new parameter.
40     * @param name the name of the parameter
41     * @param type the type of the parameter
42     * @param fin <code>true</code> if this parameter is <code>final</code>
43     */

44     public MethodParameter(String JavaDoc name, Type type, boolean fin) {
45         this.name = name;
46         this.type = type;
47         this.fin = fin;
48     }
49     
50     /** Create a method parameter by parsing its textual representation.
51     * @param text the text to be parsed
52     * @return a new method parameter described by the text
53     * @exception IllegalArgumentException if the syntax is not recognized
54     */

55     public static MethodParameter parse(String JavaDoc text) throws IllegalArgumentException JavaDoc {
56         StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(text, " []", true); // NOI18N
57
boolean rightBracketExpected = false;
58
59         boolean fin = false;
60         Type type = null;
61         String JavaDoc name = null;
62
63         while (tok.hasMoreTokens()) {
64             String JavaDoc token = tok.nextToken();
65             if (token.equals(" ")) // NOI18N
66
continue;
67
68             if (type == null) { // we are before type
69
if (token.equals("final")) { // final // NOI18N
70
if (fin) // final already was once
71
throw new IllegalArgumentException JavaDoc();
72                     fin = true;
73                 }
74                 else { // type
75
type = Type.parse(token);
76                 }
77             }
78             else { // we are after type
79
if (token.equals("[")) { // left bracket -> right bracket is expected // NOI18N
80
if (rightBracketExpected)
81                         throw new IllegalArgumentException JavaDoc();
82                     rightBracketExpected = true;
83                 }
84                 else if (token.equals("]")) { // right bracket -> create array // NOI18N
85
if (!rightBracketExpected)
86                         throw new IllegalArgumentException JavaDoc();
87                     type = Type.createArray(type);
88                     rightBracketExpected = false;
89                 }
90                 else { // it must be name of the parameter
91
if (name != null) //already was!
92
throw new IllegalArgumentException JavaDoc();
93                     name = token;
94                 }
95             }
96         }
97         if ((type == null) || (name == null) || rightBracketExpected)
98             throw new IllegalArgumentException JavaDoc();
99         // ensure that the 'name' can be used as an identifier (parse throws IAE)
100
Type t = Type.parse(name);
101         if (!t.isClass())
102             throw new IllegalArgumentException JavaDoc();
103         return new MethodParameter(name, type, fin);
104     }
105
106     /** Get the parameter type.
107     * @return the type
108     */

109     public Type getType() {
110         return type;
111     }
112
113     /** Set the parameter type.
114     * @param type the new type
115     */

116     public void setType(Type type) {
117         this.type = type;
118     }
119
120     /** Get the name of the parameter variable.
121     * @return the name
122     */

123     public String JavaDoc getName() {
124         return name;
125     }
126
127     /** Set the name of the parameter variable.
128     * @param name the new name
129     */

130     public void setName(String JavaDoc name) {
131         this.name = name;
132     }
133
134     /** Make this parameter final or not.
135     * @param fin <code>true</code> to make it final, <code>false</code> to make it unfinal
136     */

137     public void setFinal(boolean fin) {
138         this.fin = fin;
139     }
140
141     /** Test whether this parameter is final.
142     * @return <code>true</code> if so
143     */

144     public boolean isFinal() {
145         return fin;
146     }
147
148     /** Get this MethodParameter as the string.
149     * @param appendTo The string buffer where to append to
150     * @param source true means getSourceName() will be used, otherwise getFullName()
151     * @return the same string buffer which was passed into
152     */

153     StringBuffer JavaDoc getAsString(StringBuffer JavaDoc appendTo, boolean source) {
154         if (fin)
155             appendTo.append("final "); // NOI18N
156
type.getAsString(appendTo, source);
157         appendTo.append(" "); // NOI18N
158
appendTo.append(name);
159         return appendTo;
160     }
161
162     /* @return the text form of the method parameter - fully qualified type.
163     */

164     public String JavaDoc getFullString() {
165         return getAsString(new StringBuffer JavaDoc(), false).toString();
166     }
167
168     /* @return the text form of the method parameter - using
169     * getSourceString() for the type
170     */

171     public String JavaDoc getSourceString() {
172         return getAsString(new StringBuffer JavaDoc(), true).toString();
173     }
174
175     /* @return the text form of the method parameter - fully qualified type.
176     */

177     public String JavaDoc toString() {
178         return getAsString(new StringBuffer JavaDoc(), false).toString();
179     }
180
181     /** Compare the specified MethodParameter with this for equality.
182     * @param param MethodParameter to be compared with this
183     * @param onlyType Compares only the type (not name and final flag)
184     * @param source Determine if the source name (for class types)
185     * should be also compared.
186     * If <CODE>false</CODE> only fully qualified name is compared.
187     * @return <CODE>true</CODE> if the specified object equals to
188     * specified MethodParameter otherwise <CODE>false</CODE>.
189     */

190     public boolean compareTo(MethodParameter param, boolean onlyType, boolean source) {
191         if (!type.compareTo(param.type, source))
192             return false;
193
194         return onlyType ? true : ((param.fin == fin) && (param.name.equals(name)));
195     }
196
197
198     /* @return <CODE>true</CODE> if the given object represents
199     * the same parameter. Compares only the type.
200     */

201     public boolean equals(Object JavaDoc o) {
202         return (o instanceof MethodParameter) ?
203                compareTo((MethodParameter)o, true, false) :
204                false;
205     }
206
207     /* @return The hashcode of this parameter
208     */

209     public int hashCode() {
210         return name.hashCode() + type.hashCode();
211     }
212 }
213
Popular Tags