Search in sources :

Example 76 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class StaticMultithreadedUnsafeFieldsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    VariableTree variableTree = (VariableTree) tree;
    Type type = variableTree.type().symbolType();
    if (ModifiersUtils.hasModifier(variableTree.modifiers(), Modifier.STATIC) && isForbiddenType(variableTree)) {
        if (type.isSubtypeOf(JAVA_TEXT_SIMPLE_DATE_FORMAT) && onlySynchronizedUsages((Symbol.VariableSymbol) variableTree.symbol())) {
            return;
        }
        IdentifierTree identifierTree = variableTree.simpleName();
        reportIssue(identifierTree, String.format("Make \"%s\" an instance variable.", identifierTree.name()));
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 77 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class UndocumentedApiCheck method className.

private String className() {
    String className = packageName;
    IdentifierTree identifierTree = classTrees.peek().simpleName();
    if (identifierTree != null) {
        className += "." + identifierTree.name();
    }
    return className;
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 78 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class StaticMethodCheck method visitMemberSelectExpression.

@Override
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
    if (tree.expression().is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) tree.expression();
        Symbol owner = identifier.symbol().owner();
        if (owner != null && owner.isMethodSymbol()) {
            // No need to investigate selection on local symbols
            return;
        }
    }
    super.visitMemberSelectExpression(tree);
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 79 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class EnumConstantTest method test_annotation.

@Test
public void test_annotation() {
    LexerlessGrammarBuilder b = JavaLexer.createGrammarBuilder();
    ActionParser<Tree> parser = new ActionParser<>(StandardCharsets.UTF_8, b, JavaGrammar.class, new TreeFactory(), new JavaNodeBuilder(), JavaLexer.ENUM_CONSTANT);
    EnumConstantTreeImpl node = (EnumConstantTreeImpl) parser.parse("@Foo CONSTANT");
    assertThat(node.modifiers().size()).isEqualTo(1);
    assertThat(((IdentifierTree) ((AnnotationTreeImpl) node.modifiers().get(0)).annotationType()).identifierToken().text()).isEqualTo("Foo");
}
Also used : AnnotationTreeImpl(org.sonar.java.model.declaration.AnnotationTreeImpl) ActionParser(com.sonar.sslr.api.typed.ActionParser) TreeFactory(org.sonar.java.ast.parser.TreeFactory) LexerlessGrammarBuilder(org.sonar.sslr.grammar.LexerlessGrammarBuilder) JavaNodeBuilder(org.sonar.java.ast.parser.JavaNodeBuilder) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) EnumConstantTreeImpl(org.sonar.java.model.declaration.EnumConstantTreeImpl) Test(org.junit.Test)

Example 80 with IdentifierTree

use of org.sonar.plugins.java.api.tree.IdentifierTree in project sonar-java by SonarSource.

the class MethodMatcherTest method does_not_match_without_callSite_enclosingClass.

@Test
public void does_not_match_without_callSite_enclosingClass() throws Exception {
    MethodMatcher matcher = MethodMatcher.create().name(NameCriteria.any()).withAnyParameters().callSite(TypeCriteria.anyType());
    Symbol symbol = mock(Symbol.class);
    when(symbol.enclosingClass()).thenReturn(null);
    IdentifierTree id = mock(IdentifierTree.class);
    when(id.is(Tree.Kind.IDENTIFIER)).thenReturn(true);
    when(id.symbol()).thenReturn(symbol);
    MethodInvocationTree tree = mock(MethodInvocationTree.class);
    when(tree.methodSelect()).thenReturn(id);
    assertThat(matcher.matches(tree)).isFalse();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

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