Search in sources :

Example 1 with Type

use of org.sonar.plugins.java.api.semantic.Type in project JavaForFun by gumartinm.

the class ParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodTree method = (MethodTree) tree;
    if (method.parameters().size() == 1) {
        MethodSymbol symbol = method.symbol();
        Type firstParameterType = symbol.parameterTypes().get(0);
        Type returnType = symbol.returnType().type();
        if (returnType.is(firstParameterType.fullyQualifiedName())) {
            reportIssue(method.simpleName(), "Remove this method");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 2 with Type

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

the class InstanceOfAlwaysTrueCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    InstanceOfTree instanceOfTree = (InstanceOfTree) tree;
    Type expressionType = instanceOfTree.expression().symbolType();
    Type instanceOf = instanceOfTree.type().symbolType();
    if (expressionType.isSubtypeOf(instanceOf) && !instanceOfTree.expression().is(Tree.Kind.NULL_LITERAL)) {
        reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". ");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree)

Example 3 with Type

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

the class InterruptedExceptionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TryStatementTree tryStatementTree = (TryStatementTree) tree;
    withinInterruptingFinally.addFirst(isFinallyInterrupting(tryStatementTree.finallyBlock()));
    for (CatchTree catchTree : tryStatementTree.catches()) {
        Type catchType = catchTree.parameter().symbol().type();
        if (catchType.is("java.lang.InterruptedException") || catchType.is("java.lang.ThreadDeath")) {
            BlockVisitor blockVisitor = new BlockVisitor(catchTree.parameter().symbol());
            catchTree.block().accept(blockVisitor);
            if (!blockVisitor.threadInterrupted && !isWithinInterruptingFinally()) {
                reportIssue(catchTree.parameter(), "Either re-interrupt this method or rethrow the \"" + catchType.name() + "\".");
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree)

Example 4 with Type

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

the class InterfaceOrSuperclassShadowingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    if (hasSemantic()) {
        Symbol.TypeSymbol classSymbol = classTree.symbol();
        checkSuperType(classTree, classSymbol.superClass());
        for (Type interfaceType : classSymbol.interfaces()) {
            checkSuperType(classTree, interfaceType);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 5 with Type

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

the class EnumMapCheck method hasEnumKey.

private static boolean hasEnumKey(Type symbolType) {
    Type type = symbolType;
    if (type instanceof MethodJavaType) {
        type = ((MethodJavaType) type).resultType();
    }
    if (type instanceof ParametrizedTypeJavaType) {
        ParametrizedTypeJavaType parametrizedTypeJavaType = (ParametrizedTypeJavaType) type;
        List<TypeVariableJavaType> typeParameters = parametrizedTypeJavaType.typeParameters();
        if (!typeParameters.isEmpty()) {
            return parametrizedTypeJavaType.substitution(typeParameters.get(0)).symbol().isEnum();
        }
    }
    return false;
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

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