Search in sources :

Example 71 with Symbol

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

the class SelectorMethodArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (Boolean.TRUE.equals(methodTree.isOverriding())) {
        return;
    }
    List<Symbol> booleanParameterSymbols = getBooleanParametersAsSymbol(methodTree.parameters());
    BlockTree blockTree = methodTree.block();
    if (isPublic(methodTree) && blockTree != null && !booleanParameterSymbols.isEmpty()) {
        for (Symbol variable : booleanParameterSymbols) {
            Collection<IdentifierTree> usages = variable.usages();
            if (usages.size() == 1) {
                blockTree.accept(new ConditionalStatementVisitor(variable.name(), Iterables.get(usages, 0), methodTree));
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 72 with Symbol

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

the class SimpleClassNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    CompilationUnitTree cut = (CompilationUnitTree) tree;
    cut.types().stream().filter(NOT_EMPTY_STATEMENT).map(t -> ((ClassTree) t).symbol()).forEach(this::checkSymbol);
    List<ImportTree> imports = cut.imports().stream().filter(NOT_EMPTY_STATEMENT).map(t -> (ImportTree) t).collect(Collectors.toList());
    boolean fileContainsStarImport = imports.stream().filter(it -> it.qualifiedIdentifier().is(Kind.MEMBER_SELECT)).map(it -> ((MemberSelectExpressionTree) it.qualifiedIdentifier()).identifier()).anyMatch(i -> "*".equals(i.name()));
    if (!fileContainsStarImport) {
        checkImports(imports);
    }
}
Also used : Kind(org.sonar.plugins.java.api.tree.Tree.Kind) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) Predicate(java.util.function.Predicate) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SemanticModel(org.sonar.java.resolve.SemanticModel) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree)

Example 73 with Symbol

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

the class SecureCookieCheck method isSetSecureCall.

private static boolean isSetSecureCall(MethodInvocationTree mit) {
    Symbol methodSymbol = mit.symbol();
    boolean hasArityOne = mit.arguments().size() == 1;
    if (hasArityOne && isCallSiteCookie(methodSymbol)) {
        ExpressionTree expressionTree = mit.arguments().get(0);
        if (expressionTree.is(Tree.Kind.BOOLEAN_LITERAL) && "false".equals(((LiteralTree) expressionTree).value())) {
            return false;
        }
        return "setSecure".equals(getIdentifier(mit).name());
    }
    return false;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 74 with Symbol

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

the class SecureCookieCheck method checkSecureCall.

private void checkSecureCall(MethodInvocationTree mit) {
    if (isSetSecureCall(mit) && mit.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mse = (MemberSelectExpressionTree) mit.methodSelect();
        if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
            Symbol reference = ((IdentifierTree) mse.expression()).symbol();
            unsecuredCookies.remove(reference);
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 75 with Symbol

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

the class ReplaceLambdaByMethodRefCheck method hasNonFinalFieldInMethodSelect.

private static boolean hasNonFinalFieldInMethodSelect(MethodInvocationTree mit) {
    MemberSelectExpressionTree mse = getMemberSelect(mit);
    if (mse == null) {
        return false;
    }
    ExpressionTree expression = ExpressionUtils.skipParentheses(mse.expression());
    Symbol symbol = null;
    if (expression.is(Tree.Kind.IDENTIFIER)) {
        symbol = ((IdentifierTree) expression).symbol();
    } else if (expression.is(Tree.Kind.MEMBER_SELECT)) {
        symbol = ((MemberSelectExpressionTree) expression).identifier().symbol();
    }
    return symbol != null && symbol.owner().isTypeSymbol() && !isThisOrSuper(symbol.name()) && !symbol.isFinal();
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

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