Search in sources :

Example 6 with Symbol

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

the class IgnoredReturnValueCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree expr = ((ExpressionStatementTree) tree).expression();
    if (expr.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expr;
        if (isExcluded(mit)) {
            return;
        }
        Symbol methodSymbol = mit.symbol();
        if (!isVoidOrUnknown(mit.symbolType()) && isCheckedType(methodSymbol.owner().type()) && methodSymbol.isPublic() && !isConstructor(methodSymbol)) {
            IdentifierTree methodName = ExpressionUtils.methodName(mit);
            reportIssue(methodName, "The return value of \"" + methodName.name() + "\" must be used.");
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 7 with Symbol

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

the class HiddenFieldCheck method isInStaticInnerClass.

private static boolean isInStaticInnerClass(VariableTree hiddenVariable, VariableTree variableTree) {
    Symbol hiddenVariableOwner = hiddenVariable.symbol().owner();
    Symbol owner = variableTree.symbol().owner();
    while (!owner.equals(hiddenVariableOwner)) {
        if (owner.isTypeSymbol() && owner.isStatic()) {
            return true;
        }
        owner = owner.owner();
    }
    return false;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 8 with Symbol

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

the class EqualsArgumentTypeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (methodTree.block() != null && "equals".equals(methodTree.symbol().name()) && methodTree.parameters().size() == 1) {
        Symbol parameterSymbol = methodTree.parameters().get(0).symbol();
        if (parameterSymbol.type().is("java.lang.Object")) {
            CastVisitor castVisitor = new CastVisitor(parameterSymbol);
            methodTree.accept(castVisitor);
            if (castVisitor.hasUncheckedCast) {
                reportIssue(methodTree.simpleName(), "Add a type test to this method.");
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 9 with Symbol

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

the class FileCreateTempFileCheck method checkAndAdvanceState.

@Nullable
private State checkAndAdvanceState(MethodInvocationTree mit, State requiredState, State nextState) {
    ExpressionTree methodSelect = mit.methodSelect();
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        ExpressionTree expressionTree = ((MemberSelectExpressionTree) methodSelect).expression();
        if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
            Symbol symbol = ((IdentifierTree) expressionTree).symbol();
            Map<Symbol, State> symbolStateMap = symbolStack.peek();
            if (symbolStateMap != null && symbolStateMap.containsKey(symbol) && requiredState.equals(symbolStateMap.get(symbol))) {
                symbolStateMap.put(symbol, nextState);
                return nextState;
            }
        }
    }
    return null;
}
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) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Nullable(javax.annotation.Nullable)

Example 10 with Symbol

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

the class DefaultEncodingUsageCheck method onConstructorFound.

@Override
protected void onConstructorFound(NewClassTree newClassTree) {
    Symbol symbol = newClassTree.constructorSymbol();
    if (symbol.isMethodSymbol()) {
        Symbol.MethodSymbol constructor = (Symbol.MethodSymbol) symbol;
        String signature = constructor.owner().name() + "(" + Joiner.on(',').join(constructor.parameterTypes()) + ")";
        reportIssue(newClassTree.identifier(), "Remove this use of constructor \"" + signature + "\"");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

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