From 86d6a8d9af7567f2dbf7fc1884e69586d1785b9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicklas=20K=C3=B6rtge?= Date: Wed, 11 Sep 2024 12:54:22 +0200 Subject: [PATCH] fix #12 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Nicklas Körtge --- .../ibm/engine/detection/DetectionStore.java | 44 ++++++++++--------- .../issues/DuplicateDependingRules2Test.java | 4 +- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/engine/src/main/java/com/ibm/engine/detection/DetectionStore.java b/engine/src/main/java/com/ibm/engine/detection/DetectionStore.java index 6bacf720..c8e7034a 100644 --- a/engine/src/main/java/com/ibm/engine/detection/DetectionStore.java +++ b/engine/src/main/java/com/ibm/engine/detection/DetectionStore.java @@ -222,14 +222,25 @@ public void attach(int index, @Nonnull final DetectionStore detectio } void addValue(int index, @Nonnull final IValue iValue) { - this.detectionValues.compute( + addValue(this, index, iValue); + } + + void addValue( + @Nonnull DetectionStore detectionStore, + int index, + @Nonnull final IValue iValue) { + detectionStore.detectionValues.compute( index, (i, list) -> { if (list == null) { + // If the list is null, create a new ArrayList + // with the iValue and return it final List> values = new ArrayList<>(); values.add(iValue); return values; } else { + // If the list is not null, add the iValue to it + // and return the modified list list.add(iValue); return list; } @@ -289,7 +300,7 @@ public void onReceivingNewDetection(@Nonnull IDetection detection) { final Optional positionMove = detectableParameter.getShouldBeMovedUnder(); // Check if the parameter should be moved under if (positionMove.isPresent()) { - final Integer id = positionMove.get(); + final int id = positionMove.get(); // Get the iValue to be detected and store it in a variable valueDetection .toValue(valueDetection.detectableParameter().getiValueFactory()) @@ -304,30 +315,15 @@ public void onReceivingNewDetection(@Nonnull IDetection detection) { handler, statusReporting); // Compute the detection values for the given id - detectionStore.detectionValues.compute( - id, - (i, list) -> { - if (list == null) { - // If the list is null, create a new ArrayList - // with the iValue and return it - final List> values = - new ArrayList<>(); - values.add(iValue); - return values; - } else { - // If the list is not null, add the iValue to it - // and return the modified list - list.add(iValue); - return list; - } - }); + addValue(detectionStore, id, iValue); // Attach the detection store to the given id this.attach(id, detectionStore); }); } else { valueDetection .toValue(valueDetection.detectableParameter().getiValueFactory()) - .ifPresent(iValue -> addValue(detectableParameter.getIndex(), iValue)); + .ifPresent( + iValue -> addValue(this, detectableParameter.getIndex(), iValue)); } // follow method parameter related detection rules @@ -350,7 +346,13 @@ public void onReceivingNewDetection(@Nonnull IDetection detection) { // follow invoked object (object where the method was executed on) related detection // rules final TraceSymbol traceSymbol = getAssignedTraceSymbol(valueDetection.expression()); - final List> nextDetectionRules = detectionRule.nextDetectionRules(); + + final List> nextDetectionRules; + if (positionMove.isPresent()) { + nextDetectionRules = List.of(); + } else { + nextDetectionRules = detectionRule.nextDetectionRules(); + } this.statusReporting.addAdditionalExpectedRuleVisits(nextDetectionRules.size()); handler.getLanguageSupport() .getEnclosingMethod(detection.expression()) diff --git a/java/src/test/java/com/ibm/plugin/rules/issues/DuplicateDependingRules2Test.java b/java/src/test/java/com/ibm/plugin/rules/issues/DuplicateDependingRules2Test.java index c862e370..df8b1638 100644 --- a/java/src/test/java/com/ibm/plugin/rules/issues/DuplicateDependingRules2Test.java +++ b/java/src/test/java/com/ibm/plugin/rules/issues/DuplicateDependingRules2Test.java @@ -41,7 +41,7 @@ import org.sonar.plugins.java.api.semantic.Symbol; import org.sonar.plugins.java.api.tree.Tree; -public class DuplicateDependingRules2Test extends TestBase { +class DuplicateDependingRules2Test extends TestBase { static IDetectionContext detectionContext = new IDetectionContext() { @@ -96,7 +96,7 @@ public void asserts( List> stores = getStoresOfValueType(Algorithm.class, detectionStore.getChildren()); - assertThat(stores).hasSize(3); + assertThat(stores).hasSize(2); DetectionStore store_1 = stores.get(0); assertThat(store_1.getDetectionValues()).hasSize(1);