use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class AnnotationDefaultArgumentCheck method visitNode.
@Override
public void visitNode(Tree tree) {
AnnotationTree annotationTree = (AnnotationTree) tree;
TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
if (annotationSymbol.isUnknown()) {
return;
}
Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
for (ExpressionTree argument : annotationTree.arguments()) {
Tree valueSet = argument;
// Single element annotation : JLS8 9.7.3 : one param must be named value.
String paramName = "value";
if (argument.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
paramName = nameTree.name();
valueSet = assignmentTree.expression();
}
if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class DeadStoreCheck method handleVariable.
private void handleVariable(Set<Symbol> out, VariableTree localVar) {
Symbol symbol = localVar.symbol();
ExpressionTree initializer = localVar.initializer();
if (initializer != null && !isUsualDefaultValue(initializer) && !out.contains(symbol)) {
createIssue(localVar.equalToken(), initializer, symbol);
}
out.remove(symbol);
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class DeadStoreCheck method handleAssignment.
private void handleAssignment(Set<Symbol> out, Set<Tree> assignmentLHS, AssignmentExpressionTree element) {
ExpressionTree lhs = ExpressionUtils.skipParentheses(element.variable());
if (lhs.is(Tree.Kind.IDENTIFIER)) {
Symbol symbol = ((IdentifierTree) lhs).symbol();
if (isLocalVariable(symbol) && !out.contains(symbol) && (element.is(Tree.Kind.ASSIGNMENT) || isParentExpressionStatement(element))) {
createIssue(element.operatorToken(), element.expression(), symbol);
}
assignmentLHS.add(lhs);
if (element.is(Tree.Kind.ASSIGNMENT)) {
out.remove(symbol);
} else {
out.add(symbol);
}
}
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class DeprecatedHashAlgorithmCheck method algorithm.
private static Optional<InsecureAlgorithm> algorithm(ExpressionTree invocationArgument) {
ExpressionTree expectedAlgorithm = invocationArgument;
ExpressionTree defaultPropertyValue = JavaPropertiesHelper.retrievedPropertyDefaultValue(invocationArgument);
if (defaultPropertyValue != null) {
expectedAlgorithm = defaultPropertyValue;
}
if (expectedAlgorithm.is(Tree.Kind.STRING_LITERAL)) {
String algorithmName = LiteralUtils.trimQuotes(((LiteralTree) expectedAlgorithm).value());
return Arrays.stream(InsecureAlgorithm.values()).filter(alg -> alg.match(algorithmName)).findFirst();
}
return Optional.empty();
}
use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.
the class CollectionSizeAndArrayLengthCheck method checkArrayLength.
private void checkArrayLength(MemberSelectExpressionTree testedValue, BinaryExpressionTree bet, boolean leftIsZero) {
if (!"length".equals(testedValue.identifier().name())) {
return;
}
ExpressionTree expression = testedValue.expression();
if (!expression.symbolType().isArray()) {
return;
}
reportIssue(bet, leftIsZero, ARRAY_ISSUE_MSG, arrayName(expression));
}
Aggregations