From e7b0cfb9deff9105b2ddab79eed48e0e8d18d8a4 Mon Sep 17 00:00:00 2001 From: Steven Looman Date: Fri, 4 Nov 2022 16:08:37 +0100 Subject: [PATCH] Fix MagikSquidSensor --- .vscode/launch.json | 8 +++++ CHANGES.md | 1 + .../nl/ramsolutions/sw/magik/MagikFile.java | 3 ++ .../sw/magik/parser/MagikParser.java | 21 +++++++++++++ .../sw/sonar/sensors/MagikSquidSensor.java | 30 +++++++++++-------- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 472645b4..ed101f39 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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" } ] } \ No newline at end of file diff --git a/CHANGES.md b/CHANGES.md index e1dc5d06..8c4804eb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/magik-squid/src/main/java/nl/ramsolutions/sw/magik/MagikFile.java b/magik-squid/src/main/java/nl/ramsolutions/sw/magik/MagikFile.java index 72ac2dd0..79d5b73f 100644 --- a/magik-squid/src/main/java/nl/ramsolutions/sw/magik/MagikFile.java +++ b/magik-squid/src/main/java/nl/ramsolutions/sw/magik/MagikFile.java @@ -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; diff --git a/magik-squid/src/main/java/nl/ramsolutions/sw/magik/parser/MagikParser.java b/magik-squid/src/main/java/nl/ramsolutions/sw/magik/parser/MagikParser.java index b6a3dff7..3de18f74 100644 --- a/magik-squid/src/main/java/nl/ramsolutions/sw/magik/parser/MagikParser.java +++ b/magik-squid/src/main/java/nl/ramsolutions/sw/magik/parser/MagikParser.java @@ -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; @@ -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. * diff --git a/sonar-magik-plugin/src/main/java/nl/ramsolutions/sw/sonar/sensors/MagikSquidSensor.java b/sonar-magik-plugin/src/main/java/nl/ramsolutions/sw/sonar/sensors/MagikSquidSensor.java index a1400fad..2468f757 100644 --- a/sonar-magik-plugin/src/main/java/nl/ramsolutions/sw/sonar/sensors/MagikSquidSensor.java +++ b/sonar-magik-plugin/src/main/java/nl/ramsolutions/sw/sonar/sensors/MagikSquidSensor.java @@ -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 checks; + private final CheckFactory checkFactory; + // private final Checks checks; private final FileLinesContextFactory fileLinesContextFactory; private final NoSonarFilter noSonarFilter; @@ -60,9 +61,7 @@ public MagikSquidSensor( final CheckFactory checkFactory, final FileLinesContextFactory fileLinesContextFactory, final NoSonarFilter noSonarFilter) { - this.checks = checkFactory - .create(CheckList.REPOSITORY_KEY) - .addAnnotatedChecks((Iterable>) CheckList.getChecks()); + this.checkFactory = checkFactory; this.fileLinesContextFactory = fileLinesContextFactory; this.noSonarFilter = noSonarFilter; } @@ -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 checks = checkFactory + .create(CheckList.REPOSITORY_KEY) + .addAnnotatedChecks((Iterable>) CheckList.getChecks()); + for (final MagikCheck check : checks.all()) { LOGGER.debug("Running check: {}", check); final List 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. @@ -178,16 +185,13 @@ private void saveMetric( private void saveIssues( final SensorContext context, - final MagikCheck check, - final List issues, + final RuleKey ruleKey, + final MagikCheck magikCheck, + final List 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)