Search in sources :

Example 66 with Symbol

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

the class StaticFieldUpdateInConstructorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree constructor = (MethodTree) tree;
    Symbol.TypeSymbol owner = constructor.symbol().enclosingClass();
    Set<Symbol> staticFields = owner.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isStatic).collect(Collectors.toSet());
    StaticFieldUpdateVisitor visitor = new StaticFieldUpdateVisitor(staticFields);
    constructor.block().accept(visitor);
    visitor.assignedStaticFields().forEach(identifierTree -> {
        Symbol staticField = identifierTree.symbol();
        reportIssue(identifierTree, "Remove this assignment of \"" + staticField.name() + "\".", Collections.singletonList(new JavaFileScannerContext.Location("", staticField.declaration())), null);
    });
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 67 with Symbol

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

the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.

private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
    ExpressionTree arg = null;
    ExpressionTree expression = null;
    if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
        Symbol symbol = mit.symbol();
        ExpressionTree methodSelect = mit.methodSelect();
        if (mit.arguments().size() == 1) {
            arg = mit.arguments().get(0);
            if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
                expression = ((MemberSelectExpressionTree) methodSelect).expression();
            }
        }
    } else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
        arg = equalTo.leftOperand();
        expression = equalTo.rightOperand();
    }
    if (arg != null && expression != null) {
        if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
            return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
        } else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
            return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
        }
    }
    return Optional.empty();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 68 with Symbol

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

the class SynchronizedFieldAssignmentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    SynchronizedStatementTree sst = (SynchronizedStatementTree) tree;
    if (sst.expression().is(Kind.NEW_CLASS)) {
        reportIssue(tree, "Synchronizing on a new instance is a no-op.");
        return;
    }
    Symbol field = getField(sst.expression());
    if (field != null) {
        sst.block().accept(new AssignmentVisitor(field, sst.expression()));
    } else {
        Symbol parameter = getParam(sst.expression());
        if (parameter != null) {
            reportIssue(tree, String.format("\"%s\" is a method parameter, and should not be used for synchronization.", parameter.name()));
        }
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree)

Example 69 with Symbol

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

the class TooManyMethodsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    List<Tree> methods = classTree.members().stream().filter(member -> member.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR) && (countNonPublic || ((MethodTree) member).symbol().isPublic())).collect(Collectors.toList());
    if (shouldNotReportIssue(classTree, methods)) {
        return;
    }
    List<JavaFileScannerContext.Location> secondary = methods.stream().map(element -> new JavaFileScannerContext.Location("Method + 1", element)).collect(Collectors.toList());
    String classDescription;
    if (classTree.simpleName() == null) {
        classDescription = "Anonymous class \"" + ((NewClassTree) classTree.parent()).identifier().symbolType().name() + "\"";
    } else {
        classDescription = classTree.declarationKeyword().text() + " \"" + classTree.simpleName() + "\"";
    }
    reportIssue(ExpressionsHelper.reportOnClassTree(classTree), String.format("%s has %d%s methods, which is greater than the %d authorized. Split it into smaller classes.", classDescription, methods.size(), countNonPublic ? "" : " public", maximumMethodThreshold), secondary, null);
}
Also used : RuleProperty(org.sonar.check.RuleProperty) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ExpressionsHelper(org.sonar.java.checks.helpers.ExpressionsHelper) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 70 with Symbol

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

the class ThreadAsRunnableArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    List<ExpressionTree> arguments;
    Symbol methodSymbol;
    if (tree.is(Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) tree;
        methodSymbol = nct.constructorSymbol();
        arguments = nct.arguments();
    } else {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        methodSymbol = mit.symbol();
        arguments = mit.arguments();
    }
    if (!arguments.isEmpty() && methodSymbol.isMethodSymbol()) {
        checkArgumentsTypes(arguments, (MethodJavaSymbol) methodSymbol);
    }
}
Also used : MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

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