Search in sources :

Example 91 with Type

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

the class Resolve method expandVarArgsToFitSize.

private static List<JavaType> expandVarArgsToFitSize(List<JavaType> m1ArgTypes, int size) {
    List<JavaType> newArgTypes = new ArrayList<>(m1ArgTypes);
    int m1ArgTypesSize = newArgTypes.size();
    int m1ArgTypesLast = m1ArgTypesSize - 1;
    Type lastElementType = ((Type.ArrayType) newArgTypes.get(m1ArgTypesLast)).elementType();
    // replace last element type from GivenType[] to GivenType
    newArgTypes.set(m1ArgTypesLast, (JavaType) lastElementType);
    // if m1ArgTypes smaller than size pad it with lastElementType
    for (int i = m1ArgTypesSize; i < size - 1; i++) {
        if (i < newArgTypes.size()) {
            newArgTypes.set(i, (JavaType) lastElementType);
        } else {
            newArgTypes.add((JavaType) lastElementType);
        }
    }
    return newArgTypes;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ArrayList(java.util.ArrayList)

Example 92 with Type

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

the class ExceptionalCheckBasedYield method applicableOnVarArgs.

private boolean applicableOnVarArgs(List<Type> invocationTypes) {
    int numberParametersYield = parametersConstraints.size();
    int numberArgumentsInCall = invocationTypes.size();
    if (numberParametersYield > numberArgumentsInCall) {
        // VarArgs method called without variadic parameter
        return true;
    }
    ConstraintsByDomain lastParamConstraint = parametersConstraints.get(numberParametersYield - 1);
    if (lastParamConstraint.isEmpty()) {
        // no constraint on the last parameter on yield side
        return true;
    }
    if (numberParametersYield != numberArgumentsInCall) {
        // there is a constraint on last parameter, but varArgs method called with multiple arguments in variadic part
        return false;
    }
    // compatible number of parameters, type must be compatible on arguments side
    Type lastArgumentType = invocationTypes.get(numberArgumentsInCall - 1);
    return !isMethodVarargs || (lastArgumentType.isArray() || lastArgumentType.is("<nulltype>"));
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Type(org.sonar.plugins.java.api.semantic.Type) Constraint(org.sonar.java.se.constraint.Constraint)

Example 93 with Type

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

the class ExceptionalCheckBasedYield method exceptionType.

@Nonnull
@Override
public Type exceptionType(SemanticModel semanticModel) {
    Type exceptionType = super.exceptionType(semanticModel);
    Preconditions.checkArgument(!exceptionType.isUnknown(), "Exception type is required");
    return exceptionType;
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Nonnull(javax.annotation.Nonnull)

Example 94 with Type

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

the class HappyPathYield method statesAfterInvocation.

@Override
public Stream<ProgramState> statesAfterInvocation(List<SymbolicValue> invocationArguments, List<Type> invocationTypes, ProgramState programState, Supplier<SymbolicValue> svSupplier) {
    Stream<ProgramState> results = parametersAfterInvocation(invocationArguments, invocationTypes, programState);
    // applied all constraints from parameters, stack return value
    SymbolicValue sv;
    if (resultIndex < 0 || resultIndex == invocationArguments.size()) {
        // if returnIndex is the size of invocationArguments : returning vararg parameter on a call with no elements specified
        sv = svSupplier.get();
    } else {
        // returned SV is the same as one of the arguments.
        sv = invocationArguments.get(resultIndex);
    }
    // sv can be null if method is void
    if (sv != null) {
        results = results.map(s -> s.stackValue(sv));
        if (resultConstraint != null) {
            results = results.map(s -> s.addConstraints(sv, resultConstraint));
        }
    }
    return results.distinct();
}
Also used : ProgramState(org.sonar.java.se.ProgramState) HashCodeBuilder(org.apache.commons.lang.builder.HashCodeBuilder) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Type(org.sonar.plugins.java.api.semantic.Type) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) EqualsBuilder(org.apache.commons.lang.builder.EqualsBuilder) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Constraint(org.sonar.java.se.constraint.Constraint) CheckForNull(javax.annotation.CheckForNull) Nullable(javax.annotation.Nullable) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 95 with Type

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

the class MethodBehavior method createYield.

public void createYield(ExplodedGraph.Node node, boolean storeNodeForReporting) {
    ExplodedGraph.Node nodeForYield = null;
    if (storeNodeForReporting) {
        nodeForYield = node;
    }
    MethodYield yield;
    boolean expectReturnValue = !(isConstructor() || isVoidMethod());
    SymbolicValue resultSV = node.programState.exitValue();
    if ((resultSV == null && expectReturnValue) || resultSV instanceof SymbolicValue.ExceptionalSymbolicValue) {
        ExceptionalYield exceptionalYield = new ExceptionalYield(nodeForYield, this);
        if (resultSV != null) {
            Type type = ((SymbolicValue.ExceptionalSymbolicValue) resultSV).exceptionType();
            String typeName = null;
            if (type != null) {
                typeName = type.fullyQualifiedName();
            }
            exceptionalYield.setExceptionType(typeName);
        }
        yield = exceptionalYield;
    } else {
        HappyPathYield happyPathYield = new HappyPathYield(nodeForYield, this);
        if (expectReturnValue) {
            ConstraintsByDomain cleanup = cleanup(node.programState.getConstraints(resultSV), org.objectweb.asm.Type.getReturnType(signature.substring(signature.indexOf('('))));
            if (cleanup.isEmpty()) {
                cleanup = null;
            }
            happyPathYield.setResult(parameters.indexOf(resultSV), cleanup);
        }
        yield = happyPathYield;
    }
    addParameterConstraints(node, yield);
    yields.add(yield);
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Type(org.sonar.plugins.java.api.semantic.Type) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

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