Search in sources :

Example 61 with Type

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

the class CallSuperInTestCaseCheck method requiresSuperCall.

private static boolean requiresSuperCall(Symbol.MethodSymbol methodSymbol) {
    Type superType = methodSymbol.owner().type().symbol().superClass();
    Collection<Symbol> symbols = Lists.newArrayList();
    while (superType != null && !superType.is(JUNIT_FRAMEWORK_TEST_CASE) && symbols.isEmpty()) {
        symbols = superType.symbol().lookupSymbols(methodSymbol.name());
        superType = superType.symbol().superClass();
    }
    return !symbols.isEmpty() && !symbols.iterator().next().owner().type().is(JUNIT_FRAMEWORK_TEST_CASE);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 62 with Type

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

the class StringToPrimitiveConversionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTreeImpl variableTree = (VariableTreeImpl) tree;
            Type variableType = variableTree.type().symbolType();
            PrimitiveCheck primitiveCheck = getPrimitiveCheck(variableType);
            ExpressionTree initializer = variableTree.initializer();
            if (primitiveCheck != null && initializer != null) {
                primitiveCheck.checkInstantiation(initializer);
            }
        } else {
            MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
            for (PrimitiveCheck primitiveCheck : primitiveChecks) {
                primitiveCheck.checkMethodInvocation(methodInvocationTree);
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl)

Example 63 with Type

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

the class SubClassStaticReferenceCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    Type classType = classTree.symbol().type();
    List<Tree> members = classTree.members();
    // JLS 12.4. Initialization of Classes and Interfaces:
    // Initialization of a class consists of executing its static initializers and the initializers for static fields (class variables)
    // declared in the class.
    checkStaticVariables(members, classType);
    checkStaticInitializers(members, classType);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 64 with Type

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

the class ThreadAsRunnableArgumentCheck method checkArgumentsTypes.

private void checkArgumentsTypes(List<ExpressionTree> arguments, MethodJavaSymbol methodSymbol) {
    List<Type> parametersTypes = methodSymbol.parameterTypes();
    // FIXME As arguments are not handled for method resolution using static imports, the provided methodSymbol may not match.
    if (!parametersTypes.isEmpty()) {
        for (int index = 0; index < arguments.size(); index++) {
            ExpressionTree argument = arguments.get(index);
            Type providedType = argument.symbolType();
            if (!argument.is(Kind.NULL_LITERAL) && isThreadAsRunnable(providedType, parametersTypes, index, methodSymbol.isVarArgs())) {
                reportIssue(argument, getMessage(argument, providedType, index));
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree)

Example 65 with Type

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

the class ThrowCheckedExceptionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.METHOD)) {
        methods.push((MethodTree) tree);
    } else {
        ThrowStatementTree throwStatementTree = (ThrowStatementTree) tree;
        Type symbolType = throwStatementTree.expression().symbolType();
        if (symbolType.isSubtypeOf("java.lang.Exception") && !symbolType.isSubtypeOf("java.lang.RuntimeException") && !isFromMethodOverride(symbolType)) {
            reportIssue(throwStatementTree.expression(), "Remove the usage of the checked exception '" + symbolType.name() + "'.");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8