static JavaSource getJavaSourceFromClassName(Project p, String qualifiedClassName) throws IOException { FileObject fo = getFileObjectFromClassName(p, qualifiedClassName); if (fo != null) { return JavaSource.forFileObject(fo); } else { return null; } }
public static Object execute(Write task, FileObject fObj) { JavaSource js = JavaSource.forFileObject(fObj); return execute(task, js); }
T execute() throws IOException { JavaSource.forFileObject(sourceFile).runUserActionTask(this, true); return result; }
public static List<String> getTopLevelClassNames(FileObject fo) throws IOException { JavaSource javaSource = JavaSource.forFileObject(fo); if (javaSource == null) { return Collections.emptyList(); } final List<String> result = new ArrayList<String>(1); javaSource.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController cc) throws IOException { cc.toPhase(Phase.ELEMENTS_RESOLVED); for (TypeElement typeElement : cc.getTopLevelElements()) { result.add(ElementUtilities.getBinaryName(typeElement)); } } }, true); return result; }
@Override public List<SelectorNode> buildSelectionTree() { final SelectorNode[] classNode = new SelectorNode[1]; JavaSource js = JavaSource.forFileObject(file); if (js != null) { try { js.runWhenScanFinished(new Task<CompilationController>() { public void run(CompilationController controller) throws Exception { TypeElement classElement = getToplevelClass(controller); classNode[0] = new JavaClassNode(controller.getClasspathInfo(), classElement, null); } }, true); } catch (IOException e) { e.printStackTrace(); } } return classNode[0] != null ? Collections.singletonList(classNode[0]) : Collections.EMPTY_LIST; }
public static Object execute(Read task, FileObject fObj, boolean shared) { ReadTaskWrapper taskWrapper = new ReadTaskWrapper(task); JavaSource js = JavaSource.forFileObject(fObj); try { js.runUserActionTask(taskWrapper, shared); } catch (IOException ex) { ex.printStackTrace(); } return taskWrapper.result; }
private ElementHandle<TypeElement> getType( FileObject fileObject , final ElementKind kind ) throws IOException { JavaSource javaSource = JavaSource.forFileObject(fileObject); final List<ElementHandle<TypeElement>> result = new ArrayList<ElementHandle<TypeElement>>(1); javaSource.runUserActionTask( new Task<CompilationController>() { @Override public void run( CompilationController controller ) throws Exception { controller.toPhase(JavaSource.Phase.RESOLVED); String typeName = controller.getFileObject().getName(); List<? extends TypeElement> topLevelElements = controller.getTopLevelElements(); for (TypeElement typeElement : topLevelElements) { if ( kind == typeElement.getKind() && typeName.contentEquals( typeElement.getSimpleName())) { result.add(ElementHandle.create( typeElement)); return; } } } },true); return result.get(0); }
private void writeDD(FileObject referencingFile, final String referencingClassName) throws IOException { final AppClientProvider jp = webProject.getLookup().lookup(AppClientProvider.class); JavaSource javaSource = JavaSource.forFileObject(referencingFile); javaSource.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController controller) throws Exception { TypeElement typeElement = controller.getElements().getTypeElement(referencingClassName); if (isDescriptorMandatory(jp.getJ2eeProfile()) || !InjectionTargetQuery.isInjectionTarget(controller, typeElement)) { FileObject fo = jp.getDeploymentDescriptor(); getAppClient().write(fo); } } }, true); }
private static TreePathHandle resolveHandle(FileObject fileObject){ final TreePathHandle[] result = new TreePathHandle[1]; if (!isJavaFile(fileObject)){ return null; } JavaSource source = JavaSource.forFileObject(fileObject); try { source.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController co) throws Exception { co.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = co.getCompilationUnit(); if (cut.getTypeDecls().isEmpty()) { return; } result[0] = TreePathHandle.create(TreePath.getPath(cut, cut.getTypeDecls().get(0)), co); } }, true); } catch (IOException ioe) { Exceptions.printStackTrace(ioe); } return result[0]; }
@Override public void renameComponent(FormModel formModel, String currentName, String newName) { FormDataObject formDO = FormEditor.getFormDataObject(formModel); JavaSource js = JavaSource.forFileObject(formDO.getPrimaryFile()); MemberVisitor scanner = new MemberVisitor(currentName, true); //privateField); try { js.runUserActionTask(scanner, true); doRenameRefactoring(formDO, newName, scanner.getHandle()); } catch (IOException e) { Logger.getLogger(RADComponentRenameRefactoringSupport.class.getName()).log(Level.SEVERE, e.getMessage(), e); } }
/** */ private static int[] getPositionRange( FileObject fileObj, ElementHandle<Element> elemHandle) throws IOException { PositionRangeFinder posFinder = new PositionRangeFinder(fileObj, elemHandle); JavaSource.forFileObject(fileObj).runUserActionTask(posFinder, true); return posFinder.getPositionRange(); }
@Override protected long createTaggedCompilationController(FileObject file, long currenTag, Object[] out) throws IOException { assert file != null; final JavaSource js = JavaSource.forFileObject(file); if (js == null) { throw new FileNotFoundException( String.format("No java source for %s, exists: %b, file: %b", //NOI18N FileUtil.getFileDisplayName(file), file.isValid(), file.isData() )); } return JavaSourceAccessor.getINSTANCE().createTaggedCompilationController(js, currenTag, out); }
try{ JavaSource source = JavaSource.forFileObject(sourceFO);
/** * Returns a {@link JavaSource} instance associated to the given {@link javax.swing.Document}, * it returns null if the {@link Document} is not * associated with data type providing the {@link JavaSource}. * @param doc {@link Document} for which the {@link JavaSource} should be found/created. * @return {@link JavaSource} or null * @throws IllegalArgumentException if doc is null */ public static @CheckForNull JavaSource forDocument(@NonNull Document doc) throws IllegalArgumentException { if (doc == null) { throw new IllegalArgumentException ("doc == null"); //NOI18N } Reference<?> ref = (Reference<?>) doc.getProperty(JavaSource.class); JavaSource js = ref != null ? (JavaSource) ref.get() : null; if (js == null) { FileObject fo = Utilities.getFileObject(doc); if (fo != null) { js = forFileObject(fo); } } return js; }
private String getRawCommentFor(Element element) { try { FileObject fo = SourceUtils.getFile(element, cpInfo); if (fo != null) { JavaSource js = JavaSource.forFileObject(fo); if (js != null) { final String[] ret = new String[1]; final ElementHandle<? extends Element> handle = ElementHandle.create(element); js.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController controller) throws Exception { controller.toPhase(Phase.ELEMENTS_RESOLVED); Element e = handle.resolve(controller); if (e != null) ret[0] = controller.getElements().getDocComment(e); } },true); return ret[0] != null ? ret[0] : ""; //NOI18N } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } return ""; //NOI18N }
private boolean debugEmptyMapping( FileObject javaFile ){ JavaSource js = JavaSource.forFileObject(javaFile); if (isWebService(js)) { //cannot debug web service implementation file String mes = java.text.MessageFormat.format( NbBundle.getMessage(WebActionProvider.class, "TXT_cannotDebugWebservice"), new Object[]{javaFile.getName()}); // NOI18N NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(desc); return true; } else { String mes = java.text.MessageFormat.format( NbBundle.getMessage(WebActionProvider.class, "TXT_missingServletMappings"), new Object[]{javaFile.getName()}); // NOI18N NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(desc); return true; } }
public void invokeAction(String command, Lookup context) throws IllegalArgumentException { TestNGSupport.Action action = SingleMethod.COMMAND_RUN_SINGLE_METHOD.equals(command) ? TestNGSupport.Action.RUN_TESTMETHOD : TestNGSupport.Action.DEBUG_TESTMETHOD; SingleMethod sm = context.lookup(SingleMethod.class); FileObject fo = sm.getFile(); TestClassInfoTask task = new TestClassInfoTask(0); JavaSource js = JavaSource.forFileObject(fo); try { js.runUserActionTask(task, true); } catch (IOException ex) { LOGGER.log(Level.WARNING, null, ex); } Project p = FileOwnerQuery.getOwner(fo); TestNGSupportImplementation.TestExecutor exec = TestNGSupport.findTestNGSupport(p).createExecutor(p); TestConfig conf = TestNGUtils.getTestConfig(fo, false, task.getPackageName(), task.getClassName(), sm.getMethodName()); try { exec.execute(action, conf); } catch (IOException ex) { LOGGER.log(Level.SEVERE, null, ex); } } }
private Problem prepareClassRefactoring(RefactoringElementsBag refactoringElementsBag, final TreePathHandle treePathHandle) { FileObject fo = treePathHandle.getFileObject(); SpringScope scope = SpringScope.getSpringScope(fo); if (scope == null) { return null; } try { JavaSource source = JavaSource.forFileObject(fo); final String[] className = new String[] { null }; source.runUserActionTask(new Task<CompilationController>() { public void run(CompilationController compilationController) throws Exception { compilationController.toPhase(JavaSource.Phase.RESOLVED); TypeElement type = (TypeElement) treePathHandle.resolveElement(compilationController); if (type != null) { className[0] = ElementUtilities.getBinaryName(type); } } }, true); if (className[0] != null) { for (Occurrences.Occurrence item : Occurrences.getJavaClassOccurrences(className[0], scope)) { refactoringElementsBag.add(refactoring, SpringRefactoringElement.create(item)); } } } catch (IOException e) { Exceptions.printStackTrace(e); } return null; }
private void writeDD(FileObject referencingFile, final String referencingClass) throws IOException { ProjectWebModule jp = webProject.getLookup().lookup(ProjectWebModule.class); // test if referencing class is injection target final boolean[] isInjectionTarget = {false}; CancellableTask<CompilationController> task = new CancellableTask<CompilationController>() { @Override public void run(CompilationController controller) throws IOException { Elements elements = controller.getElements(); TypeElement thisElement = elements.getTypeElement(referencingClass); if (thisElement!=null) { isInjectionTarget[0] = InjectionTargetQuery.isInjectionTarget(controller, thisElement); } } @Override public void cancel() {} }; JavaSource refFile = JavaSource.forFileObject(referencingFile); if (refFile!=null) { refFile.runUserActionTask(task, true); } boolean shouldWrite = isDescriptorMandatory(jp.getJ2eeProfile()) || !isInjectionTarget[0]; if (shouldWrite) { FileObject fo = jp.getDeploymentDescriptor(); getWebApp().write(fo); } }
private Problem prepareMethodRefactoring(RefactoringElementsBag refactoringElements, final TreePathHandle treePathHandle) { FileObject fo = treePathHandle.getFileObject(); try { RenamedProperty prop = null; JavaSource js = JavaSource.forFileObject(fo); if (js != null) { prop = SpringRefactorings.getRenamedProperty(treePathHandle, js, null); } SpringScope scope = SpringScope.getSpringScope(fo); if (scope == null) { return null; } if (prop != null) { String oldName = prop.getOldName(); if (oldName != null) { for (Occurrence occurrence : Occurrences.getPropertyOccurrences(prop, js, scope)) { refactoringElements.add(refactoring, SpringRefactoringElement.create(occurrence)); } } } } catch (IOException ex) { Exceptions.printStackTrace(ex); } return null; } }