1 19 20 package org.netbeans.modules.j2ee.ejbcore.api.codegeneration; 21 22 import java.io.IOException ; 23 import java.util.List ; 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 44 public final class Util { 45 46 private Util() {} 47 48 protected static boolean directlyImplements(CompilationController controller, TypeElement typeElement, String [] interfaces) { 49 List <? 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 [] stringNames, Name name) { 83 for (String 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 { 92 JavaSource javaSource = JavaSource.forFileObject(javaFile); 93 javaSource.runUserActionTask(taskToTest, true); 94 } 95 96 protected static String getTypeName(CompilationController controller, TypeMirror typeMirror) { 98 TypeKind typeKind = typeMirror.getKind(); 99 switch (typeKind) { 100 case BOOLEAN : return "boolean"; case BYTE : return "byte"; case CHAR : return "char"; case DOUBLE : return "double"; case FLOAT : return "float"; case INT : return "int"; case LONG : return "long"; case SHORT : return "short"; case VOID : return "void"; 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 |