use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class BehaviorCacheTest method test_peek.
@Test
public void test_peek() throws Exception {
Set<String> testedPre = new HashSet<>();
Set<String> testedPost = new HashSet<>();
SECheck check = new SECheck() {
@Override
public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
assertThat(peekMethodBehavior).isNull();
testedPre.add(symbol.name());
}
return context.getState();
}
@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
String methodName = symbol.name();
MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
assertThat(peekMethodBehavior).isNotNull();
if ("foo".equals(methodName) || "isBlank".equals(methodName)) {
// foo should have been computed
assertThat(peekMethodBehavior.isComplete()).isTrue();
} else if ("bar".equals(methodName)) {
assertThat(peekMethodBehavior.isComplete()).isFalse();
}
testedPost.add(methodName);
}
return super.checkPostStatement(context, syntaxNode);
}
};
SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/BehaviorCachePeek.java", check);
assertThat(sev.behaviorCache.peek("org.apache.commons.lang.StringUtils#isBlank(Ljava/lang/String;)Z").isComplete()).isTrue();
assertThat(sev.behaviorCache.peek("org.foo.A#foo()Z").isComplete()).isTrue();
assertThat(sev.behaviorCache.peek("org.foo.A#bar()Z").isComplete()).isFalse();
assertThat(sev.behaviorCache.peek("org.foo.A#unknownMethod()Z")).isNull();
assertThat(sev.behaviorCache.behaviors.keySet()).containsOnly("org.foo.A#foo()Z");
assertThat(testedPre).containsOnly("foo", "bar", "isBlank");
assertThat(testedPost).containsOnly("foo", "bar", "isBlank");
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class NullableAnnotationUtilsTest method testIsAnnotatedNullable.
@Test
public void testIsAnnotatedNullable() {
Symbol foo = getSymbol("foo");
assertThat(isAnnotatedNullable(foo)).isFalse();
getSymbols("nullable").forEach(s -> {
assertThat(isAnnotatedNullable(s)).as(s + " should be recognized as Nullable.").isTrue();
assertThat(isAnnotatedNonNull(s)).as(s + " should NOT be recognized as Nonnull.").isFalse();
});
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class VariableDeclarationScopeCheck method check.
private void check(VariableTree variable, List<StatementTree> body, int bodySize, int next) {
Symbol symbol = variable.symbol();
ReferenceVisitor referenceVisitor = new ReferenceVisitor(symbol);
for (int i = next; i < bodySize; i++) {
referenceVisitor.visit(body.get(i));
if (referenceVisitor.referenceSymbol) {
return;
} else if (referenceVisitor.hasBreakingStatement) {
reportIssue(variable.simpleName(), "Move the declaration of \"" + symbol.name() + "\" closer to the code that uses it.");
return;
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class UnusedLocalVariableCheck method checkVariableUsages.
private void checkVariableUsages() {
for (VariableTree variableTree : variables) {
Symbol symbol = variableTree.symbol();
if (symbol.usages().size() == assignments.get(symbol).size()) {
IdentifierTree simpleName = variableTree.simpleName();
reportIssue(simpleName, "Remove this unused \"" + simpleName + "\" local variable.");
}
}
}
use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.
the class UnusedMethodParameterCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
MethodTree methodTree = (MethodTree) tree;
if (methodTree.block() == null || isExcluded(methodTree)) {
return;
}
Set<String> documentedParameters = documentedParameters(methodTree);
boolean overridableMethod = overridableMethod(methodTree.symbol());
List<IdentifierTree> unused = Lists.newArrayList();
for (VariableTree var : methodTree.parameters()) {
Symbol symbol = var.symbol();
if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION) && !isStrutsActionParameter(var) && (!overridableMethod || !documentedParameters.contains(symbol.name()))) {
unused.add(var.simpleName());
}
}
Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
// kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of unused
unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name())).collect(Collectors.toList());
if (!unused.isEmpty()) {
reportUnusedParameters(unused);
}
}
Aggregations