Skip to content

Commit

Permalink
Fix MagikSquidSensor
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenLooman committed Nov 4, 2022
1 parent 904e3ff commit e7b0cfb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
8 changes: 8 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
"hostName": "localhost",
"port": 5006,
"projectName": "magik-debug-adapter"
},
{
"type": "java",
"name": "Debug (Attach)-sonar-magik-plugin",
"request": "attach",
"hostName": "localhost",
"port": 5007,
"projectName": "sonar-magik-plugin"
}
]
}
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changes
=======

0.5.3 (unreleased)
- Fix bug in sonar plugin where all issues were applied to every file


0.5.2 (2022-05-26)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public synchronized AstNode getTopNode() {
final MagikParser parser = new MagikParser();
final String magikSource = this.getSource();
this.astNode = parser.parseSafe(magikSource);

// Update URI.
MagikParser.updateUri(this.astNode, this.uri);
}

return this.astNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
Expand Down Expand Up @@ -220,6 +221,26 @@ private void applyWhitespaceConversion(final AstNode node) {
node.getChildren().forEach(this::applyWhitespaceConversion);
}

/**
* Recusrively update URI for AstNode/Tokens.
* @param node Node to start at.
* @param newUri New URI to set.
*/
public static void updateUri(final AstNode node, final URI newUri) {
final Token token = node.getToken();
if (token != null) {
try {
final Field fieldUri = token.getClass().getDeclaredField("uri");
fieldUri.setAccessible(true);
fieldUri.set(token, newUri);
} catch (final ReflectiveOperationException exception) {
LOGGER.error(exception.getMessage(), exception);
}
}

node.getChildren().forEach(childNode -> MagikParser.updateUri(childNode, newUri));
}

/**
* Update token value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public class MagikSquidSensor implements Sensor {
private static final Logger LOGGER = Loggers.get(MagikSquidSensor.class);
private static final long SLEEP_PERIOD = 100;

private final Checks<MagikCheck> checks;
private final CheckFactory checkFactory;
// private final Checks<MagikCheck> checks;
private final FileLinesContextFactory fileLinesContextFactory;
private final NoSonarFilter noSonarFilter;

Expand All @@ -60,9 +61,7 @@ public MagikSquidSensor(
final CheckFactory checkFactory,
final FileLinesContextFactory fileLinesContextFactory,
final NoSonarFilter noSonarFilter) {
this.checks = checkFactory
.<MagikCheck>create(CheckList.REPOSITORY_KEY)
.addAnnotatedChecks((Iterable<Class<?>>) CheckList.getChecks());
this.checkFactory = checkFactory;
this.fileLinesContextFactory = fileLinesContextFactory;
this.noSonarFilter = noSonarFilter;
}
Expand Down Expand Up @@ -121,10 +120,18 @@ private void scanMagikFile(final SensorContext context, final InputFile inputFil

// Save issues.
LOGGER.debug("Running checks");
for (final MagikCheck check : this.checks.all()) {
final Checks<MagikCheck> checks = checkFactory
.<MagikCheck>create(CheckList.REPOSITORY_KEY)
.addAnnotatedChecks((Iterable<Class<?>>) CheckList.getChecks());
for (final MagikCheck check : checks.all()) {
LOGGER.debug("Running check: {}", check);
final List<MagikIssue> issues = check.scanFileForIssues(magikFile);
this.saveIssues(context, check, issues, inputFile);
final RuleKey ruleKey = checks.ruleKey(check);
if (ruleKey == null) {
continue;
}

this.saveIssues(context, ruleKey, check, issues, inputFile);
}

// Save highlighted tokens.
Expand Down Expand Up @@ -178,16 +185,13 @@ private void saveMetric(

private void saveIssues(
final SensorContext context,
final MagikCheck check,
final List<MagikIssue> issues,
final RuleKey ruleKey,
final MagikCheck magikCheck,
final List<MagikIssue> magikIssues,
final InputFile inputFile) {
for (final MagikIssue magikIssue : issues) {
for (final MagikIssue magikIssue : magikIssues) {
LOGGER.debug("Saving issue, file: {}, issue: {}", inputFile, magikIssue);

final RuleKey ruleKey = this.checks.ruleKey(check);
if (ruleKey == null) {
continue;
}
final NewIssue issue = context.newIssue();
final NewIssueLocation location = issue.newLocation()
.on(inputFile)
Expand Down

0 comments on commit e7b0cfb

Please sign in to comment.