private List<HighlightInfo> getHighlights() { if (myReadAccessRanges.isEmpty() && myWriteAccessRanges.isEmpty()) { return Collections.emptyList(); } Set<Pair<Object, TextRange>> existingMarkupTooltips = new HashSet<Pair<Object, TextRange>>(); for (RangeHighlighter highlighter : myEditor.getMarkupModel().getAllHighlighters()) { existingMarkupTooltips.add( Pair.create( highlighter.getErrorStripeTooltip(), new TextRange(highlighter.getStartOffset(), highlighter.getEndOffset()))); } List<HighlightInfo> result = new ArrayList<HighlightInfo>(myReadAccessRanges.size() + myWriteAccessRanges.size()); for (TextRange range : myReadAccessRanges) { ContainerUtil.addIfNotNull( createHighlightInfo( range, HighlightInfoType.ELEMENT_UNDER_CARET_READ, existingMarkupTooltips), result); } for (TextRange range : myWriteAccessRanges) { ContainerUtil.addIfNotNull( createHighlightInfo( range, HighlightInfoType.ELEMENT_UNDER_CARET_WRITE, existingMarkupTooltips), result); } return result; }
@NotNull private static String toVfString(@NotNull Collection<VirtualFile> list) { List<VirtualFile> sub = new ArrayList<VirtualFile>(list).subList(0, Math.min(list.size(), 100)); return list.size() + " files: " + StringUtil.join(sub, file -> file.getName(), ", ") + (list.size() == sub.size() ? "" : "..."); }
@NotNull private static PsiSubstitutor replaceVariables(Collection<InferenceVariable> inferenceVariables) { final List<InferenceVariable> targetVars = new ArrayList<InferenceVariable>(); PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; final InferenceVariable[] oldVars = inferenceVariables.toArray(new InferenceVariable[inferenceVariables.size()]); for (InferenceVariable variable : oldVars) { final InferenceVariable newVariable = new InferenceVariable( variable.getCallContext(), variable.getParameter(), variable.getName()); substitutor = substitutor.put( variable, JavaPsiFacade.getElementFactory(variable.getProject()).createType(newVariable)); targetVars.add(newVariable); if (variable.isThrownBound()) { newVariable.setThrownBound(); } } for (int i = 0; i < targetVars.size(); i++) { InferenceVariable var = targetVars.get(i); for (InferenceBound boundType : InferenceBound.values()) { for (PsiType bound : oldVars[i].getBounds(boundType)) { var.addBound(substitutor.substitute(bound), boundType, null); } } } return substitutor; }
@Nullable public static PsiClass[] getAllTestClasses(final TestClassFilter filter, boolean sync) { final PsiClass[][] holder = new PsiClass[1][]; final Runnable process = () -> { final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator(); final Collection<PsiClass> set = new LinkedHashSet<>(); final PsiManager manager = PsiManager.getInstance(filter.getProject()); final GlobalSearchScope projectScope = GlobalSearchScope.projectScope(manager.getProject()); final GlobalSearchScope scope = projectScope.intersectWith(filter.getScope()); for (final PsiClass psiClass : AllClassesSearch.search(scope, manager.getProject())) { if (filter.isAccepted(psiClass)) { if (indicator != null) { indicator.setText2( "Found test class " + ReadAction.compute(psiClass::getQualifiedName)); } set.add(psiClass); } } holder[0] = set.toArray(new PsiClass[set.size()]); }; if (sync) { ProgressManager.getInstance() .runProcessWithProgressSynchronously( process, "Searching For Tests...", true, filter.getProject()); } else { process.run(); } return holder[0]; }
private static void logStats(Collection<PsiFile> otherFiles, long start) { long time = System.currentTimeMillis() - start; final Multiset<String> stats = HashMultiset.create(); for (PsiFile file : otherFiles) { stats.add( StringUtil.notNullize(file.getViewProvider().getVirtualFile().getExtension()) .toLowerCase()); } List<String> extensions = ContainerUtil.newArrayList(stats.elementSet()); Collections.sort( extensions, new Comparator<String>() { @Override public int compare(String o1, String o2) { return stats.count(o2) - stats.count(o1); } }); String message = "Search in " + otherFiles.size() + " files with unknown types took " + time + "ms.\n" + "Mapping their extensions to an existing file type (e.g. Plain Text) might speed up the search.\n" + "Most frequent non-indexed file extensions: "; for (int i = 0; i < Math.min(10, extensions.size()); i++) { String extension = extensions.get(i); message += extension + "(" + stats.count(extension) + ") "; } LOG.info(message); }
private static void copyClassesImpl( final String copyClassName, final Project project, final Map<PsiFile, PsiClass[]> classes, final HashMap<PsiFile, String> map, final Object targetDirectory, final PsiDirectory defaultTargetDirectory, final String commandName, final boolean selectInActivePanel, final boolean openInEditor) { final boolean[] result = new boolean[] {false}; Runnable command = () -> { final Runnable action = () -> { try { PsiDirectory target; if (targetDirectory instanceof PsiDirectory) { target = (PsiDirectory) targetDirectory; } else { target = ((MoveDestination) targetDirectory) .getTargetDirectory(defaultTargetDirectory); } Collection<PsiFile> files = doCopyClasses(classes, map, copyClassName, target, project); if (files != null) { if (openInEditor) { for (PsiFile file : files) { CopyHandler.updateSelectionInActiveProjectView( file, project, selectInActivePanel); } EditorHelper.openFilesInEditor(files.toArray(new PsiFile[files.size()])); } result[0] = true; } } catch (final IncorrectOperationException ex) { ApplicationManager.getApplication() .invokeLater( () -> Messages.showMessageDialog( project, ex.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon())); } }; ApplicationManager.getApplication().runWriteAction(action); }; CommandProcessor processor = CommandProcessor.getInstance(); processor.executeCommand(project, command, commandName, null); if (result[0]) { ToolWindowManager.getInstance(project) .invokeLater(() -> ToolWindowManager.getInstance(project).activateEditorComponent()); } }
@Nullable public static PhpClass getClassInterface(Project project, @NotNull String className) { // api workaround for at least interfaces if (!className.startsWith("\\")) { className = "\\" + className; } Collection<PhpClass> phpClasses = PhpIndex.getInstance(project).getAnyByFQN(className); return phpClasses.size() == 0 ? null : phpClasses.iterator().next(); }
public static PsiElement[] getPsiElementsBySignature( Project project, @Nullable String signature) { if (signature == null) { return new PsiElement[0]; } Collection<? extends PhpNamedElement> phpNamedElementCollections = PhpIndex.getInstance(project).getBySignature(signature, null, 0); return phpNamedElementCollections.toArray(new PsiElement[phpNamedElementCollections.size()]); }
@NotNull public ResolveResult[] multiResolve(boolean incompleteCode) { Collection<PsiMethod> resMethods = StripesReferenceUtil.getResolutionMethodsAsList(actionBeanClass); ResolveResult[] retval = new ResolveResult[resMethods.size()]; int i = 0; for (PsiMethod method : resMethods) { retval[i++] = new PsiElementResolveResult(method); } return retval; }
public void findUsages( @NotNull final Processor<UsageInfo> consumer, @NotNull FindUsagesProcessPresentation processPresentation) { try { myProgress.setIndeterminate(true); myProgress.setText("Scanning indexed files..."); final Set<PsiFile> filesForFastWordSearch = ApplicationManager.getApplication() .runReadAction( new Computable<Set<PsiFile>>() { @Override public Set<PsiFile> compute() { return getFilesForFastWordSearch(); } }); myProgress.setIndeterminate(false); searchInFiles(filesForFastWordSearch, processPresentation, consumer); myProgress.setIndeterminate(true); myProgress.setText("Scanning non-indexed files..."); boolean skipIndexed = canRelyOnIndices(); final Collection<PsiFile> otherFiles = collectFilesInScope(filesForFastWordSearch, skipIndexed); myProgress.setIndeterminate(false); long start = System.currentTimeMillis(); searchInFiles(otherFiles, processPresentation, consumer); if (skipIndexed && otherFiles.size() > 1000) { logStats(otherFiles, start); } } catch (ProcessCanceledException e) { // fine } if (!myLargeFiles.isEmpty()) { processPresentation.setLargeFilesWereNotScanned(myLargeFiles); } if (!myProgress.isCanceled()) { myProgress.setText(FindBundle.message("find.progress.search.completed")); } }
@Override @NotNull public String getText() { @NonNls String message; switch (myFixType) { case MAKE_FINAL: message = "make.final.text"; break; case MAKE_ARRAY: message = "make.final.transform.to.one.element.array"; break; case COPY_TO_FINAL: return QuickFixBundle.message("make.final.copy.to.temp", myVariable.getName()); default: return ""; } Collection<PsiVariable> vars = getVariablesToFix(); String varNames = vars.size() == 1 ? "'" + myVariable.getName() + "'" : "variables"; return QuickFixBundle.message(message, varNames); }
@Override public boolean queue(@NotNull Collection<VirtualFile> files, @NotNull Object reason) { if (files.isEmpty()) { return false; } boolean queued = false; List<VirtualFile> added = new ArrayList<VirtualFile>(files.size()); for (VirtualFile file : files) { boolean wasAdded = queueIfNeeded(file, myProject); if (wasAdded) { added.add(file); } queued |= wasAdded; } if (queued) { log("Queued to resolve (from " + reason + "): " + toVfString(added)); flushLog(); } return queued; }
public void testLeafExpressionsMoreComplex() throws Exception { SliceTreeStructure treeStructure = configureTree("Duplicate"); SliceNode root = (SliceNode) treeStructure.getRootElement(); Collection<PsiElement> leaves = SliceLeafAnalyzer.calcLeafExpressions(root, treeStructure, SliceLeafAnalyzer.createMap()); assertNotNull(leaves); assertEquals(2, leaves.size()); List<PsiElement> list = new ArrayList<PsiElement>(leaves); Collections.sort( list, new Comparator<PsiElement>() { @Override public int compare(PsiElement o1, PsiElement o2) { return o1.getText().compareTo(o2.getText()); } }); assertTrue(list.get(0) instanceof PsiLiteralExpression); assertEquals(false, ((PsiLiteral) list.get(0)).getValue()); assertTrue(list.get(1) instanceof PsiLiteralExpression); assertEquals(true, ((PsiLiteral) list.get(1)).getValue()); }
private void validateEntryPoints() { long count = PsiManager.getInstance(myProject).getModificationTracker().getModificationCount(); if (count != myLastModificationCount) { myLastModificationCount = count; Collection<SmartRefElementPointer> collection = myPersistentEntryPoints.values(); SmartRefElementPointer[] entries = collection.toArray(new SmartRefElementPointer[collection.size()]); for (SmartRefElementPointer entry : entries) { RefElement refElement = (RefElement) entry.getRefElement(); if (refElement != null && !refElement.isValid()) { myPersistentEntryPoints.remove(entry.getFQName()); } } final Iterator<RefElement> it = myTemporaryEntryPoints.iterator(); while (it.hasNext()) { RefElement refElement = it.next(); if (!refElement.isValid()) { it.remove(); } } } }
@NotNull private List<Pair<PsiClass, VirtualFile>> doFindClasses( @NotNull String qName, @NotNull final GlobalSearchScope scope) { final Collection<PsiClass> classes = JavaFullClassNameIndex.getInstance().get(qName.hashCode(), myManager.getProject(), scope); if (classes.isEmpty()) return Collections.emptyList(); List<Pair<PsiClass, VirtualFile>> result = new ArrayList<>(classes.size()); for (PsiClass aClass : classes) { final String qualifiedName = aClass.getQualifiedName(); if (qualifiedName == null || !qualifiedName.equals(qName)) continue; PsiFile file = aClass.getContainingFile(); if (file == null) { throw new AssertionError("No file for class: " + aClass + " of " + aClass.getClass()); } final boolean valid = file.isValid(); VirtualFile vFile = file.getVirtualFile(); if (!valid) { LOG.error( "Invalid file " + file + "; virtualFile:" + vFile + (vFile != null && !vFile.isValid() ? " (invalid)" : "") + "; id=" + (vFile == null ? 0 : ((VirtualFileWithId) vFile).getId()), new PsiInvalidElementAccessException(aClass)); continue; } if (!hasAcceptablePackage(vFile)) continue; result.add(Pair.create(aClass, vFile)); } return result; }
public void testGroupByValuesCorrectLeaves() throws Exception { SliceTreeStructure treeStructure = configureTree("DuplicateLeaves"); SliceRootNode root = (SliceRootNode) treeStructure.getRootElement(); Map<SliceNode, Collection<PsiElement>> map = SliceLeafAnalyzer.createMap(); Collection<PsiElement> leaves = SliceLeafAnalyzer.calcLeafExpressions(root, treeStructure, map); assertNotNull(leaves); assertEquals(1, leaves.size()); PsiElement leaf = leaves.iterator().next(); assertTrue(leaf instanceof PsiLiteralExpression); assertEquals("\"oo\"", leaf.getText()); SliceRootNode newRoot = SliceLeafAnalyzer.createTreeGroupedByValues(leaves, root, map); Collection<? extends AbstractTreeNode> children = newRoot.getChildren(); assertEquals(1, children.size()); SliceNode child = (SliceNode) children.iterator().next(); assertTrue(child instanceof SliceLeafValueRootNode); children = child.getChildren(); assertEquals(1, children.size()); child = (SliceNode) children.iterator().next(); assertTrue(child.getValue().getElement() instanceof PsiField); children = child.getChildren(); assertEquals(1, children.size()); child = (SliceNode) children.iterator().next(); assertTrue(child.getValue().getElement() instanceof PsiReferenceExpression); children = child.getChildren(); assertEquals(1, children.size()); child = (SliceNode) children.iterator().next(); assertTrue(child.getValue().getElement() instanceof PsiReferenceExpression); children = child.getChildren(); assertEquals(1, children.size()); child = (SliceNode) children.iterator().next(); assertTrue(child.getValue().getElement() instanceof PsiLiteralExpression); assertEquals(child.getValue().getElement(), leaf); }
public void performRefactoring(UsageInfo[] usages) { final int[] choice = myAllRenames.size() > 1 ? new int[] {-1} : null; String message = null; try { for (Iterator<Map.Entry<PsiElement, String>> iterator = myAllRenames.entrySet().iterator(); iterator.hasNext(); ) { Map.Entry<PsiElement, String> entry = iterator.next(); if (entry.getKey() instanceof PsiFile) { final PsiFile file = (PsiFile) entry.getKey(); final PsiDirectory containingDirectory = file.getContainingDirectory(); if (CopyFilesOrDirectoriesHandler.checkFileExist( containingDirectory, choice, file, entry.getValue(), "Rename")) { iterator.remove(); continue; } } RenameUtil.checkRename(entry.getKey(), entry.getValue()); } } catch (IncorrectOperationException e) { message = e.getMessage(); } if (message != null) { CommonRefactoringUtil.showErrorMessage( RefactoringBundle.message("rename.title"), message, getHelpID(), myProject); return; } List<Runnable> postRenameCallbacks = new ArrayList<Runnable>(); final MultiMap<PsiElement, UsageInfo> classified = classifyUsages(myAllRenames.keySet(), usages); for (final PsiElement element : myAllRenames.keySet()) { String newName = myAllRenames.get(element); final RefactoringElementListener elementListener = getTransaction().getElementListener(element); final RenamePsiElementProcessor renamePsiElementProcessor = RenamePsiElementProcessor.forElement(element); Runnable postRenameCallback = renamePsiElementProcessor.getPostRenameCallback(element, newName, elementListener); final Collection<UsageInfo> infos = classified.get(element); try { RenameUtil.doRename( element, newName, infos.toArray(new UsageInfo[infos.size()]), myProject, elementListener); } catch (final IncorrectOperationException e) { RenameUtil.showErrorMessage(e, element, myProject); return; } if (postRenameCallback != null) { postRenameCallbacks.add(postRenameCallback); } } for (Runnable runnable : postRenameCallbacks) { runnable.run(); } List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<NonCodeUsageInfo>(); for (UsageInfo usage : usages) { if (usage instanceof NonCodeUsageInfo) { nonCodeUsages.add((NonCodeUsageInfo) usage); } } myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]); if (!mySkippedUsages.isEmpty()) { if (!ApplicationManager.getApplication().isUnitTestMode() && !ApplicationManager.getApplication().isHeadlessEnvironment()) { ApplicationManager.getApplication() .invokeLater( new Runnable() { public void run() { final IdeFrame ideFrame = WindowManager.getInstance().getIdeFrame(myProject); if (ideFrame != null) { StatusBarEx statusBar = (StatusBarEx) ideFrame.getStatusBar(); HyperlinkListener listener = new HyperlinkListener() { public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType() != HyperlinkEvent.EventType.ACTIVATED) return; Messages.showMessageDialog( "<html>" + StringUtil.join( mySkippedUsages, new Function<UnresolvableCollisionUsageInfo, String>() { public String fun( UnresolvableCollisionUsageInfo unresolvableCollisionUsageInfo) { return unresolvableCollisionUsageInfo .getDescription(); } }, "<br>") + "</html>", "Don't panic! They are safe to skip", null); } }; statusBar.notifyProgressByBalloon( MessageType.WARNING, "<html><body>Unable to rename certain usages. <a href=\"\">Browse</a></body></html>", null, listener); } } }, ModalityState.NON_MODAL); } } }
@Nullable public ResourceBundleEditorViewElement getSelectedElementIfOnlyOne() { final Collection<ResourceBundleEditorViewElement> selectedElements = getSelectedElements(); return selectedElements.size() == 1 ? ContainerUtil.getFirstItem(selectedElements) : null; }
@NotNull public Class classToClass(@NotNull PsiClass psiClass) { Set<String> modifiers = modifiersListToModifiersSet(psiClass.getModifierList()); List<Field> fields = fieldsToFieldList(psiClass.getFields(), psiClass); List<Element> typeParameters = elementsToElementList(psiClass.getTypeParameters()); List<Type> implementsTypes = typesToNotNullableTypeList(psiClass.getImplementsListTypes()); List<Type> extendsTypes = typesToNotNullableTypeList(psiClass.getExtendsListTypes()); IdentifierImpl name = new IdentifierImpl(psiClass.getName()); List<Expression> baseClassParams = new LinkedList<Expression>(); List<Member> members = getMembers(psiClass); // we try to find super() call and generate class declaration like that: class A(name: String, i // : Int) : Base(name) SuperVisitor visitor = new SuperVisitor(); psiClass.accept(visitor); Collection<PsiExpressionList> resolvedSuperCallParameters = visitor.getResolvedSuperCallParameters(); if (resolvedSuperCallParameters.size() == 1) { baseClassParams.addAll( expressionsToExpressionList( resolvedSuperCallParameters.toArray(new PsiExpressionList[1])[0].getExpressions())); } // we create primary constructor from all non final fields and fields without initializers if (!psiClass.isEnum() && !psiClass.isInterface() && psiClass.getConstructors().length > 1 && getPrimaryConstructorForThisCase(psiClass) == null) { List<Field> finalOrWithEmptyInitializer = getFinalOrWithEmptyInitializer(fields); Map<String, String> initializers = new HashMap<String, String>(); for (Member m : members) { // and modify secondaries if (m.getKind() == INode.Kind.CONSTRUCTOR) { Function f = (Function) m; if (!((Constructor) f).isPrimary()) { for (Field fo : finalOrWithEmptyInitializer) { String init = getDefaultInitializer(fo); initializers.put(fo.getIdentifier().toKotlin(), init); } List<Statement> newStatements = new LinkedList<Statement>(); for (Statement s : f.getBlock().getStatements()) { boolean isRemoved = false; if (s.getKind() == INode.Kind.ASSIGNMENT_EXPRESSION) { AssignmentExpression assignmentExpression = (AssignmentExpression) s; if (assignmentExpression.getLeft().getKind() == INode.Kind.CALL_CHAIN) { for (Field fo : finalOrWithEmptyInitializer) { String id = fo.getIdentifier().toKotlin(); if (((CallChainExpression) assignmentExpression.getLeft()) .getIdentifier() .toKotlin() .endsWith("." + id)) { initializers.put(id, assignmentExpression.getRight().toKotlin()); isRemoved = true; } } } } if (!isRemoved) { newStatements.add(s); } } newStatements.add( 0, new DummyStringExpression( "val __ = " + createPrimaryConstructorInvocation( name.toKotlin(), finalOrWithEmptyInitializer, initializers))); f.setBlock(new Block(newStatements)); } } } members.add( new Constructor( Identifier.EMPTY_IDENTIFIER, Collections.<String>emptySet(), new ClassType(name), Collections.<Element>emptyList(), new ParameterList(createParametersFromFields(finalOrWithEmptyInitializer)), new Block(createInitStatementsFromFields(finalOrWithEmptyInitializer)), true)); } if (psiClass.isInterface()) { return new Trait( this, name, modifiers, typeParameters, extendsTypes, Collections.<Expression>emptyList(), implementsTypes, members); } if (psiClass.isEnum()) { return new Enum( this, name, modifiers, typeParameters, Collections.<Type>emptyList(), Collections.<Expression>emptyList(), implementsTypes, members); } return new Class( this, name, modifiers, typeParameters, extendsTypes, baseClassParams, implementsTypes, members); }
private boolean hasMPSFacet(Module mod) { Collection<MPSFacet> mpsFacets = FacetManager.getInstance(mod).getFacetsByType(MPSFacetType.ID); return (mpsFacets != null && mpsFacets.size() > 0); }
private MPSFacet getMpsFacet(Module mod) { Collection<MPSFacet> mpsFacets = FacetManager.getInstance(mod).getFacetsByType(MPSFacetType.ID); if (mpsFacets != null && mpsFacets.size() > 0) return mpsFacets.iterator().next(); else return null; }
private void searchInFiles( @NotNull Collection<PsiFile> psiFiles, @NotNull FindUsagesProcessPresentation processPresentation, @NotNull final Processor<UsageInfo> consumer) { int i = 0; long totalFilesSize = 0; int count = 0; for (final PsiFile psiFile : psiFiles) { final VirtualFile virtualFile = psiFile.getVirtualFile(); final int index = i++; if (virtualFile == null) continue; long fileLength = UsageViewManagerImpl.getFileLength(virtualFile); if (fileLength == -1) continue; // Binary or invalid final boolean skipProjectFile = ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile) && !myFindModel.isSearchInProjectFiles(); if (skipProjectFile && !Registry.is("find.search.in.project.files")) continue; if (fileLength > SINGLE_FILE_SIZE_LIMIT) { myLargeFiles.add(psiFile); continue; } myProgress.checkCanceled(); myProgress.setFraction((double) index / psiFiles.size()); String text = FindBundle.message( "find.searching.for.string.in.file.progress", myFindModel.getStringToFind(), virtualFile.getPresentableUrl()); myProgress.setText(text); myProgress.setText2( FindBundle.message("find.searching.for.string.in.file.occurrences.progress", count)); int countInFile = FindInProjectUtil.processUsagesInFile( psiFile, myFindModel, new Processor<UsageInfo>() { @Override public boolean process(UsageInfo info) { return skipProjectFile || consumer.process(info); } }); if (countInFile > 0 && skipProjectFile) { processPresentation.projectFileUsagesFound( new Runnable() { @Override public void run() { FindModel model = myFindModel.clone(); model.setSearchInProjectFiles(true); FindInProjectManager.getInstance(myProject).startFindInProject(model); } }); continue; } count += countInFile; if (countInFile > 0) { totalFilesSize += fileLength; if (totalFilesSize > FILES_SIZE_LIMIT && !myWarningShown) { myWarningShown = true; String message = FindBundle.message( "find.excessive.total.size.prompt", UsageViewManagerImpl.presentableSize(totalFilesSize), ApplicationNamesInfo.getInstance().getProductName()); UsageLimitUtil.showAndCancelIfAborted( myProject, message, processPresentation.getUsageViewPresentation()); } } } }
public static PsiMethod[] findDeepestSuperMethods(PsiMethod method) { if (!canHaveSuperMethod(method, true, false)) return PsiMethod.EMPTY_ARRAY; Collection<PsiMethod> collection = DeepestSuperMethodsSearch.search(method).findAll(); return collection.toArray(new PsiMethod[collection.size()]); }
@Nullable public static PhpClass getFirstClassFromFile(PhpFile phpFile) { Collection<PhpClass> phpClasses = PsiTreeUtil.collectElementsOfType(phpFile, PhpClass.class); return phpClasses.size() == 0 ? null : phpClasses.iterator().next(); }
@NotNull final RunnerResult analyzeMethod( @NotNull PsiElement psiBlock, @NotNull InstructionVisitor visitor, boolean ignoreAssertions, @NotNull Collection<DfaMemoryState> initialStates) { if (PsiTreeUtil.findChildOfType(psiBlock, OuterLanguageElement.class) != null) return RunnerResult.NOT_APPLICABLE; try { final ControlFlow flow = new ControlFlowAnalyzer(myValueFactory, psiBlock, ignoreAssertions).buildControlFlow(); if (flow == null) return RunnerResult.NOT_APPLICABLE; int[] loopNumber = LoopAnalyzer.calcInLoop(flow); int endOffset = flow.getInstructionCount(); myInstructions = flow.getInstructions(); myNestedClosures.clear(); Set<Instruction> joinInstructions = ContainerUtil.newHashSet(); for (int index = 0; index < myInstructions.length; index++) { Instruction instruction = myInstructions[index]; if (instruction instanceof GotoInstruction) { joinInstructions.add(myInstructions[((GotoInstruction) instruction).getOffset()]); } else if (instruction instanceof ConditionalGotoInstruction) { joinInstructions.add( myInstructions[((ConditionalGotoInstruction) instruction).getOffset()]); } else if (instruction instanceof MethodCallInstruction && !((MethodCallInstruction) instruction).getContracts().isEmpty()) { joinInstructions.add(myInstructions[index + 1]); } } if (LOG.isDebugEnabled()) { LOG.debug("Analyzing code block: " + psiBlock.getText()); for (int i = 0; i < myInstructions.length; i++) { LOG.debug(i + ": " + myInstructions[i]); } } // for (int i = 0; i < myInstructions.length; i++) System.out.println(i + ": " + // myInstructions[i].toString()); Integer tooExpensiveHash = psiBlock.getUserData(TOO_EXPENSIVE_HASH); if (tooExpensiveHash != null && tooExpensiveHash == psiBlock.getText().hashCode()) { LOG.debug("Too complex because hasn't changed since being too complex already"); return RunnerResult.TOO_COMPLEX; } final StateQueue queue = new StateQueue(); for (final DfaMemoryState initialState : initialStates) { queue.offer(new DfaInstructionState(myInstructions[0], initialState)); } MultiMap<BranchingInstruction, DfaMemoryState> processedStates = MultiMap.createSet(); MultiMap<BranchingInstruction, DfaMemoryState> incomingStates = MultiMap.createSet(); long msLimit = shouldCheckTimeLimit() ? Registry.intValue("ide.dfa.time.limit.online") : Registry.intValue("ide.dfa.time.limit.offline"); WorkingTimeMeasurer measurer = new WorkingTimeMeasurer(msLimit * 1000 * 1000); int count = 0; while (!queue.isEmpty()) { List<DfaInstructionState> states = queue.getNextInstructionStates(joinInstructions); for (DfaInstructionState instructionState : states) { if (count++ % 1024 == 0 && measurer.isTimeOver()) { LOG.debug("Too complex because the analysis took too long"); psiBlock.putUserData(TOO_EXPENSIVE_HASH, psiBlock.getText().hashCode()); return RunnerResult.TOO_COMPLEX; } ProgressManager.checkCanceled(); if (LOG.isDebugEnabled()) { LOG.debug(instructionState.toString()); } // System.out.println(instructionState.toString()); Instruction instruction = instructionState.getInstruction(); if (instruction instanceof BranchingInstruction) { BranchingInstruction branching = (BranchingInstruction) instruction; Collection<DfaMemoryState> processed = processedStates.get(branching); if (processed.contains(instructionState.getMemoryState())) { continue; } if (processed.size() > MAX_STATES_PER_BRANCH) { LOG.debug("Too complex because too many different possible states"); return RunnerResult.TOO_COMPLEX; // Too complex :( } if (loopNumber[branching.getIndex()] != 0) { processedStates.putValue(branching, instructionState.getMemoryState().createCopy()); } } DfaInstructionState[] after = acceptInstruction(visitor, instructionState); for (DfaInstructionState state : after) { Instruction nextInstruction = state.getInstruction(); if (nextInstruction.getIndex() >= endOffset) { continue; } handleStepOutOfLoop( instruction, nextInstruction, loopNumber, processedStates, incomingStates, states, after, queue); if (nextInstruction instanceof BranchingInstruction) { BranchingInstruction branching = (BranchingInstruction) nextInstruction; if (processedStates.get(branching).contains(state.getMemoryState()) || incomingStates.get(branching).contains(state.getMemoryState())) { continue; } if (loopNumber[branching.getIndex()] != 0) { incomingStates.putValue(branching, state.getMemoryState().createCopy()); } } queue.offer(state); } } } psiBlock.putUserData(TOO_EXPENSIVE_HASH, null); LOG.debug("Analysis ok"); return RunnerResult.OK; } catch (ArrayIndexOutOfBoundsException e) { LOG.error(psiBlock.getText(), e); return RunnerResult.ABORTED; } catch (EmptyStackException e) { LOG.error(psiBlock.getText(), e); return RunnerResult.ABORTED; } }