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;
}
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>"));
}
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;
}
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();
}
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);
}
Aggregations