Search in sources :

Example 31 with Type

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

the class BadConstantNameCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree classTree = (ClassTree) tree;
    for (Tree member : classTree.members()) {
        if (member.is(Tree.Kind.VARIABLE) && hasSemantic()) {
            VariableTree variableTree = (VariableTree) member;
            Type symbolType = variableTree.type().symbolType();
            if (isConstantType(symbolType) && (classTree.is(Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE) || isStaticFinal(variableTree))) {
                checkName(variableTree);
            }
        } else if (member.is(Tree.Kind.ENUM_CONSTANT)) {
            checkName((VariableTree) member);
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ModifierKeywordTree(org.sonar.plugins.java.api.tree.ModifierKeywordTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 32 with Type

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

the class ExplodedGraphWalker method executeTypeCast.

private void executeTypeCast(TypeCastTree typeCast) {
    Type type = typeCast.type().symbolType();
    if (type.isPrimitive()) {
        JavaType expType = (JavaType) typeCast.expression().symbolType();
        // create SV to consume factory if any
        SymbolicValue castSV = constraintManager.createSymbolicValue(typeCast);
        // if exp type is a primitive and subtype of cast type, we can reuse the same symbolic value
        if (!expType.isPrimitive() || !new Types().isSubtype(expType, (JavaType) type)) {
            ProgramState.Pop unstack = programState.unstackValue(1);
            programState = unstack.state;
            programState = programState.stackValue(castSV);
        }
    }
}
Also used : Types(org.sonar.java.resolve.Types) JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 33 with Type

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

the class ExplodedGraphWalker method addExceptionalYield.

public void addExceptionalYield(SymbolicValue target, ProgramState exceptionalState, String exceptionFullyQualifiedName, SECheck check) {
    // in order to create such Exceptional Yield, a parameter of the method has to be the cause of the exception
    if (methodBehavior != null && methodBehavior.parameters().contains(target)) {
        Type exceptionType = semanticModel.getClassType(exceptionFullyQualifiedName);
        ProgramState newExceptionalState = exceptionalState.clearStack().stackValue(constraintManager.createExceptionalSymbolicValue(exceptionType));
        ExplodedGraph.Node exitNode = explodedGraph.node(node.programPoint, newExceptionalState);
        methodBehavior.createExceptionalCheckBasedYield(target, exitNode, exceptionFullyQualifiedName, check);
        exitNode.addParent(node, null);
    }
}
Also used : JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type)

Example 34 with Type

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

the class ExplodedGraphWalker method handleCatchVariable.

private SymbolicValue handleCatchVariable(Type caughtType) {
    SymbolicValue peekValue = programState.peekValue();
    SymbolicValue.ExceptionalSymbolicValue sv = null;
    Type exceptionType = null;
    // FIXME SONARJAVA-2069 every path conducting to a catch block should have an exceptional symbolic value on top of the stack
    if (peekValue instanceof SymbolicValue.ExceptionalSymbolicValue) {
        sv = (SymbolicValue.ExceptionalSymbolicValue) peekValue;
        exceptionType = sv.exceptionType();
    }
    if (exceptionType == null || exceptionType.isUnknown()) {
        // unknown exception, create an exception of the adequate type
        sv = constraintManager.createExceptionalSymbolicValue(caughtType);
    }
    // use a dedicated SV encapsulating the caught exception
    return constraintManager.createCaughtExceptionSymbolicValue(sv);
}
Also used : JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 35 with Type

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

the class LeastUpperBound method supertypes.

@VisibleForTesting
Set<Type> supertypes(JavaType type) {
    List<Type> result = new ArrayList<>();
    result.add(type);
    Symbol.TypeSymbol symbol = type.symbol();
    TypeSubstitution substitution = getTypeSubstitution(type);
    if (substitution.size() == 0 && !((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.isEmpty()) {
        // raw type : let's create a substitution based on erasures
        TypeSubstitution ts = new TypeSubstitution();
        ((JavaSymbol.TypeJavaSymbol) symbol).typeVariableTypes.forEach(t -> ts.add(t, t.erasure()));
        substitution = ts;
    }
    result.addAll(interfacesWithSubstitution(symbol, substitution));
    Type superClass = symbol.superClass();
    while (superClass != null) {
        JavaType substitutedSuperClass = applySubstitution(superClass, substitution);
        result.add(substitutedSuperClass);
        substitution = getTypeSubstitution(substitutedSuperClass);
        JavaSymbol.TypeJavaSymbol superClassSymbol = substitutedSuperClass.getSymbol();
        result.addAll(interfacesWithSubstitution(superClassSymbol, substitution));
        superClass = superClassSymbol.superClass();
    }
    return new LinkedHashSet<>(result);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

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