protected void runTool(
      final String testDir,
      final String jdkName,
      boolean runDeadCodeFirst,
      final InspectionTool tool,
      InspectionTool... additional) {
    final VirtualFile[] sourceDir = new VirtualFile[1];
    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                try {
                  setupRootModel(testDir, sourceDir, jdkName);
                } catch (Exception e) {
                  LOG.error(e);
                }
              }
            });
    AnalysisScope scope = createAnalysisScope(sourceDir[0].getParent());

    InspectionManagerEx inspectionManager =
        (InspectionManagerEx) InspectionManager.getInstance(getProject());
    InspectionTool[] tools =
        runDeadCodeFirst
            ? new InspectionTool[] {new UnusedDeclarationInspection(), tool}
            : new InspectionTool[] {tool};
    tools = ArrayUtil.mergeArrays(tools, additional);
    final GlobalInspectionContextImpl globalContext =
        CodeInsightTestFixtureImpl.createGlobalContextForTool(
            scope, getProject(), inspectionManager, tools);

    InspectionTestUtil.runTool(tool, scope, globalContext, inspectionManager);
  }
 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
  public void computeUsages(List<PsiLiteralExpression> targets) {
    final Project project = myTarget.getProject();
    final PsiElement parent = myTarget.getParent().getParent();
    final LocalInspectionsPass pass =
        new LocalInspectionsPass(
            myFile,
            myFile.getViewProvider().getDocument(),
            parent.getTextRange().getStartOffset(),
            parent.getTextRange().getEndOffset(),
            LocalInspectionsPass.EMPTY_PRIORITY_RANGE,
            false,
            HighlightInfoProcessor.getEmpty());
    final InspectionProfile inspectionProfile =
        InspectionProjectProfileManager.getInstance(project).getInspectionProfile();
    for (PsiLiteralExpression target : targets) {
      final Object value = target.getValue();
      if (!(value instanceof String)) {
        continue;
      }
      InspectionToolWrapper toolWrapperById =
          ((InspectionProfileImpl) inspectionProfile).getToolById((String) value, target);
      if (!(toolWrapperById instanceof LocalInspectionToolWrapper)) {
        continue;
      }
      final LocalInspectionToolWrapper toolWrapper =
          ((LocalInspectionToolWrapper) toolWrapperById).createCopy();
      final InspectionManagerEx managerEx =
          (InspectionManagerEx) InspectionManager.getInstance(project);
      final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false);
      toolWrapper.initialize(context);
      ((RefManagerImpl) context.getRefManager()).inspectionReadActionStarted();
      ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
      Runnable inspect =
          new Runnable() {
            @Override
            public void run() {
              pass.doInspectInBatch(
                  context,
                  managerEx,
                  Collections.<LocalInspectionToolWrapper>singletonList(toolWrapper));
            }
          };
      if (indicator == null) {
        ProgressManager.getInstance()
            .executeProcessUnderProgress(inspect, new ProgressIndicatorBase());
      } else {
        inspect.run();
      }

      for (HighlightInfo info : pass.getInfos()) {
        final PsiElement element =
            CollectHighlightsUtil.findCommonParent(myFile, info.startOffset, info.endOffset);
        if (element != null) {
          addOccurrence(element);
        }
      }
    }
  }
 @NotNull
 public static InspectionResultsView showOfflineView(
     @NotNull Project project,
     @NotNull Map<String, Map<String, Set<OfflineProblemDescriptor>>> resMap,
     @NotNull InspectionProfileImpl inspectionProfile,
     @NotNull String title) {
   final AnalysisScope scope = new AnalysisScope(project);
   final InspectionManagerEx managerEx =
       (InspectionManagerEx) InspectionManager.getInstance(project);
   final GlobalInspectionContextImpl context = managerEx.createNewGlobalContext(false);
   context.setExternalProfile(inspectionProfile);
   context.setCurrentScope(scope);
   context.initializeTools(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
   final InspectionResultsView view =
       new InspectionResultsView(context, new OfflineInspectionRVContentProvider(resMap, project));
   ((RefManagerImpl) context.getRefManager()).startOfflineView();
   context.addView(view, title, true);
   view.update();
   return view;
 }
Exemple #5
0
 @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;
 }
  private static void runInspection(
      @NotNull Project project,
      @NotNull InspectionToolWrapper toolWrapper,
      @Nullable VirtualFile virtualFile,
      PsiElement psiElement,
      PsiFile psiFile) {
    final InspectionManagerEx managerEx =
        (InspectionManagerEx) InspectionManager.getInstance(project);
    final Module module =
        virtualFile != null ? ModuleUtilCore.findModuleForFile(virtualFile, project) : null;

    AnalysisScope analysisScope = null;
    if (psiFile != null) {
      analysisScope = new AnalysisScope(psiFile);
    } else {
      if (virtualFile != null && virtualFile.isDirectory()) {
        final PsiDirectory psiDirectory =
            PsiManager.getInstance(project).findDirectory(virtualFile);
        if (psiDirectory != null) {
          analysisScope = new AnalysisScope(psiDirectory);
        }
      }
      if (analysisScope == null && virtualFile != null) {
        analysisScope = new AnalysisScope(project, Arrays.asList(virtualFile));
      }
      if (analysisScope == null) {
        analysisScope = new AnalysisScope(project);
      }
    }

    final FileFilterPanel fileFilterPanel = new FileFilterPanel();
    fileFilterPanel.init();

    final BaseAnalysisActionDialog dlg =
        new BaseAnalysisActionDialog(
            AnalysisScopeBundle.message(
                "specify.analysis.scope", InspectionsBundle.message("inspection.action.title")),
            AnalysisScopeBundle.message(
                "analysis.scope.title", InspectionsBundle.message("inspection.action.noun")),
            project,
            analysisScope,
            module != null ? module.getName() : null,
            true,
            AnalysisUIOptions.getInstance(project),
            psiElement) {

          @Override
          protected JComponent getAdditionalActionSettings(Project project) {
            return fileFilterPanel.getPanel();
          }

          @Override
          public SearchScope getCustomScope() {
            return PsiSearchScopeUtil.union(
                fileFilterPanel.getSearchScope(), super.getCustomScope());
          }
        };

    AnalysisScope scope = analysisScope;
    dlg.show();
    if (!dlg.isOK()) return;
    final AnalysisUIOptions uiOptions = AnalysisUIOptions.getInstance(project);
    scope = dlg.getScope(uiOptions, scope, project, module);
    RunInspectionIntention.rerunInspection(toolWrapper, managerEx, scope, psiFile);
  }
 public InspectionManagerEx getManager() {
   return (InspectionManagerEx) InspectionManager.getInstance(myProject);
 }