Search in sources :

Example 76 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class RedundantTypeCastCheck method requiredForMemberAccess.

private static boolean requiredForMemberAccess(TypeCastTree typeCastTree) {
    ExpressionTree expression = typeCastTree.expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        Tree parent = typeCastTree.parent();
        return expression.is(Tree.Kind.METHOD_REFERENCE) && parent != null && skipParentheses(parent).is(Tree.Kind.MEMBER_SELECT);
    }
    Symbol symbol = ((MethodInvocationTree) expression).symbol();
    if (!symbol.isMethodSymbol()) {
        return false;
    }
    Type returnType = ((Symbol.MethodSymbol) symbol).returnType().type();
    if (!(returnType instanceof TypeVariableJavaType) || ((TypeVariableJavaType) returnType).bounds().get(0).is("java.lang.Object")) {
        return false;
    }
    // as the member accessed could have also been part of initial type
    return skipParentheses(typeCastTree.parent()).is(Tree.Kind.MEMBER_SELECT);
}
Also used : JavaType(org.sonar.java.resolve.JavaType) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) JavaSymbol(org.sonar.java.resolve.JavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TypeVariableJavaType(org.sonar.java.resolve.TypeVariableJavaType) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 77 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class StaticMethodCheck method visitTerminalIdentifier.

private void visitTerminalIdentifier(IdentifierTree tree) {
    Symbol symbol = tree.symbol();
    MethodReference currentMethod = methodReferences.peek();
    if (symbol.isUnknown()) {
        currentMethod.setNonStaticReference();
        return;
    }
    for (MethodReference methodReference : methodReferences) {
        methodReference.checkSymbol(symbol);
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 78 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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 Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class MethodMatcherCollectionTest method should_match_if_any_of_the_matchers_match_symbol.

@Test
public void should_match_if_any_of_the_matchers_match_symbol() {
    Symbol mockSymbol = mock(Symbol.class);
    when(mockSymbol.isMethodSymbol()).thenReturn(true);
    MethodMatcher matcher1 = mock(MethodMatcher.class);
    when(matcher1.matches(any(Symbol.class))).thenReturn(false);
    MethodMatcher matcher2 = mock(MethodMatcher.class);
    when(matcher2.matches(any(Symbol.class))).thenReturn(true);
    assertThat(MethodMatcherCollection.create(matcher1, matcher2).anyMatch(mockSymbol)).isTrue();
    when(matcher1.matches(any(Symbol.class))).thenReturn(false);
    when(matcher2.matches(any(Symbol.class))).thenReturn(false);
    assertThat(MethodMatcherCollection.create(matcher1, matcher2).anyMatch(mockSymbol)).isFalse();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 80 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11