1 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 ; 31 import java.util.Collections ; 32 import java.util.List ; 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 50 public final class MethodModelSupport { 51 52 private MethodModelSupport() {} 53 54 63 public static MethodModel createMethodModel(CompilationController controller, ExecutableElement method) { 64 Parameters.notNull("controller", controller); Parameters.notNull("method", method); List <MethodModel.Variable> parameters = new ArrayList <MethodModel.Variable>(); 67 for (VariableElement variableElement : method.getParameters()) { 68 String type = getTypeName(controller, variableElement.asType()); 69 String name = variableElement.getSimpleName().toString(); 70 parameters.add(MethodModel.Variable.create(type, name)); 71 } 72 List <String > exceptions = new ArrayList <String >(); 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 "", 81 parameters, 82 exceptions, 83 method.getModifiers() 84 ); 85 } 86 87 96 public static MethodModel.Variable createVariable(CompilationController controller, VariableElement variableElement) { 97 Parameters.notNull("controller", controller); Parameters.notNull("variableElement", variableElement); return MethodModel.Variable.create( 100 getTypeName(controller, variableElement.asType()), 101 variableElement.getSimpleName().toString(), 102 variableElement.getModifiers().contains(Modifier.FINAL) 103 ); 104 } 105 106 114 public static MethodTree createMethodTree(WorkingCopy workingCopy, MethodModel methodModel) { 115 Parameters.notNull("workingCopy", workingCopy); Parameters.notNull("methodModel", methodModel); TreeMaker treeMaker = workingCopy.getTreeMaker(); 118 List <VariableTree> paramsList = new ArrayList <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 <ExpressionTree> throwsList = new ArrayList <ExpressionTree>(); 131 for (String exceptionName : methodModel.getExceptions()) { 132 TypeElement element = workingCopy.getElements().getTypeElement(exceptionName); 133 throwsList.add(treeMaker.QualIdent(element)); 134 } 135 MethodTree result; 136 String 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 173 public static boolean isSameMethod(CompilationInfo compilationInfo, ExecutableElement method, MethodModel methodModel) { 174 Parameters.notNull("compilationInfo", compilationInfo); Parameters.notNull("method", method); Parameters.notNull("methodModel", methodModel); if (!method.getSimpleName().contentEquals(methodModel.getName())) { 179 return false; 180 } 181 List <? 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 private static Tree getTypeTree(WorkingCopy workingCopy, String typeName) { 200 TreeMaker make = workingCopy.getTreeMaker(); 201 TypeKind primitiveTypeKind = null; 202 if ("boolean".equals(typeName)) { primitiveTypeKind = TypeKind.BOOLEAN; 204 } else if ("byte".equals(typeName)) { primitiveTypeKind = TypeKind.BYTE; 206 } else if ("short".equals(typeName)) { primitiveTypeKind = TypeKind.SHORT; 208 } else if ("int".equals(typeName)) { primitiveTypeKind = TypeKind.INT; 210 } else if ("long".equals(typeName)) { primitiveTypeKind = TypeKind.LONG; 212 } else if ("char".equals(typeName)) { primitiveTypeKind = TypeKind.CHAR; 214 } else if ("float".equals(typeName)) { primitiveTypeKind = TypeKind.FLOAT; 216 } else if ("double".equals(typeName)) { primitiveTypeKind = TypeKind.DOUBLE; 218 } else if ("void".equals(typeName)) { primitiveTypeKind = TypeKind.VOID; 220 } 221 if (primitiveTypeKind != null) { 222 return make.PrimitiveType(primitiveTypeKind); 223 } else { 224 return createQualIdent(workingCopy, typeName); 225 } 226 } 227 228 private static ExpressionTree createQualIdent(WorkingCopy workingCopy, String typeName) { 230 TypeElement typeElement = workingCopy.getElements().getTypeElement(typeName); 231 if (typeElement == null) { 232 throw new IllegalArgumentException ("Type " + typeName + " cannot be found"); } 234 return workingCopy.getTreeMaker().QualIdent(typeElement); 235 } 236 237 static String getTypeName(CompilationController controller, TypeMirror typeMirror) { 241 TypeKind typeKind = typeMirror.getKind(); 242 switch (typeKind) { 243 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 : 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 |