Ejemplo n.º 1
0
 /**
  * Applies the exclude and include filters to the found annotations.
  *
  * @param allAnnotations all annotations
  * @return the filtered annotations if there is a filter defined
  */
 private List<FileAnnotation> applyFilters(final List<FileAnnotation> allAnnotations) {
   List<FileAnnotation> includedAnnotations;
   if (includePatterns.isEmpty()) {
     includedAnnotations = allAnnotations;
   } else {
     includedAnnotations = new ArrayList<FileAnnotation>();
     for (FileAnnotation annotation : allAnnotations) {
       for (Pattern include : includePatterns) {
         if (include.matcher(annotation.getFileName()).matches()) {
           includedAnnotations.add(annotation);
         }
       }
     }
   }
   if (excludePatterns.isEmpty()) {
     return includedAnnotations;
   } else {
     List<FileAnnotation> excludedAnnotations = new ArrayList<FileAnnotation>(includedAnnotations);
     for (FileAnnotation annotation : includedAnnotations) {
       for (Pattern exclude : excludePatterns) {
         if (exclude.matcher(annotation.getFileName()).matches()) {
           excludedAnnotations.remove(annotation);
         }
       }
     }
     return excludedAnnotations;
   }
 }
Ejemplo n.º 2
0
 /**
  * Creates a new instance of {@link FindBugsParser}.
  *
  * @param sourceFolders a collection of folders to scan for source files. If empty, the source
  *     folders are guessed.
  * @param isRankActivated determines whether to use the rank when evaluation the priority
  * @param excludePattern RegEx patterns of files to exclude from the report
  * @param includePattern RegEx patterns of files to include in the report
  */
 public FindBugsParser(
     final Collection<String> sourceFolders,
     final boolean isRankActivated,
     final String excludePattern,
     final String includePattern) {
   mavenSources.addAll(sourceFolders);
   this.isRankActivated = isRankActivated;
   addPatterns(includePatterns, includePattern);
   addPatterns(excludePatterns, excludePattern);
 }
Ejemplo n.º 3
0
  /**
   * Returns the parsed FindBugs analysis file. This scanner accepts files in the native FindBugs
   * format.
   *
   * @param file the FindBugs analysis file
   * @param sources a collection of folders to scan for source files
   * @param moduleName name of maven module
   * @param hashToMessageMapping mapping of hash codes to messages
   * @param categories mapping from bug types to their categories
   * @return the parsed result (stored in the module instance)
   * @throws IOException if the file could not be parsed
   * @throws DocumentException in case of a parser exception
   */
  private Collection<FileAnnotation> parse(
      final InputStream file,
      final Collection<String> sources,
      final String moduleName,
      final Map<String, String> hashToMessageMapping,
      final Map<String, String> categories)
      throws IOException, DocumentException {
    SortedBugCollection collection = readXml(file);

    Project project = collection.getProject();
    for (String sourceFolder : sources) {
      project.addSourceDir(sourceFolder);
    }

    SourceFinder sourceFinder = new SourceFinder(project);
    String actualName = extractModuleName(moduleName, project);

    TreeStringBuilder stringPool = new TreeStringBuilder();
    List<FileAnnotation> annotations = new ArrayList<FileAnnotation>();
    Collection<BugInstance> bugs = collection.getCollection();

    for (BugInstance warning : bugs) {

      SourceLineAnnotation sourceLine = warning.getPrimarySourceLineAnnotation();

      String message = warning.getMessage();
      String type = warning.getType();
      if (message.contains("TEST: Unknown")) {
        message = FindBugsMessages.getInstance().getShortMessage(type, LocaleProvider.getLocale());
      }
      String category = categories.get(type);
      if (category
          == null) { // alternately, only if warning.getBugPattern().getType().equals("UNKNOWN")
        category = warning.getBugPattern().getCategory();
      }
      Bug bug =
          new Bug(
              getPriority(warning),
              StringUtils.defaultIfEmpty(
                  hashToMessageMapping.get(warning.getInstanceHash()), message),
              category,
              type,
              sourceLine.getStartLine(),
              sourceLine.getEndLine());
      bug.setInstanceHash(warning.getInstanceHash());
      bug.setRank(warning.getBugRank());

      boolean ignore = setCloudInformation(collection, warning, bug);
      if (!ignore) {
        bug.setNotAProblem(false);
        bug.setFileName(findSourceFile(project, sourceFinder, sourceLine));
        bug.setPackageName(warning.getPrimaryClass().getPackageName());
        bug.setModuleName(actualName);
        setAffectedLines(warning, bug);

        annotations.add(bug);
        bug.intern(stringPool);
      }
    }

    return applyFilters(annotations);
  }