1 19 20 package org.netbeans.modules.j2ee.persistence.util; 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 42 public class SourceUtils { 43 44 46 49 private final CompilationController controller; 50 51 55 private TypeElement typeElement; 56 57 61 private ClassTree classTree; 62 63 65 SourceUtils(CompilationController controller, TypeElement typeElement) { 66 this.controller = controller; 67 this.typeElement = typeElement; 68 } 69 70 SourceUtils(CompilationController controller, ClassTree classTree) { 71 this.controller = controller; 72 this.classTree = classTree; 73 } 74 75 public static SourceUtils newInstance(CompilationController controller, TypeElement typeElement) { 76 Parameters.notNull("controller", controller); Parameters.notNull("typeElement", typeElement); 79 return new SourceUtils(controller, typeElement); 80 } 81 82 public static SourceUtils newInstance(CompilationController controller, ClassTree classTree) { 83 Parameters.notNull("controller", controller); Parameters.notNull("classTree", classTree); 86 return new SourceUtils(controller, classTree); 87 } 88 89 public static SourceUtils newInstance(CompilationController controller) throws IOException { 90 Parameters.notNull("controller", controller); 92 ClassTree classTree = findPublicTopLevelClass(controller); 93 if (classTree != null) { 94 return newInstance(controller, classTree); 95 } 96 return null; 97 } 98 99 101 103 111 static ClassTree findPublicTopLevelClass(CompilationController controller) throws IOException { 112 controller.toPhase(Phase.ELEMENTS_RESOLVED); 113 114 final String mainElementName = controller.getFileObject().getName(); 115 for (Tree tree : controller.getCompilationUnit().getTypeDecls()) { 116 if (tree.getKind() != Tree.Kind.CLASS) { 117 continue; 118 } 119 ClassTree classTree = (ClassTree)tree; 120 if (!classTree.getSimpleName().contentEquals(mainElementName)) { 121 continue; 122 } 123 if (!classTree.getModifiers().getFlags().contains(Modifier.PUBLIC)) { 124 continue; 125 } 126 return classTree; 127 } 128 return null; 129 } 130 131 133 135 141 public TypeElement getTypeElement() { 142 if (typeElement == null) { 143 assert classTree != null; 144 TreePath classTreePath = controller.getTrees().getPath(getCompilationController().getCompilationUnit(), classTree); 145 typeElement = (TypeElement)controller.getTrees().getElement(classTreePath); 146 } 147 return typeElement; 148 } 149 150 156 public ClassTree getClassTree() { 157 if (classTree == null) { 158 assert typeElement != null; 159 classTree = controller.getTrees().getTree(typeElement); 160 } 161 return classTree; 162 } 163 164 172 public boolean isSubtype(String type) { 173 Parameters.notNull("type", type); 175 TypeMirror typeMirror = getCompilationController().getTreeUtilities().parseType(type, getTypeElement()); 176 if (typeMirror != null) { 177 return getCompilationController().getTypes().isSubtype(getTypeElement().asType(), typeMirror); 178 } 179 return false; 180 } 181 182 184 186 190 CompilationController getCompilationController() { 191 return controller; 192 } 193 194 197 ExecutableElement getNoArgConstructor() throws IOException { 198 controller.toPhase(Phase.ELEMENTS_RESOLVED); 199 200 ElementUtilities elementUtils = controller.getElementUtilities(); 201 for (Element element : getTypeElement().getEnclosedElements()) { 202 if (element.getKind() == ElementKind.CONSTRUCTOR) { 203 ExecutableElement constructor = (ExecutableElement)element; 204 if (constructor.getParameters().size() == 0 && !elementUtils.isSynthetic(constructor)) { 205 return constructor; 206 } 207 } 208 } 209 return null; 210 } 211 212 215 private boolean isMainMethod(ExecutableElement method) { 216 if (!method.getSimpleName().contentEquals("main")) { return false; 219 } 220 Set <Modifier> modifiers = method.getModifiers(); 222 if (!modifiers.contains(Modifier.PUBLIC) || !modifiers.contains(Modifier.STATIC)) { 223 return false; 224 } 225 if (TypeKind.VOID != method.getReturnType().getKind()) { 227 return false; 228 } 229 List <? extends VariableElement> params = method.getParameters(); 232 if (params.size() != 1) { 233 return false; 234 } 235 VariableElement param = params.get(0); TypeMirror paramType = param.asType(); 237 if (TypeKind.ARRAY != paramType.getKind()) { 239 return false; 240 } 241 ArrayType arrayType = (ArrayType) paramType; 242 TypeElement stringTypeElement = controller.getElements().getTypeElement(String .class.getName()); 243 if (!controller.getTypes().isSameType(stringTypeElement.asType(), arrayType.getComponentType())) { 245 return false; 246 } 247 return true; 248 } 249 250 } 252 | Popular Tags |