use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class ExpressionUtilsTest method method_name.
@Test
public void method_name() throws Exception {
File file = new File("src/test/files/model/ExpressionUtilsMethodNameTest.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
MethodTree methodTree = (MethodTree) ((ClassTree) tree.types().get(0)).members().get(0);
MethodInvocationTree firstMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(0)).expression();
MethodInvocationTree secondMIT = (MethodInvocationTree) ((ExpressionStatementTree) methodTree.block().body().get(1)).expression();
assertThat(ExpressionUtils.methodName(firstMIT).name()).isEqualTo("foo");
assertThat(ExpressionUtils.methodName(secondMIT).name()).isEqualTo("foo");
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class SyntaxTreeDebug method memberSelectString.
private static String memberSelectString(MemberSelectExpressionTree expression) {
StringBuilder buffer = new StringBuilder();
ExpressionTree target = expression.expression();
switch(target.kind()) {
case IDENTIFIER:
buffer.append(identifierString((IdentifierTree) target));
break;
case METHOD_INVOCATION:
buffer.append(methodInvocationString((MethodInvocationTree) target));
break;
case VARIABLE:
buffer.append(variableString((VariableTree) target));
break;
case INT_LITERAL:
buffer.append(literalString((LiteralTree) target));
break;
default:
break;
}
buffer.append('.');
buffer.append(identifierString(expression.identifier()));
return buffer.toString();
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree 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.MethodInvocationTree in project sonar-java by SonarSource.
the class ImmediateReverseBoxingCheck method checkForBoxing.
private void checkForBoxing(ExpressionTree expression) {
if (expression.is(Tree.Kind.NEW_CLASS)) {
NewClassTree newClassTree = (NewClassTree) expression;
Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
if (classSymbol != null) {
ExpressionTree boxingArg = newClassTree.arguments().get(0);
if (boxingArg.symbolType().isPrimitive()) {
addBoxingIssue(newClassTree, classSymbol, boxingArg);
}
}
} else if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
if (isValueOfInvocation(methodInvocationTree)) {
ExpressionTree boxingArg = methodInvocationTree.arguments().get(0);
addBoxingIssue(expression, methodInvocationTree.symbol().owner(), boxingArg);
}
}
}
use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.
the class IdenticalOperandOnBinaryExpressionCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
checkEqualsMethods(mit);
return;
}
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) tree;
ExpressionTree rightOperand = binaryExpressionTree.rightOperand();
ExpressionTree equivalentOperand = equivalentOperand(binaryExpressionTree, rightOperand);
if (equivalentOperand != null) {
reportIssue(rightOperand, "Correct one of the identical sub-expressions on both sides of operator \"" + binaryExpressionTree.operatorToken().text() + "\"", ImmutableList.of(new JavaFileScannerContext.Location("", equivalentOperand)), null);
}
}
Aggregations