KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ejbcore > api > codegeneration > Util


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.ejbcore.api.codegeneration;
21
22 import java.io.IOException JavaDoc;
23 import java.util.List JavaDoc;
24 import javax.lang.model.element.Element;
25 import javax.lang.model.element.ExecutableElement;
26 import javax.lang.model.element.Name;
27 import javax.lang.model.element.TypeElement;
28 import javax.lang.model.element.VariableElement;
29 import javax.lang.model.type.ArrayType;
30 import javax.lang.model.type.TypeKind;
31 import javax.lang.model.type.TypeMirror;
32 import javax.lang.model.util.ElementFilter;
33 import org.netbeans.api.java.source.CancellableTask;
34 import org.netbeans.api.java.source.CompilationController;
35 import org.netbeans.api.java.source.JavaSource;
36 import org.netbeans.modules.j2ee.common.method.MethodModel;
37 import org.netbeans.modules.j2ee.common.method.MethodModelSupport;
38 import org.openide.filesystems.FileObject;
39
40 /**
41  *
42  * @author Martin Adamek
43  */

44 public final class Util {
45     
46     private Util() {}
47     
48     protected static boolean directlyImplements(CompilationController controller, TypeElement typeElement, String JavaDoc[] interfaces) {
49         List JavaDoc<? extends TypeMirror> foundInterfaces = typeElement.getInterfaces();
50         if (foundInterfaces.size() != interfaces.length) {
51             return false;
52         }
53         for (TypeMirror typeMirror : foundInterfaces) {
54             TypeElement element = (TypeElement) controller.getTypes().asElement(typeMirror);
55             if (!containsName(interfaces, element.getQualifiedName())) {
56                 return false;
57             }
58         }
59         return true;
60     }
61     
62     protected static boolean contains(CompilationController controller, TypeElement typeElement, MethodModel methodModel) {
63         for (ExecutableElement executableElement : ElementFilter.methodsIn(typeElement.getEnclosedElements())) {
64             if (MethodModelSupport.isSameMethod(controller, executableElement, methodModel)) {
65                 return true;
66             }
67         }
68         return false;
69     }
70     
71     protected static boolean contains(CompilationController controller, TypeElement typeElement, MethodModel.Variable field) {
72         for (VariableElement variableElement : ElementFilter.fieldsIn(typeElement.getEnclosedElements())) {
73             if (getTypeName(controller, variableElement.asType()).equals(field.getType()) &&
74                     variableElement.getSimpleName().contentEquals(field.getName())) {
75                 return true;
76             }
77         }
78         
79         return false;
80     }
81     
82     protected static boolean containsName(String JavaDoc[] stringNames, Name name) {
83         for (String JavaDoc stringName : stringNames) {
84             if (name.contentEquals(stringName)) {
85                 return true;
86             }
87         }
88         return false;
89     }
90     
91     protected static void runUserActionTask(FileObject javaFile, CancellableTask<CompilationController> taskToTest) throws IOException JavaDoc {
92         JavaSource javaSource = JavaSource.forFileObject(javaFile);
93         javaSource.runUserActionTask(taskToTest, true);
94     }
95     
96     // see #90968
97
protected static String JavaDoc getTypeName(CompilationController controller, TypeMirror typeMirror) {
98         TypeKind typeKind = typeMirror.getKind();
99         switch (typeKind) {
100             case BOOLEAN : return "boolean"; // NOI18N
101
case BYTE : return "byte"; // NOI18N
102
case CHAR : return "char"; // NOI18N
103
case DOUBLE : return "double"; // NOI18N
104
case FLOAT : return "float"; // NOI18N
105
case INT : return "int"; // NOI18N
106
case LONG : return "long"; // NOI18N
107
case SHORT : return "short"; // NOI18N
108
case VOID : return "void"; // NOI18N
109
case DECLARED :
110                 Element element = controller.getTypes().asElement(typeMirror);
111                 return ((TypeElement) element).getQualifiedName().toString();
112             case ARRAY :
113                 ArrayType arrayType = (ArrayType) typeMirror;
114                 Element componentTypeElement = controller.getTypes().asElement(arrayType.getComponentType());
115                 return ((TypeElement) componentTypeElement).getQualifiedName().toString() + "[]";
116             case ERROR :
117             case EXECUTABLE :
118             case NONE :
119             case NULL :
120             case OTHER :
121             case PACKAGE :
122             case TYPEVAR :
123             case WILDCARD :
124                 break;
125         }
126         return null;
127     }
128
129 }
130
Popular Tags