use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class MapComputeIfAbsentOrPresentCheck method checkPreStatement.
@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
if (MAP_PUT.matches(mit) && !isMethodInvocationThrowingCheckedException(mit.arguments().get(1))) {
ProgramState ps = context.getState();
SymbolicValue keySV = ps.peekValue(1);
SymbolicValue mapSV = ps.peekValue(2);
mapGetInvocations.get(mapSV).stream().filter(getOnSameMap -> getOnSameMap.withSameKey(keySV)).findAny().ifPresent(getOnSameMap -> {
ObjectConstraint constraint = ps.getConstraint(getOnSameMap.value, ObjectConstraint.class);
if (constraint != null && isInsideIfStatementWithNullCheckWithoutElse(mit)) {
checkIssues.add(new CheckIssue(context.getNode(), getOnSameMap.mit, mit, getOnSameMap.value, constraint));
}
});
}
}
return super.checkPreStatement(context, syntaxNode);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class OptionalGetBeforeIsPresentCheck method setOptionalConstraint.
private static List<ProgramState> setOptionalConstraint(CheckerContext context, Tree syntaxNode) {
ProgramState programState = context.getState();
if (!syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
return Collections.singletonList(programState);
}
MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
SymbolicValue peekValue = programState.peekValue();
Preconditions.checkNotNull(peekValue);
if (OPTIONAL_EMPTY.matches(mit)) {
return peekValue.setConstraint(programState, OptionalConstraint.NOT_PRESENT);
}
if (OPTIONAL_OF.matches(mit)) {
return peekValue.setConstraint(programState, OptionalConstraint.PRESENT);
}
if (OPTIONAL_OF_NULLABLE.matches(mit)) {
ProgramState psPriorMethodInvocation = context.getNode().programState;
SymbolicValue paramSV = psPriorMethodInvocation.peekValue(0);
ObjectConstraint paramConstraint = psPriorMethodInvocation.getConstraint(paramSV, ObjectConstraint.class);
if (paramConstraint != null) {
// Optional.ofNullable(null) returns an empty Optional
return peekValue.setConstraint(programState, paramConstraint == ObjectConstraint.NULL ? OptionalConstraint.NOT_PRESENT : OptionalConstraint.PRESENT);
}
}
return Collections.singletonList(programState);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class RedundantAssignmentsCheck method handleAssignment.
private void handleAssignment(CheckerContext context, AssignmentExpressionTree assignmentExpressionTree) {
SymbolicValueSymbol assignedVariable = context.getState().peekValueSymbol();
Symbol assignedSymbol = assignedVariable.symbol();
if (assignedSymbol == null || // meaning that 'stream = stream.map(...);' would be detected as redundant assignment if not explicitly excluded
STREAM_TYPES.stream().anyMatch(assignedSymbol.type()::is)) {
return;
}
ExplodedGraph.Node node = context.getNode();
ProgramState previousState = node.programState;
SymbolicValue oldValue = previousState.getValue(assignedSymbol);
SymbolicValue newValue = assignedVariable.symbolicValue();
Symbol fromSymbol = previousState.peekValueSymbol().symbol();
assignmentsByMethod.peek().put(assignmentExpressionTree, new AssignmentDataHolder(assignedSymbol, oldValue, newValue, fromSymbol, node));
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class ExceptionalYieldChecker method hasConstraintOtherThanNonNull.
private static boolean hasConstraintOtherThanNonNull(ProgramState.SymbolicValueSymbol svs, ProgramState ps) {
SymbolicValue sv = svs.symbolicValue();
ConstraintsByDomain constraints = ps.getConstraints(sv);
return constraints != null && !hasOnlyNonNullConstraint(constraints);
}
use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.
the class StreamConsumedCheck method handleMethodInvocation.
private ProgramState handleMethodInvocation(CheckerContext context, MethodInvocationTree mit) {
ProgramState programState = context.getState();
programState = removeConstraintOnArgs(programState, mit.arguments().size());
SymbolicValue invocationTarget = invocationTarget(programState, mit);
if ((isIntermediateOperation(mit) || isTerminalOperation(mit)) && isPipelineConsumed(programState, invocationTarget)) {
reportIssue(mit, "Refactor this code so that this consumed stream pipeline is not reused.", flow(invocationTarget, context.getNode()));
return null;
}
if (isIntermediateOperation(mit)) {
// intermediate operations return same stream pipeline, so we reuse SV
context.getConstraintManager().setValueFactory(() -> invocationTarget);
return Iterables.getOnlyElement(invocationTarget.setConstraint(programState, StreamPipelineConstraint.NOT_CONSUMED));
}
if (isTerminalOperation(mit)) {
return Iterables.getOnlyElement(invocationTarget.setConstraint(programState, StreamPipelineConstraint.CONSUMED));
}
if (mit.symbol().isUnknown()) {
// lambdas used in pipelines are sometimes not resolved properly, this is to shutdown the noise
programState = programState.removeConstraintsOnDomain(invocationTarget, StreamPipelineConstraint.class);
}
return programState;
}
Aggregations