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);
}
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);
}
}
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);
}
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();
}
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();
}
Aggregations