use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class NonNullSetToNullCheck method checkEndOfExecutionPath.
@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
if (methodTree.is(Tree.Kind.CONSTRUCTOR) && !isDefaultConstructorForJpa(methodTree) && !callsThisConstructor(methodTree) && !exitingWithException(context)) {
ClassTree classTree = (ClassTree) methodTree.parent();
classTree.members().stream().filter(m -> m.is(Tree.Kind.VARIABLE)).map(m -> (VariableTree) m).filter(v -> v.initializer() == null).forEach(v -> checkVariable(context, methodTree, v.symbol()));
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ArrayDesignatorOnVariableCheck method visitNode.
@Override
public void visitNode(Tree tree) {
VariableTree variableTree = (VariableTree) tree;
TypeTree type = variableTree.type();
SyntaxToken identifierToken = variableTree.simpleName().identifierToken();
while (type.is(Tree.Kind.ARRAY_TYPE)) {
ArrayTypeTree arrayTypeTree = (ArrayTypeTree) type;
SyntaxToken arrayDesignatorToken = arrayTypeTree.ellipsisToken();
if (arrayDesignatorToken == null) {
arrayDesignatorToken = arrayTypeTree.openBracketToken();
}
if (isInvalidPosition(arrayDesignatorToken, identifierToken)) {
reportIssue(arrayDesignatorToken, "Move the array designator from the variable to the type.");
break;
}
type = arrayTypeTree.type();
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class MembersDifferOnlyByCapitalizationCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
List<Symbol> allMembers = retrieveMembers(classTree.symbol());
Multimap<String, Symbol> membersByName = sortByName(allMembers);
for (Tree member : classTree.members()) {
if (member.is(Tree.Kind.METHOD)) {
MethodTree methodTree = (MethodTree) member;
checkForIssue(methodTree.symbol(), methodTree.simpleName(), membersByName);
} else if (member.is(Tree.Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) member;
checkForIssue(variableTree.symbol(), variableTree.simpleName(), membersByName);
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitMethod.
@Override
public void visitMethod(MethodTree tree) {
BlockTree block = tree.block();
if (block == null) {
return;
}
CFG cfg = CFG.build(tree);
LiveVariables analyze = LiveVariables.analyze(cfg);
Set<Symbol> live = analyze.getIn(cfg.entry());
for (VariableTree parameterTree : tree.parameters()) {
if (!live.contains(parameterTree.symbol())) {
variables.add(parameterTree.symbol());
}
}
super.visitMethod(tree);
for (VariableTree parameterTree : tree.parameters()) {
if (!live.contains(parameterTree.symbol())) {
variables.remove(parameterTree.symbol());
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class LambdaTypeParameterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
List<VariableTree> parameters = lambdaExpressionTree.parameters();
if (parameters.size() <= 2 && !lambdaExpressionTree.body().is(Tree.Kind.BLOCK)) {
// ignore lambdas with one or two params and a non-block body
return;
}
String missingTypeParameters = parameters.stream().filter(variable -> variable.type().is(Tree.Kind.INFERED_TYPE)).map(VariableTree::simpleName).map(IdentifierTree::name).map(parameterName -> "'" + parameterName + "'").collect(Collectors.joining(", "));
if (!missingTypeParameters.isEmpty()) {
reportIssue(parameters.get(0), Iterables.getLast(parameters), String.format("Specify a type for: %s", missingTypeParameters));
}
}
Aggregations