/** * Copy constructor: Creates a new instance of {@link AbstractAnnotation}. * * @param copy * the annotation to copy the values from */ @SuppressFBWarnings("ST") public AbstractAnnotation(final FileAnnotation copy) { key = currentKey++; message = TreeString.of(copy.getMessage()); priority = copy.getPriority(); primaryLineNumber = copy.getPrimaryLineNumber(); lineRanges = new LineRangeList(copy.getLineRanges()); contextHashCode = copy.getContextHashCode(); fileName = TreeString.of(copy.getFileName()); category = copy.getCategory(); type = copy.getType(); moduleName = TreeString.of(copy.getModuleName()); packageName = TreeString.of(copy.getPackageName()); authorName = copy.getAuthorName(); authorEmail = copy.getAuthorEmail(); commitId = copy.getCommitId(); overridePackageCategoryName = TreeString.of(copy.getOverridePackageCategoryName()); }
/** * Returns the link with the specified hash code. * * @param linkHashCode * the hash code of the linked annotation * @return the link with the specified hash code */ public FileAnnotation getLink(final long linkHashCode) { for (FileAnnotation link : links) { if (link.getKey() == linkHashCode) { return link; } } throw new NoSuchElementException("Linked annotation not found: key=" + linkHashCode); } }
/** * For each duplicate code annotation that does not have a package name * (i.e., for non Java sources), a link name is generated. * * @param result the annotations */ private void createLinkNames(final Collection<FileAnnotation> result) { if (workspacePath != null) { for (FileAnnotation duplication : result) { duplication.setPathName(workspacePath); } } } }
public Warning(final FileAnnotation copy, final int currentLine) { super(copy.getPriority(), copy.getMessage(), copy.getPrimaryLineNumber(), currentLine, copy.getCategory(), copy.getType()); setFileName(copy.getFileName()); setOrigin(ORIGIN); }
private void setBlameResults(final Set<FileAnnotation> annotations, final Map<String, BlameRequest> blamesOfConflictingFiles) { for (FileAnnotation annotation : annotations) { if (blamesOfConflictingFiles.containsKey(annotation.getFileName())) { BlameRequest blame = blamesOfConflictingFiles.get(annotation.getFileName()); int line = annotation.getPrimaryLineNumber(); annotation.setAuthorName(blame.getName(line)); annotation.setAuthorEmail(blame.getEmail(line)); annotation.setCommitId(blame.getCommit(line)); } else { log("Skipping file %s, no result found.%n", annotation.getFileName()); } } }
/** * Creates a project that contains a single annotation. * * @return the created project */ private ParserResult createProjectWithWarning() { ParserResult project = new ParserResult(); FileAnnotation annotation = mock(FileAnnotation.class); when(annotation.getPriority()).thenReturn(Priority.HIGH); when(annotation.getFileName()).thenReturn(FILENAME); when(annotation.getModuleName()).thenReturn("Module"); when(annotation.getPackageName()).thenReturn("Package"); project.addAnnotation(annotation); return project; }
private String createMessage(FileAnnotation annotation) { String ind = (indent > 0) ? StringUtils.repeat(" ", indent) : ""; StringBuilder message = new StringBuilder(); message.append(ind); if (annotation.getPrimaryLineNumber() > 0) { message.append(annotation.getFileName().replaceAll("^.*workspace/", "")); message.append(":"); message.append(annotation.getPrimaryLineNumber()); message.append(" "); } message.append(annotation.getMessage().replace("<br>", "\n" + ind)); message.append("\n"); String toolTip; toolTip = annotation.getToolTip().replace("<br>", "\n"); if (toolTip != null) { toolTip = ind + toolTip.replace("\n", "\n" + ind).trim(); message.append(toolTip); message.append("\n"); } return message.toString(); } }
/** * Returns whether the annotation references a relative filename. * * @param annotation * the annotation * @return <code>true</code> if the filename is relative */ private boolean hasRelativeFileName(final FileAnnotation annotation) { String fileName = annotation.getFileName(); return !fileName.startsWith(SLASH) && !fileName.contains(":"); }
@Override public ParserResult invoke(final File workspace, final VirtualChannel channel) throws IOException { ContextHashCode contextHashCode = new ContextHashCode(); for (FileAnnotation annotation : result.getAnnotations()) { annotation.setContextHashCode(contextHashCode.compute( annotation.getFileName(), annotation.getPrimaryLineNumber(), defaultEncoding)); } return result; } }
@Override protected Collection<FileAnnotation> postProcessWarnings(final List<FileAnnotation> warnings) { List<FileAnnotation> condensed = Lists.newArrayList(); int line = -1; for (FileAnnotation warning : warnings) { if (warning.getPrimaryLineNumber() == line + 1 && !condensed.isEmpty()) { FileAnnotation previous = condensed.get(condensed.size() - 1); if (previous.getPriority() == warning.getPriority()) { condensed.remove(condensed.size() - 1); condensed.add(new Warning(previous, warning.getMessage(), warning.getPrimaryLineNumber())); } } else { condensed.add(warning); } line = warning.getPrimaryLineNumber(); } List<FileAnnotation> noBlank = Lists.newArrayList(); for (FileAnnotation warning : condensed) { if (StringUtils.isNotBlank(warning.getMessage())) { noBlank.add(warning); } } return noBlank; } }
/** * Verifies the created project. * * @param project the created project */ @SuppressWarnings("PMD") protected void verifyProject(final JavaProject project) { assertTrue(project.hasAnnotations()); checkSummary(project); assertTrue(MODULE_NOT_IN_PROJECT, project.containsModule(MODULE1)); assertTrue(MODULE_NOT_IN_PROJECT, project.containsModule(MODULE2)); checkFirstModule(project.getModule(MODULE1)); checkSecondModule(project.getModule(MODULE2)); for (FileAnnotation annotation : project.getModule(MODULE2).getAnnotations()) { assertEquals("Wrong primary line number.", LINE_NUMBER, annotation.getPrimaryLineNumber()); Collection<LineRange> lineRanges = annotation.getLineRanges(); assertEquals("Wrong number of ranges.", 1, lineRanges.size()); LineRange range = lineRanges.iterator().next(); assertEquals("Wrong start line number.", LINE_NUMBER, range.getStart()); assertEquals("Wrong end line number.", LINE_NUMBER, range.getEnd()); assertEquals("Wrong package prefix.", Messages.PackageDetail_header(), project.getModule(MODULE2).getPackageCategoryName()); assertSame(annotation, project.getAnnotation(annotation.getKey())); assertSame(annotation, project.getAnnotation(Long.toString(annotation.getKey()))); } }
String fileName = FilenameUtils.getName(annotation.getFileName()); if (fileNameCache.containsKey(fileName)) { int matchesCount = 0; String absoluteFileName = null; for (String match : fileNameCache.get(fileName)) { if (match.contains(annotation.getFileName())) { absoluteFileName = workspace.getPath() + SLASH + match; matchesCount++; annotation.setFileName(absoluteFileName); annotation.getFileName(), fileNameCache.get(fileName))); LOGGER.log(Level.FINE, String.format( "Absolute filename could not be resolved for: %s. No such file in workspace: %s. ", annotation.getFileName(), workspace.getPath()));
/** {@inheritDoc} */ public int compareTo(final FileAnnotation other) { int result; result = getFileName().compareTo(other.getFileName()); if (result != 0) { return result; } result = getPrimaryLineNumber() - other.getPrimaryLineNumber(); if (result != 0) { return result; } return hashCode() - other.hashCode(); // fallback }
/** * Updates the annotation drill-down mappings (priority, packages, files) with the specified annotation. * * @param annotation the new annotation */ private void updateMappings(final FileAnnotation annotation) { annotationsByPriority.get(annotation.getPriority()).add(annotation); if (StringUtils.isNotBlank(annotation.getCategory())) { addCategory(annotation); } if (StringUtils.isNotBlank(annotation.getType())) { addType(annotation); } if (hierarchy == Hierarchy.PROJECT) { addModule(annotation); } if (hierarchy == Hierarchy.PROJECT || hierarchy == Hierarchy.MODULE) { addPackage(annotation); } if (hierarchy == Hierarchy.PROJECT || hierarchy == Hierarchy.MODULE || hierarchy == Hierarchy.PACKAGE) { addFile(annotation); } }
for (FileAnnotation annotation : allAnnotations) { for (Pattern include : includePatterns) { if (include.matcher(annotation.getFileName()).matches()) { includedAnnotations.add(annotation); for (FileAnnotation annotation : includedAnnotations) { for (Pattern exclude : excludePatterns) { if (exclude.matcher(annotation.getFileName()).matches()) { excludedAnnotations.remove(annotation); if (exclude.matcher(annotation.getMessage()).matches()) { excludedAnnotations.remove(annotation); if (exclude.matcher(annotation.getCategory()).matches()) { excludedAnnotations.remove(annotation);
@Override public int compareTo(final FileAnnotation other) { int result; result = getFileName().compareTo(other.getFileName()); if (result != 0) { return result; } result = getPrimaryLineNumber() - other.getPrimaryLineNumber(); if (result != 0) { return result; } result = getColumnStart() - other.getColumnStart(); if (result != 0) { return result; } return hashCode() - other.hashCode(); // fallback }
ModuleDetector detector = new ModuleDetector(new File(workspace)); for (FileAnnotation annotation : warnings) { String module = detector.guessModuleName(annotation.getFileName()); annotation.setModuleName(module); annotation.setPathName(build.getWorkspace().getRemote());
for (LineRange range : annotation.getLineRanges()) { while (lineNumber < range.getStart()) { copyLine(output, lineIterator); output.append("<div tooltip=\""); if (range.getStart() > 0) { outputEscaped(output, annotation.getMessage()); outputEscaped(output, annotation.getToolTip()); output.append("\" nodismiss=\"\">\n"); output.append("<code><b>\n"); if (range.getStart() <= 0) { output.append(annotation.getMessage()); if (StringUtils.isBlank(annotation.getMessage())) { output.append(annotation.getToolTip());
/** * Returns the package category title for the scanned files. * * @return the package category title for the scanned files */ public final String getPackageCategoryTitle() { if (hasAnnotations()) { FileAnnotation annotation = getAnnotations().iterator().next(); String fileName = annotation.getFileName(); if (fileName.endsWith(".cs")) { return Messages.NamespaceDetail_title(); } if (annotation.hasPackageName()) { if (annotation.hasOverridePackageCategoryTitle()) { return annotation.getOverridePackageCategoryTitle(); } return Messages.PackageDetail_title(); } else { return Messages.PathDetail_title(); } } return Messages.PackageDetail_title(); }
/** * Returns the package category name for the scanned files. Currently, only * java and c# files are supported. * * @return the package category name for the scanned files */ public final String getPackageCategoryName() { if (hasAnnotations()) { FileAnnotation annotation = getAnnotations().iterator().next(); String fileName = annotation.getFileName(); if (fileName.endsWith(".cs")) { return Messages.NamespaceDetail_header(); } if (annotation.hasPackageName()) { if (annotation.hasOverridePackageCategoryName()) { return annotation.getOverridePackageCategoryName(); } return Messages.PackageDetail_header(); } else { return Messages.PathDetail_header(); } } return Messages.PackageDetail_header(); }