Search in sources :

Example 96 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class ExplodedGraphWalker method enqueueExceptionalPaths.

private void enqueueExceptionalPaths(ProgramState ps, Symbol methodSymbol, @Nullable MethodYield methodYield) {
    Set<CFG.Block> exceptionBlocks = ((CFG.Block) node.programPoint.block).exceptions();
    List<CFG.Block> catchBlocks = exceptionBlocks.stream().filter(CFG.Block.IS_CATCH_BLOCK).collect(Collectors.toList());
    SymbolicValue peekValue = ps.peekValue();
    Preconditions.checkState(peekValue instanceof SymbolicValue.ExceptionalSymbolicValue, "Top of stack should always contains exceptional SV");
    SymbolicValue.ExceptionalSymbolicValue exceptionSV = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
    // only consider the first match, as order of catch block is important
    List<CFG.Block> caughtBlocks = catchBlocks.stream().filter(b -> isCaughtByBlock(exceptionSV.exceptionType(), b)).sorted((b1, b2) -> Integer.compare(b2.id(), b1.id())).collect(Collectors.toList());
    if (!caughtBlocks.isEmpty()) {
        caughtBlocks.forEach(b -> enqueue(new ProgramPoint(b), ps, methodYield));
        return;
    }
    // branch to any unchecked exception catch
    catchBlocks.stream().filter(ExplodedGraphWalker::isCatchingUncheckedException).forEach(b -> enqueue(new ProgramPoint(b), ps, methodYield));
    // store the exception as exit value in case of method exit in next block
    ps.storeExitValue();
    // use other exceptional blocks, i.e. finally block and exit blocks
    List<CFG.Block> otherBlocks = exceptionBlocks.stream().filter(CFG.Block.IS_CATCH_BLOCK.negate().or(b -> methodSymbol.isUnknown())).collect(Collectors.toList());
    if (otherBlocks.isEmpty()) {
        // explicitly add the exception branching to method exit
        CFG.Block methodExit = node.programPoint.block.successors().stream().map(b -> (CFG.Block) b).filter(CFG.Block::isMethodExitBlock).findFirst().orElse(exitBlock);
        enqueue(new ProgramPoint(methodExit), ps, true, methodYield);
    } else {
        otherBlocks.forEach(b -> enqueue(new ProgramPoint(b), ps, true, methodYield));
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionUtils(org.sonar.java.model.ExpressionUtils) MethodYield(org.sonar.java.se.xproc.MethodYield) Loggers(org.sonar.api.utils.log.Loggers) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) NullDereferenceCheck(org.sonar.java.se.checks.NullDereferenceCheck) SECheck(org.sonar.java.se.checks.SECheck) RedundantAssignmentsCheck(org.sonar.java.se.checks.RedundantAssignmentsCheck) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Map(java.util.Map) MethodMatcher(org.sonar.java.matcher.MethodMatcher) BooleanConstraint(org.sonar.java.se.constraint.BooleanConstraint) ImmutableSet(com.google.common.collect.ImmutableSet) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) Collection(java.util.Collection) Set(java.util.Set) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) Collectors(java.util.stream.Collectors) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) DebugCheck(org.sonar.java.DebugCheck) StreamConsumedCheck(org.sonar.java.se.checks.StreamConsumedCheck) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) NullableAnnotationUtils.isGloballyAnnotatedParameterNonNull(org.sonar.java.se.NullableAnnotationUtils.isGloballyAnnotatedParameterNonNull) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Joiner(com.google.common.base.Joiner) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) NullableAnnotationUtils.isAnnotatedNonNull(org.sonar.java.se.NullableAnnotationUtils.isAnnotatedNonNull) HashMap(java.util.HashMap) Deque(java.util.Deque) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) NullableAnnotationUtils.isGloballyAnnotatedParameterNullable(org.sonar.java.se.NullableAnnotationUtils.isGloballyAnnotatedParameterNullable) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) JavaType(org.sonar.java.resolve.JavaType) DivisionByZeroCheck(org.sonar.java.se.checks.DivisionByZeroCheck) ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) NoWayOutLoopCheck(org.sonar.java.se.checks.NoWayOutLoopCheck) LiveVariables(org.sonar.java.cfg.LiveVariables) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) LinkedList(java.util.LinkedList) MethodMatcherCollection(org.sonar.java.matcher.MethodMatcherCollection) LinkedHashSet(java.util.LinkedHashSet) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) UnclosedResourcesCheck(org.sonar.java.se.checks.UnclosedResourcesCheck) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Types(org.sonar.java.resolve.Types) Iterator(java.util.Iterator) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) OptionalGetBeforeIsPresentCheck(org.sonar.java.se.checks.OptionalGetBeforeIsPresentCheck) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) NonNullSetToNullCheck(org.sonar.java.se.checks.NonNullSetToNullCheck) CFG(org.sonar.java.cfg.CFG) SemanticModel(org.sonar.java.resolve.SemanticModel) BehaviorCache(org.sonar.java.se.xproc.BehaviorCache) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) LocksNotUnlockedCheck(org.sonar.java.se.checks.LocksNotUnlockedCheck) NullableAnnotationUtils.isAnnotatedNullable(org.sonar.java.se.NullableAnnotationUtils.isAnnotatedNullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 97 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class ExplodedGraphWalker method executeIdentifier.

private void executeIdentifier(IdentifierTree tree) {
    Symbol symbol = tree.symbol();
    SymbolicValue value = programState.getValue(symbol);
    if (value == null) {
        value = constraintManager.createSymbolicValue(tree);
        programState = programState.stackValue(value, symbol);
        learnIdentifierConstraints(tree, value);
    } else {
        programState = programState.stackValue(value, symbol);
    }
    programState = programState.put(symbol, value);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 98 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class ProgramState method resetFieldValues.

ProgramState resetFieldValues(ConstraintManager constraintManager, boolean resetOnlyStaticFields) {
    List<Symbol> fields = new ArrayList<>();
    values.forEach((symbol, symbolicValue) -> {
        if (isField(symbol) && !symbol.isFinal() && (symbol.isStatic() || !resetOnlyStaticFields)) {
            fields.add(symbol);
        }
    });
    ProgramState newProgramState = this;
    for (Symbol field : fields) {
        newProgramState = newProgramState.put(field, constraintManager.createDefaultSymbolicValue());
    }
    return newProgramState;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList)

Example 99 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class ProgramState method put.

@VisibleForTesting
public ProgramState put(Symbol symbol, SymbolicValue value) {
    if (symbol.isUnknown() || isVolatileField(symbol)) {
        return this;
    }
    SymbolicValue oldValue = values.get(symbol);
    if (oldValue == null || oldValue != value) {
        PMap<SymbolicValue, Integer> newReferences = references;
        if (oldValue != null) {
            newReferences = decreaseReference(newReferences, oldValue);
        }
        newReferences = increaseReference(newReferences, value);
        PMap<Symbol, SymbolicValue> newValues = values.put(symbol, value);
        return new ProgramState(newValues, newReferences, constraints, visitedPoints, stack, exitSymbolicValue);
    }
    return this;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 100 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class SymbolMetadataResolve method metaAnnotations.

private List<Symbol> metaAnnotations(Set<Type> knownTypes) {
    List<Symbol> result = new ArrayList<>();
    for (AnnotationInstance annotationInstance : annotations) {
        Symbol annotationSymbol = annotationInstance.symbol();
        Type annotationType = annotationSymbol.type();
        if (!knownTypes.contains(annotationType)) {
            knownTypes.add(annotationType);
            result.add(annotationSymbol);
            result.addAll(((SymbolMetadataResolve) annotationSymbol.metadata()).metaAnnotations(knownTypes));
        }
    }
    return new ArrayList<>(result);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList)

Aggregations

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11