use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LeastSpecificTypeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
Symbol.MethodSymbol methodSymbol = methodTree.symbol();
if (!methodSymbol.isPublic() || !Boolean.FALSE.equals(methodTree.isOverriding()) || isOverloaded(methodSymbol)) {
return;
}
boolean springInjectionAnnotated = isSpringInjectionAnnotated(methodSymbol.metadata());
methodTree.parameters().stream().map(VariableTree::symbol).filter(p -> p.type().isClass() && !p.type().symbol().isEnum() && !p.type().is("java.lang.String")).filter(p -> !(springInjectionAnnotated && p.type().is("java.util.Collection"))).forEach(p -> handleParameter(p, springInjectionAnnotated));
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class PublicApiCheckerTest method getPublicApiVisitor.
private SubscriptionVisitor getPublicApiVisitor() {
return new SubscriptionVisitor() {
private final Deque<ClassTree> classTrees = Lists.newLinkedList();
private final Deque<MethodTree> methodTrees = Lists.newLinkedList();
@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.values());
}
@Override
public void visitNode(Tree tree) {
if (tree.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) tree;
String name = variableTree.simpleName().name();
Tree parent = classTrees.peek();
if (!methodTrees.isEmpty()) {
parent = methodTrees.peek();
}
assertThat(PublicApiChecker.isPublicApi(parent, tree)).as(name).isEqualTo(name.endsWith("Public"));
} else if (tree.is(PublicApiChecker.methodKinds())) {
MethodTree methodTree = (MethodTree) tree;
methodTrees.push(methodTree);
String name = methodTree.simpleName().name();
// getters and setters are included in the public API
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(name).isEqualTo(name.endsWith("Public") || name.contains("GetSet"));
} else if (tree.is(PublicApiChecker.classKinds())) {
IdentifierTree className = ((ClassTree) tree).simpleName();
if (className == null) {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(className.name()).isEqualTo(className != null && className.name().endsWith("Public"));
}
classTrees.push((ClassTree) tree);
} else {
assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
}
}
@Override
public void leaveNode(Tree tree) {
if (tree.is(PublicApiChecker.classKinds())) {
classTrees.pop();
} else if (tree.is(PublicApiChecker.methodKinds())) {
methodTrees.pop();
}
}
};
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.
private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
Symbol variableSymbol = identifier.symbol();
if (!variableSymbol.isVariableSymbol()) {
return false;
}
VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
if (variableDefinition != null) {
ExpressionTree initializer = variableDefinition.initializer();
if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
}
}
return false;
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class CollectionMethodsWithLinearComplexityCheck method findAssignedTypes.
private static Set<String> findAssignedTypes(Symbol symbol) {
Set<String> types = new HashSet<>();
Tree declaration = symbol.declaration();
if (declaration != null && declaration.is(Tree.Kind.VARIABLE)) {
ExpressionTree initializer = ((VariableTree) declaration).initializer();
if (initializer != null) {
types.add(initializer.symbolType().fullyQualifiedName());
}
}
symbol.usages().stream().flatMap(CollectionMethodsWithLinearComplexityCheck::usageInAssignment).map(assignment -> assignment.expression().symbolType().fullyQualifiedName()).forEach(types::add);
return types;
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ChangeMethodContractCheck method checkContractChange.
private void checkContractChange(MethodTreeImpl methodTree, Symbol.MethodSymbol overridee) {
if (methodTree.isEqualsMethod() && methodTree.parameters().get(0).symbol().metadata().isAnnotatedWith(JAVAX_ANNOTATION_NONNULL)) {
reportIssue(methodTree.parameters().get(0), "Equals method should accept null parameters and return false.");
return;
}
for (int i = 0; i < methodTree.parameters().size(); i++) {
VariableTree parameter = methodTree.parameters().get(i);
Symbol overrideeParamSymbol = ((JavaSymbol.MethodJavaSymbol) overridee).getParameters().scopeSymbols().get(i);
checkParameter(parameter, overrideeParamSymbol);
}
if (nonNullVsNull(overridee, methodTree.symbol())) {
for (AnnotationTree annotationTree : methodTree.modifiers().annotations()) {
if (annotationTree.symbolType().is(JAVAX_ANNOTATION_NULLABLE) || annotationTree.symbolType().is(JAVAX_ANNOTATION_CHECK_FOR_NULL)) {
reportIssue(annotationTree, "Remove this \"" + annotationTree.symbolType().name() + "\" annotation to honor the overridden method's contract.");
}
}
}
}
Aggregations