use of org.sonar.plugins.java.api.tree.IdentifierTree 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.IdentifierTree 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.IdentifierTree in project sonar-java by SonarSource.
the class DataStoredInSessionCheck method usedBetween.
private boolean usedBetween(Symbol variable, Tree start, Tree end) {
SyntaxToken startToken = start.lastToken();
SyntaxToken endToken = end.firstToken();
for (IdentifierTree identifier : variable.usages()) {
SyntaxToken identifierToken = identifier.identifierToken();
if (isAfterFirstToken(identifierToken, startToken) && isBeforeLastToken(identifierToken, endToken) && !identifiersUsedToSetAttribute.contains(identifier)) {
return true;
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CollectionMethodsWithLinearComplexityCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodInvocationTree mit = (MethodInvocationTree) tree;
matcherActualTypeMap.forEach((methodMatcher, actualTypes) -> {
if (methodMatcher.matches(mit) && invocationInMethod(mit)) {
Symbol target = invocationTarget(mit);
if (target != null && isField(target) && matchesActualType(target, actualTypes)) {
IdentifierTree methodName = ExpressionUtils.methodName(mit);
reportIssue(methodName, "This call to \"" + methodName.name() + "()\" may be a performance hot spot if the collection is large.");
}
}
});
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class SymbolicValueFactoryTest method testFactory.
@Test
public void testFactory() {
final IdentifierTree tree = new IdentifierTreeImpl(new InternalSyntaxToken(1, 1, "id", Collections.<SyntaxTrivia>emptyList(), 0, 0, false));
final ConstraintManager manager = new ConstraintManager();
SymbolicValue symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created without factory").isSameAs(SymbolicValue.class);
manager.setValueFactory(new TestSymbolicValueFactory());
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created with factory").isSameAs(TestSymbolicValue.class);
assertThat(symbolicValue.references(symbolicValue)).isFalse();
manager.setValueFactory(new TestSymbolicValueFactory());
try {
manager.setValueFactory(new TestSymbolicValueFactory());
fail("Able to add a second factory to the contraints manager");
} catch (IllegalStateException e) {
assertThat(e.getMessage()).as("Exception message").isEqualTo("The symbolic value factory has already been defined by another checker!");
}
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created with first factory").isSameAs(TestSymbolicValue.class);
symbolicValue = manager.createSymbolicValue(tree);
assertThat(symbolicValue.getClass()).as("Created after factory usage").isSameAs(SymbolicValue.class);
}
Aggregations