private static List<ProblemDescriptor> runInspectionOnFile( @NotNull PsiFile file, @NotNull LocalInspectionTool inspectionTool) { InspectionManager inspectionManager = InspectionManager.getInstance(file.getProject()); GlobalInspectionContext context = inspectionManager.createNewGlobalContext(false); return InspectionEngine.runInspectionOnFile( file, new LocalInspectionToolWrapper(inspectionTool), context); }
@Override protected void doAction( final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName) throws Exception { final LocalQuickFix fix = (LocalQuickFix) new SameParameterValueInspection().getQuickFix(text); assert fix != null; final int offset = getEditor().getCaretModel().getOffset(); final PsiElement psiElement = getFile().findElementAt(offset); assert psiElement != null; final ProblemDescriptor descriptor = InspectionManager.getInstance(getProject()) .createProblemDescriptor( psiElement, "", fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true); fix.applyFix(getProject(), descriptor); final String expectedFilePath = getBasePath() + "/after" + testName; checkResultByFile("In file :" + expectedFilePath, expectedFilePath, false); }
@Override public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { PsiElement container = myFix instanceof AbstractBatchSuppressByNoInspectionCommentFix ? ((AbstractBatchSuppressByNoInspectionCommentFix) myFix).getContainer(element) : null; boolean caretWasBeforeStatement = editor != null && container != null && editor.getCaretModel().getOffset() == container.getTextRange().getStartOffset(); InspectionManager inspectionManager = InspectionManager.getInstance(project); ProblemDescriptor descriptor = inspectionManager.createProblemDescriptor( element, element, "", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false); myFix.applyFix(project, descriptor); if (caretWasBeforeStatement) { editor.getCaretModel().moveToOffset(container.getTextRange().getStartOffset()); } }
@Nullable private static CommonProblemDescriptor createDescriptor( @Nullable RefEntity element, @NotNull OfflineProblemDescriptor offlineDescriptor, @NotNull InspectionToolWrapper toolWrapper, @NotNull InspectionToolPresentation presentation) { if (!(toolWrapper instanceof LocalInspectionToolWrapper)) return null; final InspectionManager inspectionManager = InspectionManager.getInstance(presentation.getContext().getProject()); final OfflineProblemDescriptor offlineProblemDescriptor = offlineDescriptor; if (element instanceof RefElement) { final PsiElement psiElement = ((RefElement) element).getElement(); if (psiElement != null) { ProblemDescriptor descriptor = ProgressManager.getInstance() .runProcess( () -> runLocalTool( psiElement, inspectionManager, offlineProblemDescriptor, (LocalInspectionToolWrapper) toolWrapper), new DaemonProgressIndicator()); if (descriptor != null) return descriptor; } return null; } final List<String> hints = offlineProblemDescriptor.getHints(); CommonProblemDescriptor descriptor = inspectionManager.createProblemDescriptor( offlineProblemDescriptor.getDescription(), (QuickFix) null); final QuickFix[] quickFixes = getFixes(descriptor, hints, presentation); if (quickFixes != null) { descriptor = inspectionManager.createProblemDescriptor( offlineProblemDescriptor.getDescription(), quickFixes); } return descriptor; }
public CommonProblemDescriptor[] checkElement( @NotNull final PsiElement psiElement, InspectionManager manager, Project project) { final Map<PsiElement, Collection<String>> suppressedScopes = new THashMap<PsiElement, Collection<String>>(); psiElement.accept( new JavaRecursiveElementWalkingVisitor() { @Override public void visitModifierList(PsiModifierList list) { super.visitModifierList(list); final PsiElement parent = list.getParent(); if (parent instanceof PsiModifierListOwner && !(parent instanceof PsiClass)) { checkElement(parent); } } @Override public void visitComment(PsiComment comment) { checkElement(comment); } @Override public void visitClass(PsiClass aClass) { if (aClass == psiElement) { super.visitClass(aClass); checkElement(aClass); } } private void checkElement(final PsiElement owner) { String idsString = SuppressManager.getInstance().getSuppressedInspectionIdsIn(owner); if (idsString != null && idsString.length() != 0) { List<String> ids = StringUtil.split(idsString, ","); if (IGNORE_ALL && (ids.contains(SuppressionUtil.ALL) || ids.contains(SuppressionUtil.ALL.toLowerCase()))) return; Collection<String> suppressed = suppressedScopes.get(owner); if (suppressed == null) { suppressed = ids; } else { for (String id : ids) { if (!suppressed.contains(id)) { suppressed.add(id); } } } suppressedScopes.put(owner, suppressed); } } }); if (suppressedScopes.values().isEmpty()) return null; // have to visit all file from scratch since inspections can be written in any perversive way // including checkFile() overriding Collection<InspectionTool> suppressedTools = new THashSet<InspectionTool>(); InspectionTool[] tools = getInspectionTools(psiElement, manager); for (Collection<String> ids : suppressedScopes.values()) { for (Iterator<String> iterator = ids.iterator(); iterator.hasNext(); ) { final String shortName = iterator.next().trim(); for (InspectionTool tool : tools) { if (tool instanceof LocalInspectionToolWrapper && ((LocalInspectionToolWrapper) tool).getTool().getID().equals(shortName)) { if (!((LocalInspectionToolWrapper) tool).isUnfair()) { suppressedTools.add(tool); } else { iterator.remove(); break; } } else if (tool.getShortName().equals(shortName)) { // ignore global unused as it won't be checked anyway if (!(tool instanceof LocalInspectionToolWrapper) && !(tool instanceof GlobalInspectionToolWrapper)) { iterator.remove(); break; } else { suppressedTools.add(tool); } } } } } final AnalysisScope scope = new AnalysisScope(psiElement.getContainingFile()); final InspectionManagerEx inspectionManagerEx = ((InspectionManagerEx) InspectionManager.getInstance(project)); GlobalInspectionContextImpl globalContext = inspectionManagerEx.createNewGlobalContext(false); globalContext.setCurrentScope(scope); final RefManagerImpl refManager = ((RefManagerImpl) globalContext.getRefManager()); refManager.inspectionReadActionStarted(); final List<ProblemDescriptor> result; try { result = new ArrayList<ProblemDescriptor>(); for (InspectionTool tool : suppressedTools) { String toolId = tool instanceof LocalInspectionToolWrapper ? ((LocalInspectionToolWrapper) tool).getTool().getID() : tool.getShortName(); tool.initialize(globalContext); Collection<CommonProblemDescriptor> descriptors; if (tool instanceof LocalInspectionToolWrapper) { LocalInspectionToolWrapper local = (LocalInspectionToolWrapper) tool; if (local.isUnfair()) continue; // cant't work with passes other than LocalInspectionPass local.processFile(psiElement.getContainingFile(), false, manager); descriptors = local.getProblemDescriptors(); } else if (tool instanceof GlobalInspectionToolWrapper) { GlobalInspectionToolWrapper global = (GlobalInspectionToolWrapper) tool; if (global.getTool().isGraphNeeded()) { refManager.findAllDeclarations(); } global.processFile(scope, manager, globalContext, false); descriptors = global.getProblemDescriptors(); } else { continue; } for (PsiElement suppressedScope : suppressedScopes.keySet()) { Collection<String> suppressedIds = suppressedScopes.get(suppressedScope); if (!suppressedIds.contains(toolId)) continue; for (CommonProblemDescriptor descriptor : descriptors) { if (!(descriptor instanceof ProblemDescriptor)) continue; PsiElement element = ((ProblemDescriptor) descriptor).getPsiElement(); if (element == null) continue; PsiElement annotation = SuppressManager.getInstance().getElementToolSuppressedIn(element, toolId); if (annotation != null && PsiTreeUtil.isAncestor(suppressedScope, annotation, false) || annotation == null && !PsiTreeUtil.isAncestor(suppressedScope, element, false)) { suppressedIds.remove(toolId); break; } } } } for (PsiElement suppressedScope : suppressedScopes.keySet()) { Collection<String> suppressedIds = suppressedScopes.get(suppressedScope); for (String toolId : suppressedIds) { PsiMember psiMember; String problemLine = null; if (suppressedScope instanceof PsiMember) { psiMember = (PsiMember) suppressedScope; } else { psiMember = PsiTreeUtil.getParentOfType(suppressedScope, PsiDocCommentOwner.class); final PsiStatement statement = PsiTreeUtil.getNextSiblingOfType(suppressedScope, PsiStatement.class); problemLine = statement != null ? statement.getText() : null; } if (psiMember != null && psiMember.isValid()) { String description = InspectionsBundle.message("inspection.redundant.suppression.description"); if (myQuickFixes == null) myQuickFixes = new BidirectionalMap<String, QuickFix>(); final String key = toolId + (problemLine != null ? ";" + problemLine : ""); QuickFix fix = myQuickFixes.get(key); if (fix == null) { fix = new RemoveSuppressWarningAction(toolId, problemLine); myQuickFixes.put(key, fix); } PsiElement identifier = null; if (psiMember instanceof PsiMethod) { identifier = ((PsiMethod) psiMember).getNameIdentifier(); } else if (psiMember instanceof PsiField) { identifier = ((PsiField) psiMember).getNameIdentifier(); } else if (psiMember instanceof PsiClass) { identifier = ((PsiClass) psiMember).getNameIdentifier(); } if (identifier == null) { identifier = psiMember; } result.add( manager.createProblemDescriptor( identifier, description, (LocalQuickFix) fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false)); } } } } finally { refManager.inspectionReadActionFinished(); globalContext.close(true); } return result.toArray(new ProblemDescriptor[result.size()]); }