Search in sources :

Example 6 with Type

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

the class EnumSetCheck method checkIssue.

private void checkIssue(Type type, Tree reportTree, TypeTree typeTree) {
    if (type.isSubtypeOf("java.util.Set") && !type.isSubtypeOf("java.util.EnumSet") && type instanceof ParametrizedTypeJavaType) {
        ParametrizedTypeJavaType parametrizedType = (ParametrizedTypeJavaType) type;
        List<TypeVariableJavaType> typeParameters = parametrizedType.typeParameters();
        Type variableType = typeTree.symbolType();
        if (typeParameters.isEmpty() && variableType instanceof ParametrizedTypeJavaType) {
            // for java 7 diamond operator lookup declaration.
            parametrizedType = (ParametrizedTypeJavaType) variableType;
            typeParameters = parametrizedType.typeParameters();
        }
        if (!typeParameters.isEmpty()) {
            Type typeParameter = parametrizedType.substitution(typeParameters.get(0));
            if (typeParameter != null && typeParameter.symbol().isEnum()) {
                reportIssue(reportTree, "Convert this Set to an EnumSet.");
            }
        }
    }
}
Also used : TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType)

Example 7 with Type

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

the class DITCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    if (!isBodyOfEnumConstantTree(tree)) {
        Type superClass = tree.symbol().superClass();
        int dit = 0;
        while (superClass != null) {
            String fullyQualifiedName = superClass.fullyQualifiedName();
            if (getPatterns().stream().anyMatch(wp -> wp.match(fullyQualifiedName))) {
                break;
            }
            dit++;
            superClass = superClass.symbol().superClass();
        }
        if (dit > max) {
            Tree reportTree = tree.simpleName();
            if (tree.parent().is(Tree.Kind.NEW_CLASS)) {
                reportTree = ((NewClassTree) tree.parent()).newKeyword();
            }
            context.reportIssue(this, reportTree, "This class has " + dit + " parents which is greater than " + max + " authorized.", new ArrayList<>(), dit - max);
        }
    }
    super.visitClass(tree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Tree(org.sonar.plugins.java.api.tree.Tree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 8 with Type

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

the class EqualsNotOverriddenInSubclassCheck method parentClassImplementsEquals.

private static boolean parentClassImplementsEquals(ClassTree tree) {
    TypeTree superClass = tree.superClass();
    if (superClass != null) {
        Type superClassType = superClass.symbolType();
        while (superClassType.symbol().isTypeSymbol() && !superClassType.is("java.lang.Object")) {
            Symbol.TypeSymbol superClassSymbol = superClassType.symbol();
            if (hasNotFinalEqualsMethod(superClassSymbol)) {
                return true;
            }
            superClassType = superClassSymbol.superClass();
        }
    }
    return false;
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 9 with Type

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

the class CFG method handleExceptionalPaths.

private void handleExceptionalPaths(Symbol symbol) {
    TryStatement pop = enclosingTry.pop();
    TryStatement tryStatement;
    Block exceptionPredecessor = currentBlock;
    if (enclosedByCatch.peek()) {
        tryStatement = enclosingTry.peek();
    } else {
        tryStatement = pop;
    }
    enclosingTry.push(pop);
    if (pop != outerTry) {
        currentBlock = createBlock(currentBlock);
        currentBlock.exceptions.add(exitBlocks.peek());
        if (!enclosedByCatch.peek()) {
            exceptionPredecessor = currentBlock;
        }
    }
    if (symbol.isMethodSymbol()) {
        List<Type> thrownTypes = ((Symbol.MethodSymbol) symbol).thrownTypes();
        thrownTypes.forEach(thrownType -> {
            for (Type caughtType : tryStatement.catches.keySet()) {
                if (thrownType.isSubtypeOf(caughtType) || caughtType.isSubtypeOf(thrownType)) {
                    currentBlock.exceptions.add(tryStatement.catches.get(caughtType));
                }
            }
        });
    }
    exceptionPredecessor.exceptions.addAll(tryStatement.runtimeCatches);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type)

Example 10 with Type

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

the class TypeSubstitutionSolver method substitutionFromSuperType.

static TypeSubstitution substitutionFromSuperType(ParametrizedTypeJavaType target, ParametrizedTypeJavaType source) {
    TypeSubstitution result = new TypeSubstitution(target.typeSubstitution);
    if (target.rawType != source.rawType) {
        TypeJavaSymbol targetSymbol = target.symbol;
        Type superClass = targetSymbol.superClass();
        if (superClass != null && ((JavaType) superClass).isParameterized()) {
            TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superClass, source);
            result = result.combine(newSub);
        }
        for (Type superInterface : targetSymbol.interfaces()) {
            if (((JavaType) superInterface).isParameterized()) {
                TypeSubstitution newSub = substitutionFromSuperType((ParametrizedTypeJavaType) superInterface, source);
                result = result.combine(newSub);
            }
        }
    } else {
        result = target.typeSubstitution.combine(source.typeSubstitution);
    }
    return result;
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Type(org.sonar.plugins.java.api.semantic.Type)

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