/** * Returns check name without 'Check' suffix. * @param event audit event. * @return check name without 'Check' suffix. */ private static String getCheckShortName(AuditEvent event) { final String checkFullName = event.getSourceName(); final String checkShortName; final int lastDotIndex = checkFullName.lastIndexOf('.'); if (lastDotIndex == -1) { if (checkFullName.endsWith(SUFFIX)) { checkShortName = checkFullName.substring(0, checkFullName.lastIndexOf(SUFFIX)); } else { checkShortName = checkFullName; } } else { if (checkFullName.endsWith(SUFFIX)) { checkShortName = checkFullName.substring(lastDotIndex + 1, checkFullName.lastIndexOf(SUFFIX)); } else { checkShortName = checkFullName.substring(lastDotIndex + 1); } } return checkShortName; }
/** * Is matching by file name and Check name. * @param event event * @return true is matching */ private boolean isFileNameAndModuleNotMatching(AuditEvent event) { return event.getFileName() == null || fileRegexp != null && !fileRegexp.matcher(event.getFileName()).find() || event.getLocalizedMessage() == null || moduleId != null && !moduleId.equals(event.getModuleId()) || checkRegexp != null && !checkRegexp.matcher(event.getSourceName()).find(); }
/** * Checks whether the suppression matches the given {@link AuditEvent}. * @param event {@link AuditEvent} instance. * @return true if the suppression matches {@link AuditEvent}. */ private boolean isMatch(AuditEvent event) { boolean match = false; if (isInScopeOfSuppression(event)) { final Matcher sourceNameMatcher = eventSourceRegexp.matcher(event.getSourceName()); if (sourceNameMatcher.find()) { match = eventMessageRegexp == null || eventMessageRegexp.matcher(event.getMessage()).find(); } else { match = event.getModuleId() != null && eventSourceRegexp.matcher(event.getModuleId()).find(); } } return match; }
@Override public void addError(AuditEvent event) { final String xpathQuery = XpathFileGeneratorAstFilter.findCorrespondingXpathQuery(event); if (xpathQuery != null) { printXmlHeader(); final File file = new File(event.getFileName()); writer.println("<suppress-xpath"); writer.print(" files=\""); writer.print(file.getName()); writer.println(QUOTE_CHAR); if (event.getModuleId() == null) { final String checkName = PackageObjectFactory.getShortFromFullModuleNames(event.getSourceName()); writer.print(" checks=\""); writer.print(checkName); } else { writer.print(" id=\""); writer.print(event.getModuleId()); } writer.println(QUOTE_CHAR); writer.print(" query=\""); writer.print(xpathQuery); writer.println("\"/>"); } }
/** * Outputs the given event to the writer. * @param event An event to print. */ private void writeFileError(AuditEvent event) { writer.print("<error" + " line=\"" + event.getLine() + "\""); if (event.getColumn() > 0) { writer.print(" column=\"" + event.getColumn() + "\""); } writer.print(" severity=\"" + event.getSeverityLevel().getName() + "\""); writer.print(" message=\"" + encode(event.getMessage()) + "\""); writer.print(" source=\""); if (event.getModuleId() == null) { writer.print(encode(event.getSourceName())); } else { writer.print(encode(event.getModuleId())); } writer.println("\"/>"); }
@Test public void testFormatModuleNameDoesNotContainCheckSuffix() { final AuditEvent mock = PowerMockito.mock(AuditEvent.class); when(mock.getSourceName()).thenReturn("TestModule"); when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING); when(mock.getLine()).thenReturn(1); when(mock.getColumn()).thenReturn(1); when(mock.getMessage()).thenReturn("Mocked message."); when(mock.getFileName()).thenReturn("InputMockFile.java"); final AuditEventFormatter formatter = new AuditEventDefaultFormatter(); final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]"; assertEquals("Invalid format", expected, formatter.format(mock)); }
@Test public void testFormatModuleNameContainsCheckSuffix() { final AuditEvent mock = PowerMockito.mock(AuditEvent.class); when(mock.getSourceName()).thenReturn("TestModuleCheck"); when(mock.getSeverityLevel()).thenReturn(SeverityLevel.WARNING); when(mock.getLine()).thenReturn(1); when(mock.getColumn()).thenReturn(1); when(mock.getMessage()).thenReturn("Mocked message."); when(mock.getFileName()).thenReturn("InputMockFile.java"); final AuditEventFormatter formatter = new AuditEventDefaultFormatter(); final String expected = "[WARN] InputMockFile.java:1:1: Mocked message. [TestModule]"; assertEquals("Invalid format", expected, formatter.format(mock)); }
/** * Checks for a suppression of a check with the given source name and * location in the last file processed. * @param event audit event. * @return whether the check with the given name is suppressed at the given * source location */ public static boolean isSuppressed(AuditEvent event) { final List<Entry> entries = ENTRIES.get(); final String sourceName = event.getSourceName(); final String checkAlias = getAlias(sourceName); final int line = event.getLine(); final int column = event.getColumn(); boolean suppressed = false; for (Entry entry : entries) { final boolean afterStart = isSuppressedAfterEventStart(line, column, entry); final boolean beforeEnd = isSuppressedBeforeEventEnd(line, column, entry); final boolean nameMatches = ALL_WARNING_MATCHING_ID.equals(entry.getCheckName()) || entry.getCheckName().equalsIgnoreCase(checkAlias); final boolean idMatches = event.getModuleId() != null && event.getModuleId().equals(entry.getCheckName()); if (afterStart && beforeEnd && (nameMatches || idMatches)) { suppressed = true; break; } } return suppressed; }
/** * Get the rule category from an audit event. * * @param event the audit event * @return the rule category, which is the last package name or "misc" or "extension" */ public static String getCategory( AuditEvent event ) { return getCategory( event.getSourceName() ); }
/** * Get the rule name from an audit event. * * @param event the audit event * @return the rule name, which is the class name without package and removed eventual "Check" suffix */ public static String getName( AuditEvent event ) { return getName( event.getSourceName() ); } /**
@Override public boolean accept(AuditEvent evt) { String filename = evt.getFileName().replace('\\', '/'); if (JAVA5PATTERN.matcher(filename).find()) { if (evt.getSourceName().endsWith("MissingOverrideCheck")) { return false; } } if (pattern.matcher(filename).find()) { return false; } if (examplePattern.matcher(filename).find()) { if (evt.getSourceName().endsWith(".JavadocPackageCheck")) { return false; } if (evt.getSourceName().endsWith(".HideUtilityClassConstructorCheck")) { return false; } } return true; } }
@VisibleForTesting static String getRuleKey(AuditEvent event) { String key = null; try { key = event.getModuleId(); } catch (Exception e) { // checkstyle throws a NullPointerException if the message is not set } if (StringUtils.isBlank(key)) { try { key = event.getSourceName(); } catch (Exception e) { // checkstyle can throw a NullPointerException if the message is not set } } return key; }
@VisibleForTesting static String getRuleKey(AuditEvent event) { String key = null; try { key = event.getModuleId(); } catch (Exception e) { // checkstyle throws a NullPointerException if the message is not set } if (StringUtils.isBlank(key)) { try { key = event.getSourceName(); } catch (Exception e) { // checkstyle can throw a NullPointerException if the message is not set } } return key; }
@VisibleForTesting static String getRuleKey(AuditEvent event) { String key = null; try { key = event.getModuleId(); } catch (Exception ex) { LOG.warn("AuditEvent is created incorrectly. Exception happen during getModuleId()", ex); } if (StringUtils.isBlank(key)) { try { key = event.getSourceName(); } catch (Exception ex) { LOG.warn("AuditEvent is created incorrectly." + "Exception happen during getSourceName()", ex); } } return key; }
/** * Is matching by file name and Check name. * @param event event * @return true is matching */ private boolean isFileNameAndModuleNotMatching(AuditEvent event) { return event.getFileName() == null || fileRegexp != null && !fileRegexp.matcher(event.getFileName()).find() || event.getLocalizedMessage() == null || moduleId != null && !moduleId.equals(event.getModuleId()) || checkRegexp != null && !checkRegexp.matcher(event.getSourceName()).find(); }
/** * Checks whether the suppression matches the given {@link AuditEvent}. * @param event {@link AuditEvent} instance. * @return true if the suppression matches {@link AuditEvent}. */ private boolean isMatch(AuditEvent event) { boolean match = false; if (isInScopeOfSuppression(event)) { final Matcher sourceNameMatcher = eventSourceRegexp.matcher(event.getSourceName()); if (sourceNameMatcher.find()) { match = eventMessageRegexp == null || eventMessageRegexp.matcher(event.getMessage()).find(); } else { match = event.getModuleId() != null && eventSourceRegexp.matcher(event.getModuleId()).find(); } } return match; }
@Override public void addException(final AuditEvent event, final Throwable throwable) { final String check = event.getSourceName(); Logger.error( this, "%s[%d]: %s (%s), %[exception]s", event.getFileName().substring( this.env.basedir().toString().length() ), event.getLine(), event.getMessage(), check.substring(check.lastIndexOf('.') + 1), throwable ); }
@Override public void addException(final AuditEvent event, final Throwable throwable) { final String check = event.getSourceName(); Logger.error( this, "%s[%d]: %s (%s), %[exception]s", event.getFileName().substring( this.env.basedir().toString().length() ), event.getLine(), event.getMessage(), check.substring(check.lastIndexOf('.') + 1), throwable ); }
@Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public Collection<Violation> validate(final Collection<File> files) { try { this.checker.process(Lists.newArrayList(files)); } catch (final CheckstyleException ex) { throw new IllegalStateException("Failed to process files", ex); } final List<AuditEvent> events = this.listener.events(); final Collection<Violation> results = new LinkedList<>(); for (final AuditEvent event : events) { final String check = event.getSourceName(); results.add( new Violation.Default( this.name(), check.substring(check.lastIndexOf('.') + 1), event.getFileName(), String.valueOf(event.getLine()), event.getMessage() ) ); } return results; }
/** * Outputs the given event to the writer. * @param event An event to print. */ private void writeFileError(AuditEvent event) { writer.print("<error" + " line=\"" + event.getLine() + "\""); if (event.getColumn() > 0) { writer.print(" column=\"" + event.getColumn() + "\""); } writer.print(" severity=\"" + event.getSeverityLevel().getName() + "\""); writer.print(" message=\"" + encode(event.getMessage()) + "\""); writer.print(" source=\""); if (event.getModuleId() == null) { writer.print(encode(event.getSourceName())); } else { writer.print(encode(event.getModuleId())); } writer.println("\"/>"); }