use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ForLoopTerminationConditionCheck method visitForStatement.
@Override
public void visitForStatement(ForStatementTree forStatement) {
ExpressionTree condition = forStatement.condition();
if (condition == null || !condition.is(Tree.Kind.NOT_EQUAL_TO)) {
return;
}
BinaryExpressionTree inequalityCondition = (BinaryExpressionTree) condition;
IntInequality loopVarAndTerminalValue = IntInequality.of(inequalityCondition);
if (loopVarAndTerminalValue != null) {
IdentifierTree loopIdentifier = loopVarAndTerminalValue.identifier;
int terminationValue = loopVarAndTerminalValue.literalValue;
Integer initialValue = initialValue(loopIdentifier, forStatement);
if (initialValue != null && initialValue != terminationValue) {
checkIncrement(forStatement, loopIdentifier, initialValue < terminationValue);
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class ExceptionsShouldBeImmutableCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
if (isException(classTree)) {
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.VARIABLE) && !isFinal((VariableTree) member)) {
IdentifierTree simpleName = ((VariableTree) member).simpleName();
reportIssue(simpleName, "Make this \"" + simpleName.name() + "\" field final.");
}
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class FileCreateTempFileCheck method checkAndAdvanceState.
@Nullable
private State checkAndAdvanceState(MethodInvocationTree mit, State requiredState, State nextState) {
ExpressionTree methodSelect = mit.methodSelect();
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
ExpressionTree expressionTree = ((MemberSelectExpressionTree) methodSelect).expression();
if (expressionTree.is(Tree.Kind.IDENTIFIER)) {
Symbol symbol = ((IdentifierTree) expressionTree).symbol();
Map<Symbol, State> symbolStateMap = symbolStack.peek();
if (symbolStateMap != null && symbolStateMap.containsKey(symbol) && requiredState.equals(symbolStateMap.get(symbol))) {
symbolStateMap.put(symbol, nextState);
return nextState;
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class IndexOfStartPositionCheck method checkIndexOfInvocation.
private void checkIndexOfInvocation(MethodInvocationTree mit, ExpressionTree other) {
if (INDEX_OF_METHOD.matches(mit)) {
String replaceMessage;
ExpressionTree firstPar = mit.arguments().get(0);
if (firstPar.is(Tree.Kind.STRING_LITERAL)) {
replaceMessage = ((LiteralTree) firstPar).value();
} else if (firstPar.is(Tree.Kind.IDENTIFIER)) {
replaceMessage = ((IdentifierTree) firstPar).name();
} else {
replaceMessage = "xxx";
}
Long otherValue = LiteralUtils.longLiteralValue(other);
if (otherValue != null && otherValue != -1 && otherValue != 0) {
reportIssue(ExpressionUtils.methodName(mit), "Use \".indexOf(" + replaceMessage + ",n) > -1\" instead.");
}
}
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CFG method buildBreakStatement.
private void buildBreakStatement(BreakStatementTree tree) {
IdentifierTree label = tree.label();
Block targetBlock = null;
if (label == null) {
if (breakTargets.isEmpty()) {
if (!ignoreBreakAndContinue) {
throw new IllegalStateException("'break' statement not in loop or switch statement");
}
} else {
targetBlock = breakTargets.getLast();
}
} else {
targetBlock = labelsBreakTarget.get(label.name());
}
currentBlock = createUnconditionalJump(tree, targetBlock);
if (currentBlock.exitBlock != null) {
currentBlock.exitBlock = null;
}
}
Aggregations