use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MethodInvocationTreeImplTest method symbol_should_be_set.
@Test
public void symbol_should_be_set() {
CompilationUnitTree cut = createTree("class A { void foo(){} void bar(){foo();} }");
ClassTree classTree = (ClassTree) cut.types().get(0);
Symbol.MethodSymbol declaration = ((MethodTree) classTree.members().get(0)).symbol();
StatementTree statementTree = ((MethodTree) classTree.members().get(1)).block().body().get(0);
MethodInvocationTree mit = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).expression();
assertThat(mit.symbol()).isSameAs(declaration);
assertThat(mit.arguments()).isNotNull();
assertThat(mit.arguments().openParenToken()).isNotNull();
assertThat(mit.arguments().closeParenToken()).isNotNull();
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class ArrayForVarArgCheck method visitNode.
@Override
public void visitNode(Tree tree) {
Symbol sym;
Arguments args;
Tree methodName;
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree nct = (NewClassTree) tree;
sym = nct.constructorSymbol();
args = nct.arguments();
methodName = nct.identifier();
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
sym = mit.symbol();
args = mit.arguments();
methodName = mit.methodSelect();
}
if (sym.isMethodSymbol() && !args.isEmpty()) {
ExpressionTree lastArg = args.get(args.size() - 1);
JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
MethodJavaType methodType = getMethodType(methodSymbol, methodName);
checkInvokedMethod(methodSymbol, methodType, lastArg);
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class PublicStaticMutableMembersCheck method preCheckVariable.
private void preCheckVariable(Tree owner, VariableTree variableTree) {
Symbol symbol = variableTree.symbol();
if (symbol != null && isPublicStatic(symbol) && isForbiddenType(symbol.type())) {
if (isMutable(variableTree.initializer(), symbol)) {
String message = "Make this member \"protected\".";
if (owner.is(Tree.Kind.INTERFACE)) {
message = MessageFormat.format("Move \"{0}\" to a class and lower its visibility", variableTree.simpleName().name());
}
reportIssue(variableTree.simpleName(), message);
} else {
IMMUTABLE_CANDIDATES.add(symbol);
CLASS_IMMUTABLE_CANDIDATES.put(owner, symbol);
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class PublicStaticMutableMembersCheck method checkAssignment.
private void checkAssignment(AssignmentExpressionTree node) {
ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
if (variable.is(Tree.Kind.MEMBER_SELECT)) {
variable = ((MemberSelectExpressionTree) variable).identifier();
}
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) variable;
Symbol symbol = identifierTree.symbol();
if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
IMMUTABLE_CANDIDATES.remove(symbol);
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MutableMembersUsageCheck method visitAssignmentExpression.
@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
super.visitAssignmentExpression(tree);
if (!isMutableType(tree.expression())) {
return;
}
ExpressionTree variable = tree.variable();
Symbol leftSymbol = null;
if (variable.is(Tree.Kind.IDENTIFIER)) {
IdentifierTree identifierTree = (IdentifierTree) variable;
leftSymbol = identifierTree.symbol();
} else if (variable.is(Tree.Kind.MEMBER_SELECT)) {
MemberSelectExpressionTree mit = (MemberSelectExpressionTree) variable;
leftSymbol = mit.identifier().symbol();
}
if (leftSymbol != null && leftSymbol.isPrivate()) {
checkStore(tree.expression());
}
}
Aggregations