KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > common > method > MethodModel


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.netbeans.modules.j2ee.common.method;
21
22 import java.util.Collections JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25 import javax.lang.model.element.Modifier;
26 import org.openide.util.Parameters;
27
28 /**
29  * Immutable model of method.
30  * Check {@link MethodModelSupport} for additional support functionality related to this class
31  *
32  * @author Martin Adamek
33  */

34 public final class MethodModel {
35     
36     private final String JavaDoc name;
37     private final String JavaDoc returnType;
38     private final String JavaDoc body;
39     private final List JavaDoc<Variable> parameters; // unmodifiable list
40
private final List JavaDoc<String JavaDoc> exceptions; // unmodifiable list
41
private final Set JavaDoc<Modifier> modifiers; // unmodifiable set
42

43     private MethodModel(String JavaDoc name, String JavaDoc returnType, String JavaDoc body, List JavaDoc<Variable> parameters, List JavaDoc<String JavaDoc> exceptions, Set JavaDoc<Modifier> modifiers) {
44         this.name = name;
45         this.returnType = returnType;
46         this.body = body;
47         this.parameters = Collections.unmodifiableList(parameters);
48         this.exceptions = Collections.unmodifiableList(exceptions);
49         this.modifiers = Collections.unmodifiableSet(modifiers);
50     }
51     
52     /**
53      * Creates new instance of method model. None of the parameters can be null.
54      *
55      * @param name name of the method, must be valid Java identifier
56      * @param returnType name of return type as written in source code,
57      * for non-primitive types fully-qualfied name must be used,
58      * must contain at least one non-whitespace character
59      * @param body string representation of body, can be null
60      * @param parameters list of method parameters, can be empty
61      * @param exceptions list of exceptions represented by fully-qualified names of exceptions, can be empty
62      * @param modifiers list of modifiers of method, can be empty
63      * @throws NullPointerException if any of the parameters is <code>null</code>.
64      * @throws IllegalArgumentException if the paramter returnType does not contain at least one non-whitespace character
65      * or the parameter name is not a valid Java identifier
66      * @return immutable model of method
67      */

68     public static MethodModel create(String JavaDoc name, String JavaDoc returnType, String JavaDoc body, List JavaDoc<Variable> parameters, List JavaDoc<String JavaDoc> exceptions, Set JavaDoc<Modifier> modifiers) {
69         Parameters.javaIdentifier("name", name);
70         Parameters.notWhitespace("returnType", returnType);
71         Parameters.notNull("parameters", parameters);
72         Parameters.notNull("exceptions", exceptions);
73         Parameters.notNull("modifiers", modifiers);
74         return new MethodModel(name, returnType, body, parameters, exceptions, modifiers);
75     }
76     
77     /**
78      * Immutable type representing class field or method parameter
79      */

80     public static final class Variable {
81         
82         private final String JavaDoc type;
83         private final String JavaDoc name;
84         private final boolean finalModifier;
85         
86         private Variable(String JavaDoc type, String JavaDoc name, boolean finalModifier) {
87             this.type = type;
88             this.name = name;
89             this.finalModifier = finalModifier;
90         }
91
92         /**
93          * Creates new instance of a model of class variable or method parameter
94          * without final modifier. Same as calling {@link #create(String, String, boolean)}
95          * with 3rd argument set to false
96          *
97          * @param type name of type as written in source code
98          * for non-primitive types fully-qualfied name must be used,
99          * must contain at least one non-whitespace character
100          * @param name name of the paramter or variable, must be valid Java identifier
101          * @throws NullPointerException if any of the parameters is <code>null</code>.
102          * @throws IllegalArgumentException if the paramter type does not contain at least one non-whitespace character
103          * or the parameter name is not a valid Java identifier
104          * @return immutable model of variable or method parameter
105          */

106         public static Variable create(String JavaDoc type, String JavaDoc name) {
107             Parameters.notWhitespace("type", type);
108             Parameters.javaIdentifier("name", name);
109             return new MethodModel.Variable(type, name, false);
110         }
111         
112         /**
113          * Creates new instance of a model of class variable or method parameter
114          *
115          * @param type name of type as written in source code
116          * for non-primitive types fully-qualfied name must be used,
117          * must contain at least one non-whitespace character
118          * @param name name of the paramter or variable, must be valid Java identifier
119          * @param finalModifier specifies if variable is final (if it has final modifier)
120          * @throws NullPointerException if any of the parameters is <code>null</code>.
121          * @throws IllegalArgumentException if the paramter type does not contain at least one non-whitespace character
122          * or the parameter name is not a valid Java identifier
123          * @return immutable model of variable or method parameter
124          */

125         public static Variable create(String JavaDoc type, String JavaDoc name, boolean finalModifier) {
126             Parameters.notWhitespace("type", type);
127             Parameters.javaIdentifier("name", name);
128             return new MethodModel.Variable(type, name, finalModifier);
129         }
130         
131         // <editor-fold defaultstate="collapsed" desc="Variable's getters">
132

133         /**
134          * Variable or method paramter type, for non-primitive types fully-qualified name is returned
135          *
136          * @return non-null value
137          */

138         public String JavaDoc getType() {
139             return type;
140         }
141         
142         /**
143          * Variable or method paramter name
144          *
145          * @return non-null value
146          */

147         public String JavaDoc getName() {
148             return name;
149         }
150         
151         /**
152          * Flag specifying if variable is final or not.
153          *
154          * @return true if variable is final, false otherwise
155          */

156         public boolean getFinalModifier() {
157             return finalModifier;
158         }
159         
160     // </editor-fold>
161

162     }
163     
164     // <editor-fold defaultstate="collapsed" desc="MethodModel's getters">
165

166     /**
167      * Method name
168      *
169      * @return non-null value
170      */

171     public String JavaDoc getName() {
172         return name;
173     }
174     
175     /**
176      * Return type, for non-primitive types fully-qualified name is returned
177      *
178      * @return non-null value
179      */

180     public String JavaDoc getReturnType() {
181         return returnType;
182     }
183     
184     /**
185      * String representation of method body
186      *
187      * @return non-null value
188      */

189     public String JavaDoc getBody() {
190         return body;
191     }
192     
193     /**
194      * Unmodifiable list of method parameters. Attempts to modify the returned list, whether
195      * direct or via its iterator, result in an <tt>UnsupportedOperationException</tt>.
196      *
197      * @return non-null value
198      */

199     public List JavaDoc<Variable> getParameters() {
200         return parameters;
201     }
202     
203     /**
204      * Unmodifiable list of method exceptions. Attempts to modify the returned list, whether
205      * direct or via its iterator, result in an <tt>UnsupportedOperationException</tt>.
206      *
207      * @return non-null value
208      */

209     public List JavaDoc<String JavaDoc> getExceptions() {
210         return exceptions;
211     }
212     
213     /**
214      * Unmodifiable set of method modifiers. Attempts to modify the returned set, whether
215      * direct or via its iterator, result in an <tt>UnsupportedOperationException</tt>.
216      *
217      * @return non-null value
218      */

219     public Set JavaDoc<Modifier> getModifiers() {
220         return modifiers;
221     }
222     
223     // </editor-fold>
224

225     @Override JavaDoc
226     public String JavaDoc toString() {
227         return "MethodModel<" + modifiers + "," + returnType + "," + name + "," + parameters + "," + exceptions + ",{" + body + "}>";
228     }
229     
230 }
231
Popular Tags