use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class TypeAndReferenceSolverTest method typeOf.
private JavaType typeOf(String input) {
SemanticModel semanticModel = mock(SemanticModel.class);
when(semanticModel.getEnv(any(Tree.class))).thenReturn(env);
TypeAndReferenceSolver visitor = new TypeAndReferenceSolver(semanticModel, symbols, new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache), parametrizedTypeCache);
String p = "class Test { void wrapperMethod() { " + input + "; } }";
CompilationUnitTree tree = parse(p);
tree.accept(visitor);
TestedNodeExtractor testedNodeExtractor = new TestedNodeExtractor(false);
testedNodeExtractor.visitCompilationUnit(tree);
return visitor.getType(((ExpressionStatementTree) testedNodeExtractor.testedNode).expression());
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class TypeSubstitutionSolverTest method type_hierarchy_visit_should_be_limited.
@Test
public void type_hierarchy_visit_should_be_limited() {
ParametrizedTypeCache parametrizedTypeCache = new ParametrizedTypeCache();
BytecodeCompleter bytecodeCompleter = new BytecodeCompleter(new SquidClassLoader(new ArrayList<>()), parametrizedTypeCache);
Symbols symbols = new Symbols(bytecodeCompleter);
ActionParser<Tree> parser = JavaParser.createParser();
SemanticModel semanticModel = new SemanticModel(bytecodeCompleter);
Resolve resolve = new Resolve(symbols, bytecodeCompleter, parametrizedTypeCache);
TypeAndReferenceSolver typeAndReferenceSolver = new TypeAndReferenceSolver(semanticModel, symbols, resolve, parametrizedTypeCache);
CompilationUnitTree tree = (CompilationUnitTree) parser.parse(new File("src/test/files/sym/ComplexHierarchy.java"));
new FirstPass(semanticModel, symbols, resolve, parametrizedTypeCache, typeAndReferenceSolver).visitCompilationUnit(tree);
typeAndReferenceSolver.visitCompilationUnit(tree);
ClassTree classTree = (ClassTree) tree.types().get(tree.types().size() - 1);
JavaType site = (JavaType) classTree.symbol().type();
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) ((MethodTree) classTree.members().get(0)).block().body().get(0)).expression();
TypeSubstitutionSolver typeSubstitutionSolver = Mockito.spy(new TypeSubstitutionSolver(parametrizedTypeCache, symbols));
// call with empty formals should return.
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(new ArrayList<>(), site);
verify(typeSubstitutionSolver, times(0)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
JavaSymbol.MethodJavaSymbol methodJavaSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
typeSubstitutionSolver.applySiteSubstitutionToFormalParameters(((MethodJavaType) methodJavaSymbol.type).argTypes, site);
verify(typeSubstitutionSolver, times(11)).applySiteSubstitutionToFormalParameters(anyList(), any(JavaType.class), anySet());
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class UnusedLocalVariableCheck method leaveNode.
@Override
public void leaveNode(Tree tree) {
if (hasSemantic()) {
if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER)) {
BlockTree blockTree = (BlockTree) tree;
addVariables(blockTree.body());
} else if (tree.is(Tree.Kind.FOR_STATEMENT)) {
ForStatementTree forStatementTree = (ForStatementTree) tree;
addVariables(forStatementTree.initializer());
} else if (tree.is(Tree.Kind.FOR_EACH_STATEMENT)) {
ForEachStatement forEachStatement = (ForEachStatement) tree;
addVariable(forEachStatement.variable());
} else if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
leaveExpressionStatement((ExpressionStatementTree) tree);
} else {
checkVariableUsages();
variables.clear();
assignments.clear();
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class UnusedReturnedDataCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
} else {
BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
ExpressionTree leftOperand = expressionTree.leftOperand();
ExpressionTree rightOperand = expressionTree.rightOperand();
for (MethodMatcher matcher : CHECKED_METHODS) {
MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
if (leftMit != null && isTreeLiteralNull(rightOperand)) {
raiseIssue(ExpressionUtils.methodName(leftMit));
}
MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
if (rightMit != null && isTreeLiteralNull(leftOperand)) {
raiseIssue(ExpressionUtils.methodName(rightMit));
}
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionStatementTree in project sonar-java by SonarSource.
the class SyntaxTreeNameFinderTest method testFieldAccess.
@Test
public void testFieldAccess() {
MethodTree tree = buildSyntaxTree("public void test() {this.field = value;} Object field;");
BlockTree block = tree.block();
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
tree = buildSyntaxTree("public void test() {super.field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("field");
tree = buildSyntaxTree("public void test() {A.field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("A");
tree = buildSyntaxTree("public void test() {foo().field = value;}");
block = tree.block();
assignmentTree = (AssignmentExpressionTree) ((ExpressionStatementTree) block.body().get(0)).expression();
assertThat(SyntaxTreeNameFinder.getName(assignmentTree.variable())).isEqualTo("foo");
}
Aggregations