use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class JavaTreeModelTest method explicit_constructor_invocation.
/**
* 8.8.7.1. Explicit Constructor Invocations
*/
@Test
public void explicit_constructor_invocation() {
// TODO test NonWildTypeArguments
MethodInvocationTree tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>this(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("this");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { <T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
assertThat(((IdentifierTree) tree.methodSelect()).name()).isEqualTo("super");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
MemberSelectExpressionTree methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
tree = (MethodInvocationTree) expressionOfFirstStatement("class T { T() { ClassName.<T>super(true, false); } }");
assertThat(tree.is(Tree.Kind.METHOD_INVOCATION)).isTrue();
methodSelect = (MemberSelectExpressionTree) tree.methodSelect();
assertThatChildrenIteratorHasSize(methodSelect, 3);
assertThat(methodSelect.identifier().name()).isEqualTo("super");
assertThat(methodSelect.operatorToken()).isNotNull();
assertThat(((IdentifierTree) methodSelect.expression()).name()).isEqualTo("ClassName");
assertThat(tree.arguments()).hasSize(2);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class OpensDirectiveTreeImplTest method opens_with_modules.
@Test
public void opens_with_modules() {
OpensDirectiveTree exports = moduleDirective("opens org.foo to com.module1, module2;");
assertThat(exports.kind()).isEqualTo(Tree.Kind.OPENS_DIRECTIVE);
assertThat(exports.directiveKeyword().text()).isEqualTo("opens");
ExpressionTree packageName = exports.packageName();
assertThat(packageName.is(Tree.Kind.MEMBER_SELECT)).isTrue();
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) packageName;
assertThat(((IdentifierTree) mset.expression()).name()).isEqualTo("org");
assertThat(mset.identifier().name()).isEqualTo("foo");
assertThat(exports.toKeyword().text()).isEqualTo("to");
ListTree<ModuleNameTree> moduleNames = exports.moduleNames();
assertThat(moduleNames).hasSize(2);
assertThat(moduleNames.get(0).stream().map(IdentifierTree::name)).containsExactly("com", "module1");
assertThat(moduleNames.separators()).hasSize(1);
assertThat(moduleNames.separators().iterator().next().text()).isEqualTo(",");
assertThat(moduleNames.get(1).stream().map(IdentifierTree::name)).containsExactly("module2");
assertThat(exports.semicolonToken().text()).isEqualTo(";");
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class AbstractInjectionChecker method isIdentifierDynamicString.
protected boolean isIdentifierDynamicString(Tree methodTree, IdentifierTree arg, @Nullable Symbol currentlyChecking, boolean firstLevel) {
Symbol symbol = arg.symbol();
if (isExcluded(currentlyChecking, symbol)) {
return false;
}
boolean isLocalVar = symbol.owner().isMethodSymbol() && !((JavaSymbol.MethodJavaSymbol) symbol.owner()).getParameters().scopeSymbols().contains(symbol);
if (isLocalVar) {
// symbol is a local variable, check it is not a dynamic string.
// Check declaration
VariableTree declaration = ((Symbol.VariableSymbol) symbol).declaration();
ExpressionTree initializer = declaration.initializer();
if (initializer != null && isDynamicString(methodTree, initializer, currentlyChecking)) {
return true;
}
// check usages by revisiting the enclosing tree.
Collection<IdentifierTree> usages = symbol.usages();
LocalVariableDynamicStringVisitor visitor = new LocalVariableDynamicStringVisitor(symbol, usages, methodTree);
Tree argEnclosingDeclarationTree = semanticModel.getTree(semanticModel.getEnv(symbol));
argEnclosingDeclarationTree.accept(visitor);
return visitor.dynamicString;
}
// arg is not a local variable nor a constant, so it is a parameter or a field.
parameterName = "\"" + arg.name() + "\"";
return symbol.owner().isMethodSymbol() && !firstLevel;
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CatchUsesExceptionWithContextCheck method visitMemberSelectExpression.
@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
IdentifierTree identifier = null;
ExpressionTree expression = tree.expression();
if (expression.is(Kind.IDENTIFIER)) {
identifier = (IdentifierTree) expression;
} else if (expression.is(Kind.PARENTHESIZED_EXPRESSION) && ((ParenthesizedTree) expression).expression().is(Kind.IDENTIFIER)) {
identifier = (IdentifierTree) ((ParenthesizedTree) expression).expression();
}
if (!validUsagesStack.isEmpty() && identifier != null) {
Iterator<Collection<IdentifierTree>> iterator = validUsagesStack.iterator();
while (iterator.hasNext()) {
iterator.next().remove(identifier);
}
}
super.visitMemberSelectExpression(tree);
}
use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.
the class CatchUsesExceptionWithContextCheck method visitCatch.
@Override
public void visitCatch(CatchTree tree) {
if (!isExcludedType(tree.parameter().type()) && !excludedCatchTrees.contains(tree)) {
Symbol exception = tree.parameter().symbol();
validUsagesStack.addFirst(Lists.newArrayList(exception.usages()));
super.visitCatch(tree);
Collection<IdentifierTree> usages = validUsagesStack.pop();
if (usages.isEmpty()) {
context.reportIssue(this, tree.parameter(), "Either log or rethrow this exception.");
}
}
}
Aggregations