KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.sun.source.tree.BlockTree;
23 import javax.lang.model.type.ArrayType;
24 import org.netbeans.api.java.source.CompilationController;
25 import com.sun.source.tree.ExpressionTree;
26 import com.sun.source.tree.MethodTree;
27 import com.sun.source.tree.Tree;
28 import com.sun.source.tree.TypeParameterTree;
29 import com.sun.source.tree.VariableTree;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Collections JavaDoc;
32 import java.util.List JavaDoc;
33 import javax.lang.model.element.Element;
34 import javax.lang.model.element.ExecutableElement;
35 import javax.lang.model.element.Modifier;
36 import javax.lang.model.element.TypeElement;
37 import javax.lang.model.element.VariableElement;
38 import javax.lang.model.type.TypeKind;
39 import javax.lang.model.type.TypeMirror;
40 import org.netbeans.api.java.source.CompilationInfo;
41 import org.netbeans.api.java.source.TreeMaker;
42 import org.netbeans.api.java.source.WorkingCopy;
43 import org.openide.util.Parameters;
44
45 /**
46  * Support class for {@link MethodModel} providing some factory and conversion methods.
47  *
48  * @author Martin Adamek
49  */

50 public final class MethodModelSupport {
51     
52     private MethodModelSupport() {}
53     
54     /**
55      * Creates new instance of method model. None of the parameters can be null.
56      * This method must be called from within javac context.
57      *
58      * @param workingCopy controller from javac context
59      * @param method method for which the model is to be created
60      * @throws NullPointerException if any of the parameters is <code>null</code>.
61      * @return immutable model of method
62      */

63     public static MethodModel createMethodModel(CompilationController controller, ExecutableElement method) {
64         Parameters.notNull("controller", controller); // NOI18N
65
Parameters.notNull("method", method); // NOI18N
66
List JavaDoc<MethodModel.Variable> parameters = new ArrayList JavaDoc<MethodModel.Variable>();
67         for (VariableElement variableElement : method.getParameters()) {
68             String JavaDoc type = getTypeName(controller, variableElement.asType());
69             String JavaDoc name = variableElement.getSimpleName().toString();
70             parameters.add(MethodModel.Variable.create(type, name));
71         }
72         List JavaDoc<String JavaDoc> exceptions = new ArrayList JavaDoc<String JavaDoc>();
73         for (TypeMirror typeMirror : method.getThrownTypes()) {
74             exceptions.add(getTypeName(controller, typeMirror));
75         }
76         return MethodModel.create(
77                 method.getSimpleName().toString(),
78                 getTypeName(controller, method.getReturnType()),
79                 //TODO: RETOUCHE get body of method
80
"",
81                 parameters,
82                 exceptions,
83                 method.getModifiers()
84                 );
85     }
86     
87     /**
88      * Creates new instance of model of class variable or method parameter
89      * This method must be called from within javac context.
90      *
91      * @param workingCopy controller from javac context
92      * @param variableElement variable or method parameter for which the model is to be created
93      * @throws NullPointerException if any of the parameters is <code>null</code>.
94      * @return immutable model of variable or method parameter
95      */

96     public static MethodModel.Variable createVariable(CompilationController controller, VariableElement variableElement) {
97         Parameters.notNull("controller", controller); //NOI18N
98
Parameters.notNull("variableElement", variableElement); //NOI18N
99
return MethodModel.Variable.create(
100                 getTypeName(controller, variableElement.asType()),
101                 variableElement.getSimpleName().toString(),
102                 variableElement.getModifiers().contains(Modifier.FINAL)
103                 );
104     }
105     
106     /**
107      * Creates {@link MethodTree} represented by methodModel in given javac context.
108      *
109      * @param workingCopy controller from javac context
110      * @param methodModel model of method
111      * @throws NullPointerException if any of the parameters is <code>null</code>.
112      * @return tree representing methodModel
113      */

114     public static MethodTree createMethodTree(WorkingCopy workingCopy, MethodModel methodModel) {
115         Parameters.notNull("workingCopy", workingCopy); //NOI18N
116
Parameters.notNull("methodModel", methodModel); //NOI18N
117
TreeMaker treeMaker = workingCopy.getTreeMaker();
118         List JavaDoc<VariableTree> paramsList = new ArrayList JavaDoc<VariableTree>();
119         if (methodModel.getParameters() != null) {
120             for (MethodModel.Variable parameter : methodModel.getParameters()) {
121                 VariableTree variableTree = treeMaker.Variable(
122                         treeMaker.Modifiers(Collections.<Modifier>emptySet()),
123                         parameter.getName(),
124                         getTypeTree(workingCopy, parameter.getType()),
125                         null
126                         );
127                 paramsList.add(variableTree);
128             }
129         }
130         List JavaDoc<ExpressionTree> throwsList = new ArrayList JavaDoc<ExpressionTree>();
131         for (String JavaDoc exceptionName : methodModel.getExceptions()) {
132             TypeElement element = workingCopy.getElements().getTypeElement(exceptionName);
133             throwsList.add(treeMaker.QualIdent(element));
134         }
135         MethodTree result;
136         String JavaDoc body = methodModel.getBody();
137         if (body == null) {
138             result = treeMaker.Method(
139                     treeMaker.Modifiers(methodModel.getModifiers()),
140                     methodModel.getName(),
141                     getTypeTree(workingCopy, methodModel.getReturnType()),
142                     Collections.<TypeParameterTree>emptyList(),
143                     paramsList,
144                     throwsList,
145                     (BlockTree) null,
146                     null
147                     );
148         } else {
149             result = treeMaker.Method(
150                     treeMaker.Modifiers(methodModel.getModifiers()),
151                     methodModel.getName(),
152                     getTypeTree(workingCopy, methodModel.getReturnType()),
153                     Collections.<TypeParameterTree>emptyList(),
154                     paramsList,
155                     throwsList,
156                     "{" + methodModel.getBody() + "}",
157                     null
158                     );
159         }
160         return result;
161     }
162     
163     /**
164      * Checks if signature of {@link ExecutableElement} is represented by {@link MethodModel} methodModel
165      * in given javac context.
166      *
167      * @param compilationInfo controller from javac context
168      * @param method method existing in given javac context
169      * @param methodModel model of method
170      * @throws NullPointerException if any of the parameters is <code>null</code>.
171      * @return true if method and methodModel have same signature, false otherwise
172      */

173     public static boolean isSameMethod(CompilationInfo compilationInfo, ExecutableElement method, MethodModel methodModel) {
174         //TODO: RETOUCHE fix this method, see #90505
175
Parameters.notNull("compilationInfo", compilationInfo); // NOI18N
176
Parameters.notNull("method", method); // NOI18N
177
Parameters.notNull("methodModel", methodModel); // NOI18N
178
if (!method.getSimpleName().contentEquals(methodModel.getName())) {
179             return false;
180         }
181         List JavaDoc<? extends VariableElement> methodParams = method.getParameters();
182         if (methodParams.size() != methodModel.getParameters().size()) {
183             return false;
184         }
185         for (int i = 0; i < methodParams.size(); i++) {
186             VariableElement variableElement = methodParams.get(i);
187             TypeMirror variableElementType = variableElement.asType();
188             MethodModel.Variable variable = methodModel.getParameters().get(i);
189             TypeElement typeElement = (TypeElement) method.getEnclosingElement();
190             TypeMirror variableType = compilationInfo.getTreeUtilities().parseType(variable.getType(), typeElement);
191             if (!compilationInfo.getTypes().isSameType(variableElementType, variableType)) {
192                 return false;
193             }
194         }
195         return true;
196     }
197     
198     //TODO: RETOUCHE move/reuse to GenerationUtil, this one has also void type
199
private static Tree getTypeTree(WorkingCopy workingCopy, String JavaDoc typeName) {
200         TreeMaker make = workingCopy.getTreeMaker();
201         TypeKind primitiveTypeKind = null;
202         if ("boolean".equals(typeName)) { // NOI18N
203
primitiveTypeKind = TypeKind.BOOLEAN;
204         } else if ("byte".equals(typeName)) { // NOI18N
205
primitiveTypeKind = TypeKind.BYTE;
206         } else if ("short".equals(typeName)) { // NOI18N
207
primitiveTypeKind = TypeKind.SHORT;
208         } else if ("int".equals(typeName)) { // NOI18N
209
primitiveTypeKind = TypeKind.INT;
210         } else if ("long".equals(typeName)) { // NOI18N
211
primitiveTypeKind = TypeKind.LONG;
212         } else if ("char".equals(typeName)) { // NOI18N
213
primitiveTypeKind = TypeKind.CHAR;
214         } else if ("float".equals(typeName)) { // NOI18N
215
primitiveTypeKind = TypeKind.FLOAT;
216         } else if ("double".equals(typeName)) { // NOI18N
217
primitiveTypeKind = TypeKind.DOUBLE;
218         } else if ("void".equals(typeName)) { // NOI18N
219
primitiveTypeKind = TypeKind.VOID;
220         }
221         if (primitiveTypeKind != null) {
222             return make.PrimitiveType(primitiveTypeKind);
223         } else {
224             return createQualIdent(workingCopy, typeName);
225         }
226     }
227     
228     //TODO: RETOUCHE move/reuse to GenerationUtil
229
private static ExpressionTree createQualIdent(WorkingCopy workingCopy, String JavaDoc typeName) {
230         TypeElement typeElement = workingCopy.getElements().getTypeElement(typeName);
231         if (typeElement == null) {
232             throw new IllegalArgumentException JavaDoc("Type " + typeName + " cannot be found"); // NOI18N
233
}
234         return workingCopy.getTreeMaker().QualIdent(typeElement);
235     }
236     
237     //TODO: RETOUCHE move/reuse in SourceUtil, or best - get from java/source!
238
// package private only for unit test
239
// see #90968
240
static String JavaDoc getTypeName(CompilationController controller, TypeMirror typeMirror) {
241         TypeKind typeKind = typeMirror.getKind();
242         switch (typeKind) {
243             case BOOLEAN : return "boolean"; // NOI18N
244
case BYTE : return "byte"; // NOI18N
245
case CHAR : return "char"; // NOI18N
246
case DOUBLE : return "double"; // NOI18N
247
case FLOAT : return "float"; // NOI18N
248
case INT : return "int"; // NOI18N
249
case LONG : return "long"; // NOI18N
250
case SHORT : return "short"; // NOI18N
251
case VOID : return "void"; // NOI18N
252
case DECLARED :
253                 Element element = controller.getTypes().asElement(typeMirror);
254                 return ((TypeElement) element).getQualifiedName().toString();
255             case ARRAY :
256                 ArrayType arrayType = (ArrayType) typeMirror;
257                 Element componentTypeElement = controller.getTypes().asElement(arrayType.getComponentType());
258                 return ((TypeElement) componentTypeElement).getQualifiedName().toString() + "[]";
259             case ERROR :
260             case EXECUTABLE :
261             case NONE :
262             case NULL :
263             case OTHER :
264             case PACKAGE :
265             case TYPEVAR :
266             case WILDCARD :
267             default:break;
268         }
269         return null;
270     }
271     
272 }
273
Popular Tags