Search in sources :

Example 81 with IdentifierTree

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);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 82 with IdentifierTree

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(";");
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) OpensDirectiveTree(org.sonar.plugins.java.api.tree.OpensDirectiveTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) Test(org.junit.Test)

Example 83 with IdentifierTree

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;
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 84 with IdentifierTree

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);
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Collection(java.util.Collection)

Example 85 with IdentifierTree

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.");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7