use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class StreamNotConsumedCheck method checkEndOfExecutionPath.
@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
if (context.getState().exitValue() instanceof SymbolicValue.ExceptionalSymbolicValue) {
// don't report when exiting on exception
return;
}
ProgramState state = context.getState();
List<SymbolicValue> notConsumed = state.getValuesWithConstraints(NOT_CONSUMED);
notConsumed.forEach(sv -> {
Set<Flow> flows = FlowComputation.flow(context.getNode(), Collections.singleton(sv), NOT_CONSUMED::equals, NOT_CONSUMED::equals, Collections.singletonList(StreamConsumedCheck.StreamPipelineConstraint.class), Collections.emptySet());
Flow flow = flows.iterator().next();
JavaFileScannerContext.Location location = flow.elements().get(0);
reportIssue(location.syntaxNode, "Refactor the code so this stream pipeline is used.");
});
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class MapComputeIfAbsentOrPresentCheck method checkPostStatement.
@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
if (MAP_GET.matches(mit)) {
ProgramState psBeforeInvocation = context.getNode().programState;
ProgramState psAfterInvocation = context.getState();
SymbolicValue keySV = psBeforeInvocation.peekValue(0);
SymbolicValue mapSV = psBeforeInvocation.peekValue(1);
SymbolicValue valueSV = psAfterInvocation.peekValue();
mapGetInvocations.put(mapSV, new MapGetInvocation(valueSV, keySV, mit));
}
}
return super.checkPostStatement(context, syntaxNode);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class NonNullSetToNullCheck method isUndefinedOrNull.
private static boolean isUndefinedOrNull(CheckerContext context, Symbol symbol) {
ProgramState programState = context.getState();
SymbolicValue value = programState.getValue(symbol);
return value == null;
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class ExceptionalYieldChecker method flowsForMethodArguments.
private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
ProgramState programState = node.programState;
List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
Set<Symbol> argSymbols = new LinkedHashSet<>();
arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
argSymbolicValues.add(svs.symbolicValue());
Symbol symbol = svs.symbol();
if (symbol != null) {
argSymbols.add(symbol);
}
});
List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
use of org.sonar.java.se.ProgramState in project sonar-java by SonarSource.
the class NullDereferenceCheck method setNullConstraint.
private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) {
SymbolicValue val = context.getState().peekValue();
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
Preconditions.checkNotNull(val);
List<ProgramState> states = new ArrayList<>();
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL));
states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL));
return states;
}
return Lists.newArrayList(context.getState());
}
Aggregations