use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class MagicNumberCheck method visitVariable.
@Override
public void visitVariable(VariableTree tree) {
ExpressionTree initializer = tree.initializer();
boolean arrayNotInitialized = initializer != null && initializer.is(Kind.NEW_ARRAY) && ((NewArrayTree) initializer).initializers().isEmpty();
boolean isFinalOrNoSemantic = context.getSemanticModel() == null || tree.symbol().isFinal();
if (arrayNotInitialized || !isFinalOrNoSemantic) {
super.visitVariable(tree);
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class RawByteBitwiseOperationsCheck method visitBinaryExpression.
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
super.visitBinaryExpression(tree);
if (isShifting(tree)) {
shifts.add(tree);
return;
}
if (isSecuringByte(tree)) {
byteContainments.add(tree);
return;
}
if (isIntegerOrLongExpected(tree.symbolType())) {
ExpressionTree leftOperand = ExpressionUtils.skipParentheses(tree.leftOperand());
ExpressionTree rightOperand = ExpressionUtils.skipParentheses(tree.rightOperand());
checkShiftWithoutByteSecuring(leftOperand, rightOperand);
checkShiftWithoutByteSecuring(rightOperand, leftOperand);
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class RedundantTypeCastCheck method targetType.
@CheckForNull
private static Type targetType(TypeCastTree tree) {
Tree parent = skipParentheses(tree.parent());
Type target = null;
if (parent.is(Tree.Kind.RETURN_STATEMENT)) {
Tree method = parent;
while (!method.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
method = method.parent();
}
target = method.is(Tree.Kind.LAMBDA_EXPRESSION) ? null : ((MethodJavaType) ((MethodTree) method).symbol().type()).resultType();
} else if (parent.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) parent;
target = variableTree.symbol().type();
} else if (parent.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree mit = (MethodInvocationTree) parent;
if (mit.symbol().isMethodSymbol()) {
JavaSymbol.MethodJavaSymbol sym = (JavaSymbol.MethodJavaSymbol) mit.symbol();
int castArgIndex = mit.arguments().indexOf(tree);
target = sym.parameterTypes().get(castArgIndex);
}
} else if (parent.is(Tree.Kind.MEMBER_SELECT, Tree.Kind.CONDITIONAL_EXPRESSION)) {
target = tree.type().symbolType();
} else if (parent.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
target = ((ArrayAccessExpressionTree) parent).expression().symbolType();
} else if (parent instanceof ExpressionTree) {
target = ((ExpressionTree) parent).symbolType();
}
return target;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isMapPut.
@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
MethodInvocationTree mapPut = (MethodInvocationTree) expression;
Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
ExpressionTree keyTree = mapPut.arguments().get(0);
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, false, null);
}
}
}
return null;
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isArrayAssignment.
@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
ExpressionTree variable = assignment.variable();
if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
Symbol collection = symbolFromIdentifier(aaet.expression());
ExpressionTree keyTree = aaet.dimension().expression();
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
}
}
}
}
return null;
}
Aggregations