Search in sources :

Example 56 with Type

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

the class RawExceptionCheck method visitThrowStatement.

@Override
public void visitThrowStatement(ThrowStatementTree tree) {
    if (tree.expression().is(Tree.Kind.NEW_CLASS)) {
        TypeTree exception = ((NewClassTree) tree.expression()).identifier();
        Type symbolType = exception.symbolType();
        if (symbolType instanceof MethodJavaType) {
            symbolType = ((MethodJavaType) exception.symbolType()).resultType();
        }
        if (isRawException(symbolType)) {
            reportIssue(exception);
        }
    }
    super.visitThrowStatement(tree);
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 57 with Type

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

the class OutputStreamOverrideWriteCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    ClassTree classTree = (ClassTree) tree;
    Type superType = classTree.symbol().superClass();
    IdentifierTree className = classTree.simpleName();
    if (className == null || classTree.symbol().isAbstract() || superType == null || !(superType.is("java.io.OutputStream") || superType.is("java.io.FilterOutputStream"))) {
        return;
    }
    Optional<MethodTree> writeByteIntInt = findMethod(classTree, WRITE_BYTES_INT_INT);
    Optional<MethodTree> writeInt = findMethod(classTree, WRITE_INT);
    if (!writeByteIntInt.isPresent()) {
        String message = "Provide an override of \"write(byte[],int,int)\" for this class.";
        if (writeInt.isPresent()) {
            MethodTree writeIntTree = writeInt.get();
            if (writeIntTree.block().body().isEmpty()) {
                message = "Provide an empty override of \"write(byte[],int,int)\" for this class as well.";
            }
        }
        reportIssue(className, message);
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 58 with Type

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

the class ObjectCreatedOnlyToCallGetClassCheck method getTypeName.

private static String getTypeName(ExpressionTree tree) {
    Type type = tree.symbolType();
    String name = getTypeName(type);
    if (name.isEmpty()) {
        name = getAnonymousClassTypeName(type.symbol());
    }
    return name;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type)

Example 59 with Type

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

the class CompareObjectWithEqualsCheck method parentClassImplementsEquals.

private static boolean parentClassImplementsEquals(Symbol.TypeSymbol symbol) {
    Type superClass = symbol.superClass();
    while (superClass != null && superClass.symbol().isTypeSymbol()) {
        Symbol.TypeSymbol superClassSymbol = superClass.symbol();
        if (!superClass.is(JAVA_LANG_OBJECT) && hasEqualsMethod(superClassSymbol)) {
            return true;
        }
        superClass = superClassSymbol.superClass();
    }
    return false;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 60 with Type

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

the class ConfusingOverloadCheck method confusingOverload.

private static boolean confusingOverload(Symbol.MethodSymbol methodSymbol, Symbol.MethodSymbol methodWithSameName) {
    if (methodSymbol.isStatic()) {
        return false;
    }
    List<Type> argTypes = methodSymbol.parameterTypes();
    List<Type> parameterTypes = methodWithSameName.parameterTypes();
    if (argTypes.size() != parameterTypes.size()) {
        return false;
    }
    for (int i = 0; i < argTypes.size(); i++) {
        Type argType = argTypes.get(i);
        if (argType.isUnknown() || !argType.name().equals(parameterTypes.get(i).name())) {
            return false;
        }
    }
    return true;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ClassJavaType(org.sonar.java.resolve.ClassJavaType)

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