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);
}
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);
}
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 + "[]\".");
}
}
}
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);
}
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);
}
Aggregations