use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class MembersDifferOnlyByCapitalizationCheck method invalidMethodAndVariable.
private static boolean invalidMethodAndVariable(Symbol currentMember, Symbol knownMember) {
if (!sameVisibilityNotPrivate(currentMember, knownMember)) {
return false;
}
Symbol methodSymbol = currentMember.isMethodSymbol() ? currentMember : knownMember;
Symbol variableSymbol = methodSymbol == currentMember ? knownMember : currentMember;
return !methodReturningVariableWithSameName(methodSymbol, variableSymbol) && !isBuilderPattern(methodSymbol, variableSymbol);
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class RedundantCloseCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
TryStatementTree tryStatementTree = (TryStatementTree) tree;
Set<Symbol> resourceSymbols = tryStatementTree.resourceList().stream().map(RedundantCloseCheck::resourceSymbol).filter(s -> !s.isUnknown()).collect(Collectors.toSet());
if (resourceSymbols.isEmpty()) {
return;
}
tryStatementTree.block().accept(new CloseVisitor(resourceSymbols));
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isMapPut.
@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
MethodInvocationTree mapPut = (MethodInvocationTree) expression;
Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
ExpressionTree keyTree = mapPut.arguments().get(0);
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, false, null);
}
}
}
return null;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class OverwrittenKeyCheck method isArrayAssignment.
@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
if (expression.is(Tree.Kind.ASSIGNMENT)) {
AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
ExpressionTree variable = assignment.variable();
if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
Symbol collection = symbolFromIdentifier(aaet.expression());
ExpressionTree keyTree = aaet.dimension().expression();
Object key = extractKey(keyTree);
if (collection != null && key != null) {
return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
}
}
}
}
return null;
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class ParameterReassignedToCheck method visitForEachStatement.
@Override
public void visitForEachStatement(ForEachStatement tree) {
CFG cfg = CFG.buildCFG(Collections.singletonList(tree), true);
Symbol var = tree.variable().symbol();
boolean liveVar = true;
if (var.owner().isMethodSymbol()) {
cfg.setMethodSymbol((Symbol.MethodSymbol) var.owner());
LiveVariables analyze = LiveVariables.analyze(cfg);
Set<Symbol> live = analyze.getOut(cfg.reversedBlocks().get(1));
liveVar = live.contains(var);
}
if (!liveVar) {
variables.add(var);
}
super.visitForEachStatement(tree);
if (!liveVar) {
variables.remove(var);
}
}
Aggregations