1 19 20 package org.netbeans.modules.j2ee.common.source; 21 22 import com.sun.source.tree.*; 23 import com.sun.source.util.*; 24 import java.io.IOException ; 25 import java.util.List ; 26 import java.util.Set ; 27 import javax.lang.model.element.*; 28 import javax.lang.model.type.*; 29 import org.netbeans.api.java.source.CompilationController; 30 import org.netbeans.api.java.source.ElementUtilities; 31 import org.netbeans.api.java.source.JavaSource.Phase; 32 import org.openide.util.Parameters; 33 34 38 public class SourceUtils { 39 40 42 45 private final CompilationController controller; 46 47 51 private TypeElement typeElement; 52 53 57 private ClassTree classTree; 58 59 61 SourceUtils(CompilationController controller, TypeElement typeElement) { 62 this.controller = controller; 63 this.typeElement = typeElement; 64 } 65 66 SourceUtils(CompilationController controller, ClassTree classTree) { 67 this.controller = controller; 68 this.classTree = classTree; 69 } 70 71 public static SourceUtils newInstance(CompilationController controller, TypeElement typeElement) { 72 Parameters.notNull("controller", controller); Parameters.notNull("typeElement", typeElement); 75 return new SourceUtils(controller, typeElement); 76 } 77 78 public static SourceUtils newInstance(CompilationController controller, ClassTree classTree) { 79 Parameters.notNull("controller", controller); Parameters.notNull("classTree", classTree); 82 return new SourceUtils(controller, classTree); 83 } 84 85 public static SourceUtils newInstance(CompilationController controller) throws IOException { 86 Parameters.notNull("controller", controller); 88 ClassTree classTree = findPublicTopLevelClass(controller); 89 if (classTree != null) { 90 return newInstance(controller, classTree); 91 } 92 return null; 93 } 94 95 97 99 107 static ClassTree findPublicTopLevelClass(CompilationController controller) throws IOException { 108 controller.toPhase(Phase.ELEMENTS_RESOLVED); 109 110 final String mainElementName = controller.getFileObject().getName(); 111 for (Tree tree : controller.getCompilationUnit().getTypeDecls()) { 112 if (tree.getKind() != Tree.Kind.CLASS) { 113 continue; 114 } 115 ClassTree classTree = (ClassTree)tree; 116 if (!classTree.getSimpleName().contentEquals(mainElementName)) { 117 continue; 118 } 119 if (!classTree.getModifiers().getFlags().contains(Modifier.PUBLIC)) { 120 continue; 121 } 122 return classTree; 123 } 124 return null; 125 } 126 127 129 131 137 public TypeElement getTypeElement() { 138 if (typeElement == null) { 139 assert classTree != null; 140 TreePath classTreePath = controller.getTrees().getPath(getCompilationController().getCompilationUnit(), classTree); 141 typeElement = (TypeElement)controller.getTrees().getElement(classTreePath); 142 } 143 return typeElement; 144 } 145 146 152 public ClassTree getClassTree() { 153 if (classTree == null) { 154 assert typeElement != null; 155 classTree = controller.getTrees().getTree(typeElement); 156 } 157 return classTree; 158 } 159 160 168 public boolean isSubtype(String type) { 169 Parameters.notNull("type", type); 171 TypeMirror typeMirror = getCompilationController().getTreeUtilities().parseType(type, getTypeElement()); 172 if (typeMirror != null) { 173 return getCompilationController().getTypes().isSubtype(getTypeElement().asType(), typeMirror); 174 } 175 return false; 176 } 177 178 180 182 186 CompilationController getCompilationController() { 187 return controller; 188 } 189 190 193 ExecutableElement getNoArgConstructor() throws IOException { 194 controller.toPhase(Phase.ELEMENTS_RESOLVED); 195 196 ElementUtilities elementUtils = controller.getElementUtilities(); 197 for (Element element : getTypeElement().getEnclosedElements()) { 198 if (element.getKind() == ElementKind.CONSTRUCTOR) { 199 ExecutableElement constructor = (ExecutableElement)element; 200 if (constructor.getParameters().size() == 0 && !elementUtils.isSynthetic(constructor)) { 201 return constructor; 202 } 203 } 204 } 205 return null; 206 } 207 208 211 private boolean isMainMethod(ExecutableElement method) { 212 if (!method.getSimpleName().contentEquals("main")) { return false; 215 } 216 Set <Modifier> modifiers = method.getModifiers(); 218 if (!modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC)) { 219 return false; 220 } 221 if (TypeKind.VOID != method.getReturnType().getKind()) { 223 return false; 224 } 225 List <? extends VariableElement> params = method.getParameters(); 228 if (params.size() != 1) { 229 return false; 230 } 231 VariableElement param = params.get(0); TypeMirror paramType = param.asType(); 233 if (TypeKind.ARRAY != paramType.getKind()) { 235 return false; 236 } 237 ArrayType arrayType = (ArrayType) paramType; 238 TypeElement stringTypeElement = controller.getElements().getTypeElement(String .class.getName()); 239 if (!controller.getTypes().isSameType(stringTypeElement.asType(), arrayType.getComponentType())) { 241 return false; 242 } 243 return true; 244 } 245 246 } 248 | Popular Tags |