use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class StringConcatenationInLoopCheck method isNotLoopLocalVar.
private boolean isNotLoopLocalVar(AssignmentExpressionTree tree) {
IdentifierTree idTree = getIdentifierTree(tree.variable());
Tree envTree = semanticModel.getTree(semanticModel.getEnv(idTree.symbol()));
Tree loopTree = loopLevel.peek();
return envTree == null || !(envTree.equals(loopTree) || envTree.equals(loopStatement(loopTree)));
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class StringToStringCheck method onMethodInvocationFound.
@Override
protected void onMethodInvocationFound(MethodInvocationTree tree) {
ExpressionTree expressionTree = extractBaseExpression(((MemberSelectExpressionTree) tree.methodSelect()).expression());
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
reportIssue(expressionTree, String.format("\"%s\" is already a string, there's no need to call \"toString()\" on it.", ((IdentifierTree) expressionTree).identifierToken().text()));
} else if (expressionTree.is(Tree.Kind.STRING_LITERAL)) {
reportIssue(expressionTree, "there's no need to call \"toString()\" on a string literal.");
} else if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
IdentifierTree methodName = ExpressionUtils.methodName((MethodInvocationTree) expressionTree);
reportIssue(methodName, "\"" + methodName + "\" returns a string, there's no need to call \"toString()\".");
} else if (expressionTree.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTree arrayAccess = (ArrayAccessExpressionTree) expressionTree;
IdentifierTree name = extractName(arrayAccess.expression());
if (name == null) {
reportIssue(arrayAccess.expression(), "There's no need to call \"toString()\" on an array of String.");
} else {
reportIssue(name, String.format("\"%s\" is an array of strings, there's no need to call \"toString()\".", name.identifierToken().text()));
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class StaticFieldInitializationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
switch(tree.kind()) {
case CLASS:
classWithSynchronizedMethod.push(hasSynchronizedMethod((ClassTree) tree));
break;
case STATIC_INITIALIZER:
withinStaticInitializer.push(true);
break;
case METHOD:
methodUsesLocks.push(false);
break;
case METHOD_INVOCATION:
if (locks.anyMatch((MethodInvocationTree) tree) && methodUsesLocks.size() != 1) {
methodUsesLocks.pop();
methodUsesLocks.push(true);
}
break;
case ASSIGNMENT:
AssignmentExpressionTree aet = (AssignmentExpressionTree) tree;
if (hasSemantic() && aet.variable().is(Tree.Kind.IDENTIFIER) && !isInSyncBlock() && !isInStaticInitializer() && !isUsingLock() && isInClassWithSynchronizedMethod()) {
IdentifierTree variable = (IdentifierTree) aet.variable();
if (isStaticNotVolatileObject(variable)) {
reportIssue(variable, "Synchronize this lazy initialization of '" + variable.name() + "'");
}
}
break;
default:
}
super.visitNode(tree);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.
private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
ExpressionTree arg = null;
ExpressionTree expression = null;
if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
Symbol symbol = mit.symbol();
ExpressionTree methodSelect = mit.methodSelect();
if (mit.arguments().size() == 1) {
arg = mit.arguments().get(0);
if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
expression = ((MemberSelectExpressionTree) methodSelect).expression();
}
}
} else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
arg = equalTo.leftOperand();
expression = equalTo.rightOperand();
}
if (arg != null && expression != null) {
if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
} else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
}
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class SwitchWithLabelsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
CaseGroupTree cgt = (CaseGroupTree) tree;
for (StatementTree statementTree : cgt.body()) {
if (statementTree.is(LABELED_STATEMENT)) {
IdentifierTree label = ((LabeledStatementTree) statementTree).label();
reportIssue(label, "Remove this misleading \"" + label.name() + "\" label.");
}
}
}
Aggregations