use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method ignore_assignation_after_starting_point.
@Test
public void ignore_assignation_after_starting_point() throws Exception {
String code = newCode("int foo() {", " int b = 0;", " doSomething(b);", " b = 1;", "}");
List<StatementTree> statements = methodBody(code);
Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ReassignmentFinderTest method ignore_assignation_after_starting_point_same_line.
@Test
public void ignore_assignation_after_starting_point_same_line() throws Exception {
String code = newCode("int foo() {", " int b = 0;", " doSomething(b); b = 1;", "}");
List<StatementTree> statements = methodBody(code);
Tree expectedVariableDeclaration = initializerFromVariableDeclarationStatement(statements.get(0));
MethodInvocationTree startingPoint = (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
assertThatLastReassignmentsOfVariableIsEqualTo(searchedVariable, startingPoint, expectedVariableDeclaration);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree 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.IdentifierTree 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.IdentifierTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolver method resolveMethodSymbol.
@CheckForNull
private Resolve.Resolution resolveMethodSymbol(Tree methodSelect, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes) {
Resolve.Resolution resolution;
IdentifierTree identifier;
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
resolveAs(mset.expression(), JavaSymbol.TYP | JavaSymbol.VAR);
JavaType type = getType(mset.expression());
if (type.isTagged(JavaType.DEFERRED)) {
return null;
}
identifier = mset.identifier();
resolution = resolve.findMethod(methodEnv, type, identifier.name(), argTypes, typeParamTypes);
} else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
identifier = (IdentifierTree) methodSelect;
resolution = resolve.findMethod(methodEnv, identifier.name(), argTypes, typeParamTypes);
} else {
throw new IllegalStateException("Method select in method invocation is not of the expected type " + methodSelect);
}
registerType(identifier, resolution.type());
associateReference(identifier, resolution.symbol());
return resolution;
}
Aggregations