use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ValueBasedUtilsTest method testIsValueBased.
@Test
public void testIsValueBased() throws Exception {
File file = new File("src/test/files/checks/helpers/ValueBasedUtilsTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, new SquidClassLoader(Collections.emptyList()));
List<Tree> members = ((ClassTree) tree.types().get(0)).members();
members.stream().forEach(member -> checkMember((VariableTree) member));
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method startingStates.
private Iterable<ProgramState> startingStates(MethodTree tree, ProgramState currentState) {
Stream<ProgramState> stateStream = Stream.of(currentState);
boolean isEqualsMethod = EQUALS.matches(tree);
boolean nonNullParameters = isGloballyAnnotatedParameterNonNull(methodTree.symbol());
boolean nullableParameters = isGloballyAnnotatedParameterNullable(methodTree.symbol());
boolean hasMethodBehavior = methodBehavior != null;
for (final VariableTree variableTree : tree.parameters()) {
// create
final SymbolicValue sv = constraintManager.createSymbolicValue(variableTree);
Symbol variableSymbol = variableTree.symbol();
if (hasMethodBehavior) {
methodBehavior.addParameter(sv);
}
stateStream = stateStream.map(ps -> ps.put(variableSymbol, sv));
if (isEqualsMethod || parameterCanBeNull(variableSymbol, nullableParameters)) {
stateStream = stateStream.flatMap((ProgramState ps) -> Stream.concat(sv.setConstraint(ps, ObjectConstraint.NULL).stream(), sv.setConstraint(ps, ObjectConstraint.NOT_NULL).stream()));
} else if (nonNullParameters || isAnnotatedNonNull(variableSymbol)) {
stateStream = stateStream.flatMap(ps -> sv.setConstraint(ps, ObjectConstraint.NOT_NULL).stream());
}
}
return stateStream.collect(Collectors.toList());
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ExplodedGraphWalker method learnIdentifierConstraints.
private void learnIdentifierConstraints(IdentifierTree tree, SymbolicValue sv) {
if (THIS_SUPER.contains(tree.name())) {
programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
return;
}
Tree declaration = tree.symbol().declaration();
if (!isFinalField(tree.symbol()) || declaration == null) {
return;
}
VariableTree variableTree = (VariableTree) declaration;
ExpressionTree initializer = variableTree.initializer();
if (initializer == null) {
return;
}
// only check final field with an initializer
initializer = ExpressionUtils.skipParentheses(initializer);
if (initializer.is(Tree.Kind.NULL_LITERAL)) {
programState = programState.addConstraint(sv, ObjectConstraint.NULL);
} else if (initializer.is(Tree.Kind.NEW_CLASS, Tree.Kind.NEW_ARRAY, Tree.Kind.STRING_LITERAL) || isNonNullMethodInvocation(initializer) || variableTree.symbol().type().isPrimitive() || initializer.symbolType().isPrimitive()) {
programState = programState.addConstraint(sv, ObjectConstraint.NOT_NULL);
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class FlowComputation method flowsForArgumentsChangingName.
public static Flow flowsForArgumentsChangingName(List<Integer> argumentIndices, MethodInvocationTree mit) {
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
MethodTree declaration = methodSymbol.declaration();
if (declaration == null) {
return Flow.empty();
}
Flow.Builder flowBuilder = Flow.builder();
List<VariableTree> methodParameters = declaration.parameters();
for (Integer argumentIndex : argumentIndices) {
// do not consider varargs part
if (methodSymbol.isVarArgs() && argumentIndex >= methodParameters.size() - 1) {
break;
}
IdentifierTree argumentName = getArgumentIdentifier(mit, argumentIndex);
if (argumentName != null) {
IdentifierTree parameterIdentifier = methodParameters.get(argumentIndex).simpleName();
String identifierName = parameterIdentifier.name();
if (!argumentName.name().equals(identifierName)) {
flowBuilder.add(new JavaFileScannerContext.Location(String.format(IMPLIES_SAME_VALUE, identifierName, argumentName.name()), parameterIdentifier));
}
}
}
return flowBuilder.build().reverse();
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method visitLambdaExpression.
@Override
public void visitLambdaExpression(LambdaExpressionTree tree) {
LambdaExpressionTreeImpl lambdaExpressionTree = (LambdaExpressionTreeImpl) tree;
if (lambdaExpressionTree.isTypeSet()) {
// type should be tied to a SAM interface
JavaType lambdaType = (JavaType) lambdaExpressionTree.symbolType();
List<JavaType> samMethodArgs = resolve.findSamMethodArgs(lambdaType);
for (int i = 0; i < samMethodArgs.size(); i++) {
VariableTree param = lambdaExpressionTree.parameters().get(i);
if (param.type().is(Tree.Kind.INFERED_TYPE)) {
JavaType inferedType = samMethodArgs.get(i);
if (inferedType.isTagged(JavaType.WILDCARD)) {
// JLS8 18.5.3
inferedType = ((WildCardType) inferedType).bound;
}
((AbstractTypedTree) param.type()).setInferedType(inferedType);
((JavaSymbol.VariableJavaSymbol) param.symbol()).type = inferedType;
}
}
super.visitLambdaExpression(tree);
if (lambdaType.isUnknown() || lambdaType.isTagged(JavaType.DEFERRED)) {
return;
}
refineLambdaType(lambdaExpressionTree, lambdaType);
} else {
registerType(tree, symbols.deferedType(lambdaExpressionTree));
}
}
Aggregations