Search in sources :

Example 31 with Symbol

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

the class ExplodedGraphWalker method executeVariable.

/**
 * @see JLS8 4.12.5 for details
 */
private void executeVariable(VariableTree variableTree, @Nullable Tree terminator) {
    Symbol variableSymbol = variableTree.symbol();
    if (variableTree.initializer() == null) {
        SymbolicValue sv = null;
        if (terminator != null && terminator.is(Tree.Kind.FOR_EACH_STATEMENT)) {
            sv = constraintManager.createSymbolicValue(variableTree);
            if (isAnnotatedNonNull(variableSymbol)) {
                programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
            }
        } else if (variableTree.parent().is(Tree.Kind.CATCH)) {
            sv = handleCatchVariable(variableSymbol.type());
            // an exception have been thrown and caught, stack must be cleared
            // see notes in JVMS 8 - ยง6.5. - instruction "athrow"
            programState = programState.clearStack();
            // exception variable is not null by definition
            programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
        }
        if (sv != null) {
            programState = programState.put(variableSymbol, sv);
        }
    } else {
        ProgramState.Pop unstack = programState.unstackValue(1);
        programState = unstack.state;
        programState = programState.put(variableSymbol, unstack.values.get(0));
    }
}
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 32 with Symbol

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

the class FlowComputation method run.

private Set<Flow> run(final ExplodedGraph.Node node, PSet<Symbol> trackedSymbols) {
    Set<Flow> flows = new HashSet<>();
    Deque<ExecutionPath> workList = new ArrayDeque<>();
    SameConstraints sameConstraints = new SameConstraints(node, trackedSymbols, domains);
    node.edges().stream().flatMap(e -> startPath(e, trackedSymbols, sameConstraints)).forEach(workList::push);
    int flowSteps = 0;
    Set<ExecutionPath> visited = new HashSet<>(workList);
    while (!workList.isEmpty()) {
        ExecutionPath path = workList.pop();
        if (path.finished) {
            flows.add(path.flow);
        } else {
            path.lastEdge.parent.edges().stream().filter(path::notVisited).flatMap(path::addEdge).forEach(ep -> {
                if (visited.add(ep)) {
                    workList.push(ep);
                }
            });
        }
        flowSteps++;
        if (flowSteps == MAX_FLOW_STEPS) {
            LOG.debug("Flow was not able to complete");
            break;
        }
    }
    return flows;
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) ExpressionUtils(org.sonar.java.model.ExpressionUtils) MethodYield(org.sonar.java.se.xproc.MethodYield) Loggers(org.sonar.api.utils.log.Loggers) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Map(java.util.Map) Arguments(org.sonar.plugins.java.api.tree.Arguments) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) Collection(java.util.Collection) SyntaxTreeNameFinder(org.sonar.java.se.checks.SyntaxTreeNameFinder) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) Optional(java.util.Optional) Constraint(org.sonar.java.se.constraint.Constraint) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) IntStream(java.util.stream.IntStream) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Deque(java.util.Deque) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Lists(com.google.common.collect.Lists) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) PSet(org.sonar.java.collections.PSet) Node(org.sonar.java.se.ExplodedGraph.Node) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) PCollections(org.sonar.java.collections.PCollections) CFG(org.sonar.java.cfg.CFG) Preconditions(com.google.common.base.Preconditions) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) HappyPathYield(org.sonar.java.se.xproc.HappyPathYield) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ArrayDeque(java.util.ArrayDeque) Constraint(org.sonar.java.se.constraint.Constraint) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) HashSet(java.util.HashSet)

Example 33 with Symbol

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

the class FlowComputation method flow.

private static Set<Flow> flow(ExplodedGraph.Node currentNode, Set<SymbolicValue> symbolicValues, Predicate<Constraint> addToFlow, Predicate<Constraint> terminateTraversal, List<Class<? extends Constraint>> domains, Set<Symbol> symbols, boolean skipExceptionMessages) {
    Set<SymbolicValue> allSymbolicValues = symbolicValues.stream().map(FlowComputation::computedFrom).flatMap(Set::stream).collect(Collectors.toSet());
    PSet<Symbol> trackedSymbols = PCollections.emptySet();
    for (Symbol symbol : symbols) {
        trackedSymbols = trackedSymbols.add(symbol);
    }
    if (symbols.isEmpty()) {
        for (SymbolicValue symbolicValue : symbolicValues) {
            for (Symbol symbol : symbolicValue.computedFromSymbols()) {
                trackedSymbols = trackedSymbols.add(symbol);
            }
        }
    }
    FlowComputation flowComputation = new FlowComputation(allSymbolicValues, addToFlow, terminateTraversal, domains, skipExceptionMessages);
    return flowComputation.run(currentNode, trackedSymbols);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinarySymbolicValue(org.sonar.java.se.symbolicvalues.BinarySymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 34 with Symbol

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

the class TypeAndReferenceSolver method addConstantValue.

private static void addConstantValue(AnnotationTree tree, AnnotationInstanceResolve annotationInstance) {
    Collection<Symbol> scopeSymbols = tree.annotationType().symbolType().symbol().memberSymbols();
    for (ExpressionTree expressionTree : tree.arguments()) {
        String name = "";
        for (Symbol scopeSymbol : scopeSymbols) {
            if (scopeSymbol.isMethodSymbol()) {
                name = scopeSymbol.name();
                break;
            }
        }
        annotationInstance.addValue(new AnnotationValueResolve(name, expressionTree));
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

Example 35 with Symbol

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

the class LeastUpperBound method supertypes.

@VisibleForTesting
Set<Type> supertypes(JavaType type) {
    List<Type> result = new ArrayList<>();
    result.add(type);
    Symbol.TypeSymbol symbol = type.symbol();
    TypeSubstitution substitution = getTypeSubstitution(type);
    if (substitution.size() == 0 && !((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.isEmpty()) {
        // raw type : let's create a substitution based on erasures
        TypeSubstitution ts = new TypeSubstitution();
        ((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.forEach(t -> ts.add(t, t.erasure()));
        substitution = ts;
    }
    result.addAll(interfacesWithSubstitution(symbol, substitution));
    Type superClass = symbol.superClass();
    while (superClass != null) {
        JavaType substitutedSuperClass = applySubstitution(superClass, substitution);
        result.add(substitutedSuperClass);
        substitution = getTypeSubstitution(substitutedSuperClass);
        JavaSymbol.TypeJavaSymbol superClassSymbol = substitutedSuperClass.getSymbol();
        result.addAll(interfacesWithSubstitution(superClassSymbol, substitution));
        superClass = superClassSymbol.superClass();
    }
    return new LinkedHashSet<>(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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