Skip to content

Commit

Permalink
Issue checkstyle#45: resolved multiple simple formatting violations
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and tsjensen committed Mar 21, 2019
1 parent afb4466 commit 1e05854
Show file tree
Hide file tree
Showing 34 changed files with 2,046 additions and 2,051 deletions.
27 changes: 6 additions & 21 deletions checkstyle-sonar-plugin/config/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,41 @@

<suppressions>
<!-- START of legacy code, all violations will be resolved during transition to main project -->
<suppress checks="Indentation" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="Javadoc*" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="Header" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="FinalLocalVariable" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="ImportControl" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="DesignForExtension" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="WriteTag" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="DeclarationOrder" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="RightCurly" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="IllegalCatch" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="CatchParameterName" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="EmptyLineSeparator" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="OperatorWrap" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="MemberName" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="InnerTypeLast" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="ArrayTrailingComma" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="ReturnCount" files=".*[\\/]src[\\/]main[\\/]"/>
<suppress checks="ParameterName" files=".*[\\/]src[\\/]main[\\/]"/>

<suppress checks="LeftCurly" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="RightCurly" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="FinalLocalVariable" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="HiddenField" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MagicNumber" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="VariableDeclarationUsageDistance" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="DesignForExtension" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="Header" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="RegexpHeader" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="AvoidStaticImport" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="ImportControl" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="Indentation" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="JavadocMethod" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="JavadocPackage" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="JavadocType" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="JavadocVariable" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="WriteTag" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="ClassDataAbstractionCoupling" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="CyclomaticComplexity" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="NPathComplexity" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="CatchParameterName" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MemberName" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="RegexpMultiline" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="AnonInnerLength" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="TrailingComment" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="EmptyLineSeparator" files=".*[\\/]src[\\/]test[\\/]"/>
<!-- END of legacy code -->


<!-- parsing of xml imply reference attrubute names, does not make sense to move all to special variables -->
<suppress checks="MultipleStringLiterals" files="CheckstyleProfileImporter\.java"/>
<!-- messing test code with such optimization does not make sense , readability will decrease -->
<suppress checks="MultipleStringLiterals" files=".*[\\/]src[\\/]test[\\/]"/>

<!-- Tone down the checking for test code -->
<suppress checks="Javadoc" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="WriteTag" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="AvoidStaticImport" files=".*[\\/]src[\\/]test[\\/]"/>
<suppress checks="MagicNumber" files=".*[\\/]src[\\/]test[\\/]"/>
</suppressions>
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/*
* SonarQube Checkstyle Plugin
* Copyright (C) 2012 SonarSource
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////

package org.sonar.plugins.checkstyle;

import org.apache.commons.lang.StringUtils;
Expand All @@ -40,119 +40,124 @@
*/
public class CheckstyleAuditListener implements AuditListener, BatchExtension {

private static final Logger LOG = LoggerFactory.getLogger(CheckstyleAuditListener.class);

private final RuleFinder ruleFinder;
private final FileSystem fs;
private final ResourcePerspectives perspectives;
private InputFile currentResource;

public CheckstyleAuditListener(RuleFinder ruleFinder, FileSystem fs,
ResourcePerspectives perspectives) {
this.ruleFinder = ruleFinder;
this.fs = fs;
this.perspectives = perspectives;
}

@Override
public void auditStarted(AuditEvent event) {
// nop
}

@Override
public void auditFinished(AuditEvent event) {
// nop
}

@Override
public void fileStarted(AuditEvent event) {
// nop
}

@Override
public void fileFinished(AuditEvent event) {
currentResource = null;
}

@Override
public void addError(AuditEvent event) {
String ruleKey = getRuleKey(event);
if (ruleKey != null) {
String message = getMessage(event);
// In Checkstyle 5.5 exceptions are reported as an events from TreeWalker
if ("com.puppycrawl.tools.checkstyle.TreeWalker".equals(ruleKey)) {
LOG.warn("{} : {}", event.getFileName(), message);
}
initResource(event);
Issuable issuable = perspectives.as(Issuable.class, currentResource);
Rule rule = ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
if (rule != null && issuable != null) {
IssueBuilder issueBuilder = issuable.newIssueBuilder()
.ruleKey(rule.ruleKey())
.message(message)
.line(getLineId(event));
issuable.addIssue(issueBuilder.build());
}
private static final Logger LOG = LoggerFactory.getLogger(CheckstyleAuditListener.class);

private final RuleFinder ruleFinder;
private final FileSystem fs;
private final ResourcePerspectives perspectives;
private InputFile currentResource;

public CheckstyleAuditListener(RuleFinder ruleFinder, FileSystem fs,
ResourcePerspectives perspectives) {
this.ruleFinder = ruleFinder;
this.fs = fs;
this.perspectives = perspectives;
}
}

private void initResource(AuditEvent event) {
if (currentResource == null) {
String absoluteFilename = event.getFileName();
currentResource = fs.inputFile(fs.predicates().hasAbsolutePath(absoluteFilename));
@Override
public void auditStarted(AuditEvent event) {
// nop
}
}

@VisibleForTesting
static String getRuleKey(AuditEvent event) {
String key = null;
try {
key = event.getModuleId();
} catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getModuleId()", e);

@Override
public void auditFinished(AuditEvent event) {
// nop
}
if (StringUtils.isBlank(key)) {
try {
key = event.getSourceName();
} catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getSourceName()", e);
}

@Override
public void fileStarted(AuditEvent event) {
// nop
}

@Override
public void fileFinished(AuditEvent event) {
currentResource = null;
}
return key;
}

@VisibleForTesting
static String getMessage(AuditEvent event) {
try {
return event.getMessage();
@Override
public void addError(AuditEvent event) {
String ruleKey = getRuleKey(event);
if (ruleKey != null) {
String message = getMessage(event);
// In Checkstyle 5.5 exceptions are reported as an events from
// TreeWalker
if ("com.puppycrawl.tools.checkstyle.TreeWalker".equals(ruleKey)) {
LOG.warn("{} : {}", event.getFileName(), message);
}
initResource(event);
Issuable issuable = perspectives.as(Issuable.class, currentResource);
Rule rule = ruleFinder.findByKey(CheckstyleConstants.REPOSITORY_KEY, ruleKey);
if (rule != null && issuable != null) {
IssueBuilder issueBuilder = issuable.newIssueBuilder().ruleKey(rule.ruleKey())
.message(message).line(getLineId(event));
issuable.addIssue(issueBuilder.build());
}
}
}

} catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getMessage()", e);
return null;
private void initResource(AuditEvent event) {
if (currentResource == null) {
String absoluteFilename = event.getFileName();
currentResource = fs.inputFile(fs.predicates().hasAbsolutePath(absoluteFilename));
}
}
}

@VisibleForTesting
static Integer getLineId(AuditEvent event) {
Integer result = null;
try {
int line = event.getLine();
// checkstyle returns 0 if there is no relation to a file content, but we use null
if (line != 0) {
result = line;
}
} catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getLine()", e);

@VisibleForTesting
static String getRuleKey(AuditEvent event) {
String key = null;
try {
key = event.getModuleId();
}
catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getModuleId()", e);
}
if (StringUtils.isBlank(key)) {
try {
key = event.getSourceName();
}
catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly."
+ "Exception happen during getSourceName()", e);
}
}
return key;
}

@VisibleForTesting
static String getMessage(AuditEvent event) {
try {
return event.getMessage();

}
catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getMessage()", e);
return null;
}
}

@VisibleForTesting
static Integer getLineId(AuditEvent event) {
Integer result = null;
try {
int line = event.getLine();
// checkstyle returns 0 if there is no relation to a file content,
// but we use null
if (line != 0) {
result = line;
}
}
catch (Exception e) {
LOG.warn("AuditEvent is created incorrectly. Exception happen during getLine()", e);
}
return result;
}

/**
* Note that this method never invoked from Checkstyle 5.5.
*/
@Override
public void addException(AuditEvent event, Throwable throwable) {
// nop
}
return result;
}

/**
* Note that this method never invoked from Checkstyle 5.5.
*/
@Override
public void addException(AuditEvent event, Throwable throwable) {
// nop
}

}
Loading

0 comments on commit 1e05854

Please sign in to comment.