Search in sources :

Example 46 with Type

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

the class GenericsTest method test_method_resolution_for_parametrized_method_with_type_variable_inheritance.

@Test
public void test_method_resolution_for_parametrized_method_with_type_variable_inheritance() {
    List<Type> elementTypes = declaredTypes("class Test<T> {" + "  <S extends T> void foo(S s) {}" + "  void test() {" + // type substitution provided
    "    new Test<A>().<A>foo(new A());" + "    new Test<A>().<B>foo(new B());" + // type inference
    "    new Test<A>().foo(new A());" + "    new Test<A>().foo(new B());" + "  }" + "}" + "class A {}" + "class B extends A {}");
    JavaType type = (JavaType) elementTypes.get(0);
    JavaSymbol.MethodJavaSymbol methodSymbol = getMethodSymbol(type, "foo");
    assertThat(methodSymbol.usages()).hasSize(4);
    elementTypes = declaredTypes("class Test<T> {" + "  <S extends T> void foo(S s) {}" + "  void test() {" + // does not compile - not resolved
    " new Test<B>().foo(new A());" + " new Test<B>().<A>foo(new A());" + "  }" + "}" + "class A {}" + "class B extends A {}");
    type = (JavaType) elementTypes.get(0);
    methodSymbol = getMethodSymbol(type, "foo");
    assertThat(methodSymbol.usages()).hasSize(0);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Test(org.junit.Test)

Example 47 with Type

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

the class GenericsTest method testLowerBoundedWildCards.

@Test
public void testLowerBoundedWildCards() {
    List<Type> elementTypes = declaredTypesUsingHierarchy("B<?>", "B<? super Object>", "A<? super Object>", "A<? super Animal>", "A<? super Cat>", "A<Cat>", "A<Cat>");
    Type wcBType = elementTypes.get(0);
    Type wcSuperObjectBType = elementTypes.get(1);
    Type wcSuperObjectAType = elementTypes.get(2);
    Type wcSuperAnimalAType = elementTypes.get(3);
    Type wcSuperCatAType = elementTypes.get(4);
    Type catAType = elementTypes.get(5);
    Type catAType2 = elementTypes.get(6);
    SubtypeAssert.assertThat(wcBType).isNotSubtypeOf(wcSuperObjectAType);
    SubtypeAssert.assertThat(wcSuperObjectAType).isNotSubtypeOf(wcBType);
    SubtypeAssert.assertThat(wcBType).isNotSubtypeOf(wcSuperObjectBType);
    SubtypeAssert.assertThat(wcSuperObjectBType).isSubtypeOf(wcBType);
    SubtypeAssert.assertThat(wcSuperObjectAType).isNotSubtypeOf(wcSuperObjectBType);
    SubtypeAssert.assertThat(wcSuperObjectBType).isSubtypeOf(wcSuperObjectAType);
    SubtypeAssert.assertThat(wcSuperObjectBType).isSubtypeOf(wcSuperAnimalAType);
    SubtypeAssert.assertThat(wcSuperAnimalAType).isNotSubtypeOf(wcSuperObjectBType);
    SubtypeAssert.assertThat(wcSuperAnimalAType).isSubtypeOf(wcSuperCatAType);
    SubtypeAssert.assertThat(wcSuperCatAType).isNotSubtypeOf(wcSuperAnimalAType);
    SubtypeAssert.assertThat(catAType).isSubtypeOf(wcSuperCatAType);
    SubtypeAssert.assertThat(wcSuperCatAType).isNotSubtypeOf(catAType);
    SubtypeAssert.assertThat(catAType).isSubtypeOf(catAType2);
    SubtypeAssert.assertThat(catAType2).isSubtypeOf(catAType);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Test(org.junit.Test)

Example 48 with Type

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

the class ArrayForVarArgCheck method checkInvokedMethod.

private void checkInvokedMethod(JavaSymbol.MethodJavaSymbol methodSymbol, @Nullable MethodJavaType methodType, ExpressionTree lastArg) {
    if (methodSymbol.isVarArgs() && lastArg.is(Tree.Kind.NEW_ARRAY)) {
        if (lastParamHasSameType(methodSymbol, methodType, lastArg.symbolType())) {
            String message = "Remove this array creation";
            NewArrayTree newArrayTree = (NewArrayTree) lastArg;
            if (newArrayTree.openBraceToken() == null) {
                ExpressionTree expression = newArrayTree.dimensions().get(0).expression();
                Integer literalValue = LiteralUtils.intLiteralValue(expression);
                if (literalValue == null || literalValue != 0 || isCallingOverload(methodSymbol, lastArg)) {
                    return;
                }
            } else if (!newArrayTree.initializers().isEmpty()) {
                message += " and simply pass the elements";
            }
            reportIssue(lastArg, message + ".");
        } else {
            String type = ((Type.ArrayType) getLastParameterType(methodSymbol.parameterTypes())).elementType().name();
            reportIssue(lastArg, "Disambiguate this call by either casting as \"" + type + "\" or \"" + type + "[]\".");
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodJavaType(org.sonar.java.resolve.MethodJavaType) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 49 with Type

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

the class PrimitiveTypeBoxingWithToStringCheck method isValueOfInvocation.

private static boolean isValueOfInvocation(ExpressionTree abstractTypedTree) {
    if (!abstractTypedTree.is(Kind.METHOD_INVOCATION)) {
        return false;
    }
    Type type = abstractTypedTree.symbolType();
    MethodMatcher valueOfMatcher = MethodMatcher.create().typeDefinition(type.fullyQualifiedName()).name("valueOf").addParameter(((JavaType) type).primitiveType().fullyQualifiedName());
    return valueOfMatcher.matches((MethodInvocationTree) abstractTypedTree);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) JavaType(org.sonar.java.resolve.JavaType) JavaType(org.sonar.java.resolve.JavaType) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 50 with Type

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

the class PrintfMisuseCheck method verifyParameters.

private void verifyParameters(MethodInvocationTree mit, List<ExpressionTree> args, List<String> params) {
    int index = 0;
    List<ExpressionTree> unusedArgs = new ArrayList<>(args);
    for (String rawParam : params) {
        String param = rawParam;
        int argIndex = index;
        if (param.contains("$")) {
            argIndex = getIndex(param) - 1;
            if (argIndex == -1) {
                return;
            }
            param = param.substring(param.indexOf('$') + 1);
        } else if (param.charAt(0) == '<') {
            // refers to previous argument
            argIndex = Math.max(0, argIndex - 1);
        } else {
            index++;
        }
        ExpressionTree argExpressionTree = args.get(argIndex);
        unusedArgs.remove(argExpressionTree);
        Type argType = argExpressionTree.symbolType();
        checkBoolean(mit, param, argType);
    }
    reportUnusedArgs(mit, args, unusedArgs);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Aggregations

Type (org.sonar.plugins.java.api.semantic.Type)164 Test (org.junit.Test)67 Symbol (org.sonar.plugins.java.api.semantic.Symbol)23 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)23 MethodJavaSymbol (org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol)18 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)18 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)17 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)17 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 JavaType (org.sonar.java.resolve.JavaType)16 Tree (org.sonar.plugins.java.api.tree.Tree)15 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)13 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)12 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)11 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)10 ArrayList (java.util.ArrayList)9 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)8 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)8