use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SpringComponentWithNonAutowiredMembersCheck method visitNode.
@Override
public void visitNode(Tree tree) {
ClassTree clazzTree = (ClassTree) tree;
SymbolMetadata clazzMeta = clazzTree.symbol().metadata();
if (isSpringComponent(clazzMeta) && !hasUniqueConstructor(clazzTree)) {
clazzTree.members().stream().filter(v -> v.is(Kind.VARIABLE)).map(m -> (VariableTree) m).filter(v -> !v.symbol().isStatic()).filter(v -> !isSpringInjectionAnnotated(v.symbol().metadata())).forEach(v -> reportIssue(v.simpleName(), "Annotate this member with \"@Autowired\", \"@Resource\", \"@Inject\", or \"@Value\", or remove it."));
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class SerialVersionUidCheck method checkModifiers.
private void checkModifiers(Symbol.VariableSymbol serialVersionUidSymbol) {
List<String> missingModifiers = Lists.newArrayList();
if (!serialVersionUidSymbol.isStatic()) {
missingModifiers.add("static");
}
if (!serialVersionUidSymbol.isFinal()) {
missingModifiers.add("final");
}
if (!serialVersionUidSymbol.type().is("long")) {
missingModifiers.add("long");
}
VariableTree variableTree = serialVersionUidSymbol.declaration();
if (variableTree != null && !missingModifiers.isEmpty()) {
reportIssue(variableTree.simpleName(), "Make this \"serialVersionUID\" field \"" + Joiner.on(' ').join(missingModifiers) + "\".");
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class VariableDeclarationScopeCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
BlockTree block = (BlockTree) tree;
List<StatementTree> body = block.body();
int bodySize = body.size();
for (int i = 0; i < bodySize; i++) {
StatementTree statement = body.get(i);
if (statement.is(Kind.VARIABLE)) {
VariableTree variableTree = (VariableTree) statement;
if (!variableTree.symbol().usages().isEmpty()) {
check(variableTree, body, bodySize, i + 1);
}
}
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class BadLocalConstantNameCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
VariableTree variableTree = (VariableTree) tree;
Symbol symbol = variableTree.symbol();
if (!symbol.owner().isMethodSymbol() || !symbol.isFinal()) {
return;
}
if (!hasLiteralInitializer(variableTree.initializer())) {
return;
}
IdentifierTree simpleName = variableTree.simpleName();
if (!pattern.matcher(simpleName.name()).matches()) {
reportIssue(simpleName, "Rename this constant name to match the regular expression '" + format + "'.");
}
}
use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildCall.
private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
if (isStringVarDecl(element)) {
VariableTree variableTree = (VariableTree) element;
String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
if (!idGenerator.isConst(lhs)) {
ExpressionTree initializer = variableTree.initializer();
String source = idGenerator.lookupIdFor(initializer);
blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
}
return;
}
if (element.is(METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
} else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
if (element.is(PLUS)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
} else if (element.is(PLUS_ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
idGenerator.varForExpression(element, ((Expression.Variable) var).id());
blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
}
} else if (element.is(ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
}
}
}
}
Aggregations