forked from checkstyle/sonar-checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue checkstyle#45: resolved multiple simple formatting violations
- Loading branch information
Showing
34 changed files
with
2,046 additions
and
2,051 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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 | ||
} | ||
|
||
} |
Oops, something went wrong.