Search in sources :

Example 11 with Symbol

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

the class CastArithmeticOperandCheck method checkMethodInvocationArgument.

private void checkMethodInvocationArgument(MethodInvocationTree mit) {
    Symbol symbol = mit.symbol();
    if (symbol.isMethodSymbol()) {
        List<Type> parametersTypes = ((Symbol.MethodSymbol) symbol).parameterTypes();
        if (mit.arguments().size() == parametersTypes.size()) {
            int i = 0;
            for (Type argType : parametersTypes) {
                checkExpression(argType, mit.arguments().get(i));
                i++;
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 12 with Symbol

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

the class AssertionsWithoutMessageCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    Symbol symbol = mit.symbol();
    if (symbol.owner().type().isSubtypeOf(GENERIC_ASSERT) && !FEST_AS_METHOD.matches(mit)) {
        if (isConstructor(symbol)) {
            return;
        }
        FestVisitor visitor = new FestVisitor();
        mit.methodSelect().accept(visitor);
        if (!visitor.useDescription) {
            reportIssue(mit, "Add a message to this assertion.");
        }
    } else if (mit.arguments().isEmpty() || !isString(mit.arguments().get(0)) || isAssertingOnStringWithNoMessage(mit)) {
        reportIssue(mit, "Add a message to this assertion.");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 13 with Symbol

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

the class BytecodeCompleterTest method annotationArrayOfEnum.

@Test
public void annotationArrayOfEnum() {
    JavaSymbol.TypeJavaSymbol classSymbol = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.AnnotationSymbolMethod");
    Symbol barMethod = classSymbol.memberSymbols().stream().filter(symbol -> "bar".equals(symbol.name())).findFirst().get();
    SymbolMetadata barMetadata = barMethod.metadata();
    assertThat(barMetadata.annotations()).hasSize(1);
    List<SymbolMetadata.AnnotationValue> valuesForAnnotation = barMetadata.valuesForAnnotation("org.sonar.java.resolve.targets.ArrayEnumAnnotation");
    assertThat(valuesForAnnotation).hasSize(1);
    assertThat(valuesForAnnotation.get(0).name()).isEqualTo("value");
    Object annotationValue = valuesForAnnotation.get(0).value();
    assertThat(annotationValue).isInstanceOf(Object[].class);
    assertThat((Object[]) annotationValue).hasSize(2);
    assertThat(Arrays.stream((Object[]) annotationValue)).allMatch(o -> o instanceof Symbol && ((Symbol) o).type().is("org.sonar.java.resolve.targets.MyEnum"));
}
Also used : SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Test(org.junit.Test)

Example 14 with Symbol

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

the class BytecodeCompleterTest method deprecated_classes_should_be_flagged.

@Test
public void deprecated_classes_should_be_flagged() throws Exception {
    JavaSymbol.TypeJavaSymbol deprecatedClass = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DeprecatedClass");
    assertThat(deprecatedClass.isDeprecated()).isTrue();
    JavaSymbol.TypeJavaSymbol staticInnerClass = (JavaSymbol.TypeJavaSymbol) deprecatedClass.members().lookup("StaticInnerClass").get(0);
    assertThat(staticInnerClass.isDeprecated()).isTrue();
    JavaSymbol.TypeJavaSymbol innerClass = (JavaSymbol.TypeJavaSymbol) deprecatedClass.members().lookup("InnerClass").get(0);
    assertThat(innerClass.isDeprecated()).isTrue();
    JavaSymbol.TypeJavaSymbol deprecatedEnum = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.DeprecatedEnum");
    assertThat(deprecatedEnum.isDeprecated()).isTrue();
    assertThat(deprecatedEnum.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isEnum).noneMatch(Symbol::isDeprecated)).isTrue();
    JavaSymbol.TypeJavaSymbol partiallyDeprecatedEnum = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.PartiallyDeprecatedEnum");
    assertThat(partiallyDeprecatedEnum.isDeprecated()).isFalse();
    List<Symbol> deprecatedEnumConstants = partiallyDeprecatedEnum.memberSymbols().stream().filter(Symbol::isVariableSymbol).filter(Symbol::isEnum).filter(Symbol::isDeprecated).collect(Collectors.toList());
    assertThat(deprecatedEnumConstants).hasSize(1);
    assertThat(deprecatedEnumConstants.get(0).name()).isEqualTo("C");
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Test(org.junit.Test)

Example 15 with Symbol

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

the class BytecodeCompleterTest method forward_type_parameter_in_methods.

@Test
public void forward_type_parameter_in_methods() throws Exception {
    Symbol.TypeSymbol clazz = bytecodeCompleter.getClassSymbol("org.sonar.java.resolve.targets.ForwardParameterInMethod");
    assertThat(clazz.type()).isNotNull();
    Collection<Symbol> symbols = clazz.lookupSymbols("bar");
    assertThat(symbols).hasSize(1);
    Symbol method = symbols.iterator().next();
    Collection<JavaSymbol> typeParameters = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().scopeSymbols();
    assertThat(typeParameters).hasSize(2);
    JavaSymbol xSymbol = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().lookup("X").iterator().next();
    JavaSymbol ySymbol = ((JavaSymbol.MethodJavaSymbol) method).typeParameters().lookup("Y").iterator().next();
    assertThat(((TypeVariableJavaType) xSymbol.type).bounds).hasSize(1);
    JavaType bound = ((TypeVariableJavaType) xSymbol.type).bounds.get(0);
    assertThat(((ParametrizedTypeJavaType) bound).typeParameters()).hasSize(1);
    assertThat(((ParametrizedTypeJavaType) bound).substitution(((ParametrizedTypeJavaType) bound).typeParameters().get(0))).isSameAs(ySymbol.type);
}
Also used : TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) TypeJavaSymbol(org.sonar.java.resolve.JavaSymbol.TypeJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) 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