/** * Returns the javac tree representing the source file. * @return {@link CompilationUnitTree} the compilation unit cantaining the top level classes contained in the, * java source file. * @throws java.lang.IllegalStateException when the phase is less than {@link JavaSource.Phase#PARSED} */ public CompilationUnitTree getCompilationUnit() { if (this.jfo == null) { throw new IllegalStateException (); } if (this.phase.compareTo (JavaSource.Phase.PARSED) < 0) throw new IllegalStateException("Cannot call getCompilationUnit() if current phase < JavaSource.Phase.PARSED. You must call toPhase(Phase.PARSED) first.");//NOI18N return this.compilationUnit; }
if (info.getPhase().compareTo(Phase.ELEMENTS_RESOLVED) >= 0) { String fqn = ((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName().toString(); Iterable<? extends Processor> processors = info.impl.getJavacTask().getProcessors();
private void computeImports(CompilationController ci, List<String> imports) throws IOException { if (ci.toPhase(Phase.PARSED).compareTo(Phase.PARSED) < 0) { ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+ "\nDiagnostics = "+ci.getDiagnostics()+ "\nFree memory = "+Runtime.getRuntime().freeMemory()); return; } List importDecl = ci.getCompilationUnit().getImports(); int i = 0; for (Iterator it = importDecl.iterator(); it.hasNext(); i++) { ImportTree itree = (ImportTree) it.next(); String importStr = itree.getQualifiedIdentifier().toString(); imports.add(importStr); } }
@Override public void run(CompilationController ci) throws Exception { if (ci.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {//TODO: ELEMENTS_RESOLVED may be sufficient ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+
return; if (ci.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {//TODO: ELEMENTS_RESOLVED may be sufficient ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+
if (info.getPhase().compareTo(Phase.RESOLVED) < 0) throw new IllegalArgumentException("Not in correct Phase. Required: Phase.RESOLVED, got: Phase." + info.getPhase().toString());
if (currentPhase.compareTo(requiredPhase)<0) { ciImpl.setPhase(requiredPhase);
return; if (ci.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {//TODO: ELEMENTS_RESOLVED may be sufficient ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+
/** * Returns tree which was reparsed by an incremental reparse. * When the source file wasn't parsed yet or the parse was a full parse * this method returns null. * <p class="nonnormative"> * Currently the leaf tree is a MethodTree but this may change in the future. * Client of this method is responsible to check the corresponding TreeKind * to find out if it may perform on the changed subtree or it needs to * reprocess the whole tree. * </p> * @return {@link TreePath} or null * @since 0.31 */ public @CheckForNull @CheckReturnValue TreePath getChangedTree () { checkConfinement(); if (JavaSource.Phase.PARSED.compareTo (impl.getPhase())>0) { return null; } final Pair<DocPositionRegion,MethodTree> changedTree = impl.getChangedTree(); if (changedTree == null) { return null; } final CompilationUnitTree cu = impl.getCompilationUnit(); if (cu == null) { return null; } return TreePath.getPath(cu, changedTree.second()); }
public void run(CompilationController control) throws Exception { if (JavaSource.Phase.ELEMENTS_RESOLVED.compareTo(control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED))<=0) { Elements elements = control.getElements(); Trees trees = control.getTrees(); Types types = control.getTypes(); TypeElement applet = elements.getTypeElement("java.applet.Applet"); //NOI18N TypeElement japplet = elements.getTypeElement("javax.swing.JApplet"); //NOI18N CompilationUnitTree cu = control.getCompilationUnit(); List<? extends Tree> topLevels = cu.getTypeDecls(); for (Tree topLevel : topLevels) { if (topLevel.getKind() == Tree.Kind.CLASS) { TypeElement type = (TypeElement) trees.getElement(TreePath.getPath(cu, topLevel)); if (type != null) { Set<Modifier> modifiers = type.getModifiers(); if (modifiers.contains(Modifier.PUBLIC) && ((applet != null && types.isSubtype(type.asType(), applet.asType())) || (japplet != null && types.isSubtype(type.asType(), japplet.asType())))) { result[0] = true; break; } } } } } }
private MethodArgument[] computeMethodArguments(CompilationController ci, Operation operation) throws IOException { MethodArgument args[]; if (ci.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+ "\nDiagnostics = "+ci.getDiagnostics()+ "\nFree memory = "+Runtime.getRuntime().freeMemory()); return null; } int offset = operation.getMethodEndPosition().getOffset(); Scope scope = ci.getTreeUtilities().scopeFor(offset); Element method = scope.getEnclosingMethod(); if (method == null) { return null; } Tree methodTree = ci.getTrees().getTree(method); CompilationUnitTree cu = ci.getCompilationUnit(); MethodArgumentsScanner scanner = new MethodArgumentsScanner(offset, cu, ci.getTrees().getSourcePositions(), true, new OperationCreationDelegateImpl()); args = methodTree.accept(scanner, null); args = scanner.getArguments(); return args; }
@Override public void run(final CompilationController control) throws Exception { if (control.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED).compareTo (JavaSource.Phase.ELEMENTS_RESOLVED)>=0) { final List<TypeElement> types = new ArrayList<>(); final ElementScanner6<Void,Void> visitor = new ElementScanner6<Void, Void>() { @Override public Void visitType(TypeElement e, Void p) { if (e.getEnclosingElement().getKind() == ElementKind.PACKAGE || e.getModifiers().contains(Modifier.STATIC)) { types.add(e); return super.visitType(e, p); } else { return null; } } }; visitor.scan(control.getTopLevelElements(), null); for (TypeElement type : types) { for (ExecutableElement exec : ElementFilter.methodsIn(control.getElements().getAllMembers(type))) { if (SourceUtils.isMainMethod(exec)) { result.add (ElementHandle.create(type)); } } } } }
@Override public void run (final CompilationController controller) { try { if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED)<0) { return; } try { final SourceAnalyzerFactory.SimpleAnalyzer sa = SourceAnalyzerFactory.createSimpleAnalyzer(); dataHolder[0] = sa.analyseUnit( controller.getCompilationUnit(), JavaSourceAccessor.getINSTANCE().getJavacTask(controller)); } catch (IllegalArgumentException ia) { //Debug info for issue #187344 //seems that invalid dirty class index is used final ClassPath scp = controller.getClasspathInfo().getClassPath(PathKind.SOURCE); throw new IllegalArgumentException( String.format("Provided source path: %s root: %s", //NOI18N scp == null ? "<null>" : scp.toString(), //NOI18N root.toExternalForm()), ia); } } catch (IOException ioe) { Exceptions.printStackTrace(ioe); } } }, true);
private MethodArgument[] computeMethodArguments(CompilationController ci, int methodLineNumber, int offset) throws IOException { MethodArgument args[]; if (ci.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { ErrorManager.getDefault().log(ErrorManager.WARNING, "Unable to resolve "+ci.getFileObject()+" to phase "+Phase.RESOLVED+", current phase = "+ci.getPhase()+ "\nDiagnostics = "+ci.getDiagnostics()+ "\nFree memory = "+Runtime.getRuntime().freeMemory()); return null; } Scope scope = ci.getTreeUtilities().scopeFor(offset); Element clazz = scope.getEnclosingClass(); if (clazz == null) { return null; } Tree methodTree = ci.getTrees().getTree(clazz); CompilationUnitTree cu = ci.getCompilationUnit(); MethodArgumentsScanner scanner = new MethodArgumentsScanner(methodLineNumber, cu, ci.getTrees().getSourcePositions(), false, new OperationCreationDelegateImpl()); args = methodTree.accept(scanner, null); args = scanner.getArguments(); return args; }
@Override public void run(ResultIterator resultIterator) throws Exception { Result res = resultIterator.getParserResult(offset); if (res == null) { return; } CompilationController controller = CompilationController.get(res); if (controller == null || controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { return; } TreePath path = controller.getTreeUtilities().pathFor(offset); javax.lang.model.element.Element elem = controller.getTrees().getElement(path); ElementKind kind = elem.getKind(); if (kind == ElementKind.CLASS || kind == ElementKind.ENUM || kind == ElementKind.ANNOTATION_TYPE) { result[0] = elem.asType().toString(); } } });
@Override public void run(ResultIterator resultIterator) throws Exception { Result res = resultIterator.getParserResult(offset); if (res == null) { return; } CompilationController controller = CompilationController.get(res); if (controller == null || controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { return; } TreePath path = controller.getTreeUtilities().pathFor(offset); javax.lang.model.element.Element elem = controller.getTrees().getElement(path); ElementKind kind = elem.getKind(); if (kind == ElementKind.CLASS || kind == ElementKind.ENUM || kind == ElementKind.ANNOTATION_TYPE) { result[0] = elem.asType().toString(); } } });
public void run(CompilationController cc) throws Exception { if (cancel.get()) return; HintsSettings.setPreferencesOverride(preferencesOverlay); DataObject d = DataObject.find(cc.getFileObject()); EditorCookie ec = d.getLookup().lookup(EditorCookie.class); Document doc = ec.openDocument(); try { handle.progress(FileUtil.getFileDisplayName(cc.getFileObject())); if (cc.toPhase(JavaSource.Phase.RESOLVED).compareTo(JavaSource.Phase.RESOLVED) < 0) { return; } handle.progress(f.incrementAndGet()); eds.addAll(new HintsTask().computeHints(cc)); } finally { HintsSettings.setPreferencesOverride(null); } } }, true);
private static TypeElement getToplevelClass(final CompilationController controller) throws IOException { // Controller has to be in some advanced phase, otherwise controller.getCompilationUnit() == null if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { return null; } TreePathScanner<TypeElement, Void> scanner = new TreePathScanner<TypeElement, Void>() { public TypeElement visitClass(ClassTree node, Void p) { try { return (TypeElement)controller.getTrees().getElement(getCurrentPath()); } catch (NullPointerException e) { Exceptions.printStackTrace(e); return null; } } }; return scanner.scan(controller.getCompilationUnit(), null); } }
@Override public @NonNull JavaSource.Phase toPhase(@NonNull JavaSource.Phase phase) throws IOException { //checkConfinement() called by super JavaSource.Phase result = super.toPhase(phase); if (result.compareTo(JavaSource.Phase.PARSED) >= 0) { init(); } return result; }