Skip to content

Commit

Permalink
fix #12
Browse files Browse the repository at this point in the history
Signed-off-by: Nicklas Körtge <[email protected]>
  • Loading branch information
n1ckl0sk0rtge committed Sep 11, 2024
1 parent af7d128 commit 86d6a8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
44 changes: 23 additions & 21 deletions engine/src/main/java/com/ibm/engine/detection/DetectionStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,25 @@ public void attach(int index, @Nonnull final DetectionStore<R, T, S, P> detectio
}

void addValue(int index, @Nonnull final IValue<T> iValue) {
this.detectionValues.compute(
addValue(this, index, iValue);
}

void addValue(
@Nonnull DetectionStore<R, T, S, P> detectionStore,
int index,
@Nonnull final IValue<T> 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<IValue<T>> 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;
}
Expand Down Expand Up @@ -289,7 +300,7 @@ public void onReceivingNewDetection(@Nonnull IDetection<T> detection) {
final Optional<Integer> 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())
Expand All @@ -304,30 +315,15 @@ public void onReceivingNewDetection(@Nonnull IDetection<T> 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<IValue<T>> 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
Expand All @@ -350,7 +346,13 @@ public void onReceivingNewDetection(@Nonnull IDetection<T> detection) {
// follow invoked object (object where the method was executed on) related detection
// rules
final TraceSymbol<S> traceSymbol = getAssignedTraceSymbol(valueDetection.expression());
final List<IDetectionRule<T>> nextDetectionRules = detectionRule.nextDetectionRules();

final List<IDetectionRule<T>> nextDetectionRules;
if (positionMove.isPresent()) {
nextDetectionRules = List.of();
} else {
nextDetectionRules = detectionRule.nextDetectionRules();
}
this.statusReporting.addAdditionalExpectedRuleVisits(nextDetectionRules.size());
handler.getLanguageSupport()
.getEnclosingMethod(detection.expression())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -96,7 +96,7 @@ public void asserts(

List<DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext>> stores =
getStoresOfValueType(Algorithm.class, detectionStore.getChildren());
assertThat(stores).hasSize(3);
assertThat(stores).hasSize(2);

DetectionStore<JavaCheck, Tree, Symbol, JavaFileScannerContext> store_1 = stores.get(0);
assertThat(store_1.getDetectionValues()).hasSize(1);
Expand Down

0 comments on commit 86d6a8d

Please sign in to comment.