use of org.sonar.java.resolve.ClassJavaType in project sonar-java by SonarSource.
the class CompareToNotOverloadedCheck method visitNode.
@Override
public void visitNode(Tree tree) {
MethodTree methodTree = (MethodTree) tree;
if (hasSemantic() && isCompareToMethod(methodTree) && Boolean.FALSE.equals(methodTree.isOverriding())) {
ClassJavaType ownerType = (ClassJavaType) methodTree.symbol().owner().type();
ownerType.superTypes().stream().filter(supertype -> supertype.is("java.lang.Comparable")).findFirst().ifPresent(comparableType -> {
String name = "Object";
if (comparableType.isParameterized()) {
ParametrizedTypeJavaType ptjt = (ParametrizedTypeJavaType) comparableType;
name = ptjt.substitution(ptjt.typeParameters().get(0)).symbol().name();
}
reportIssue(methodTree.parameters().get(0), "Refactor this method so that its argument is of type '" + name + "'.");
});
}
}
use of org.sonar.java.resolve.ClassJavaType in project sonar-java by SonarSource.
the class SymbolicValueTest method exceptional_SV_equals.
@Test
public void exceptional_SV_equals() {
JavaSymbol.PackageJavaSymbol packageSymbol = new JavaSymbol.PackageJavaSymbol("org.foo.bar", null);
JavaSymbol.TypeJavaSymbol exceptionSymbol = new JavaSymbol.TypeJavaSymbol(Flags.PUBLIC, "MyException", packageSymbol);
Type exceptionType = new ClassJavaType(exceptionSymbol);
SymbolicValue.ExceptionalSymbolicValue sv = new SymbolicValue.ExceptionalSymbolicValue(exceptionType);
assertThat(sv).isEqualTo(sv);
assertThat(sv).isNotEqualTo(null);
assertThat(sv).isNotEqualTo(new SymbolicValue());
// different IDs but same exception
assertThat(sv).isNotEqualTo(new SymbolicValue.ExceptionalSymbolicValue(sv.exceptionType()));
// same IDs but different exception
assertThat(sv).isNotEqualTo(new SymbolicValue.ExceptionalSymbolicValue(null));
}
use of org.sonar.java.resolve.ClassJavaType in project sonar-java by SonarSource.
the class SymbolicValueTest method exceptional_SV_should_contain_exception_type_in_toString.
@Test
public void exceptional_SV_should_contain_exception_type_in_toString() {
SymbolicValue.ExceptionalSymbolicValue unknownException = new SymbolicValue.ExceptionalSymbolicValue(null);
// contains the exception
assertThat(unknownException.toString()).contains("!unknownException!");
JavaSymbol.PackageJavaSymbol packageSymbol = new JavaSymbol.PackageJavaSymbol("org.foo.bar", null);
JavaSymbol.TypeJavaSymbol exceptionSymbol = new JavaSymbol.TypeJavaSymbol(Flags.PUBLIC, "MyException", packageSymbol);
Type exceptionType = new ClassJavaType(exceptionSymbol);
SymbolicValue.ExceptionalSymbolicValue knownException = new SymbolicValue.ExceptionalSymbolicValue(exceptionType);
// contains the exception
assertThat(knownException.toString()).contains("org.foo.bar.MyException!");
}
use of org.sonar.java.resolve.ClassJavaType in project sonar-java by SonarSource.
the class UselessExtendsCheck method checkRedundancy.
private void checkRedundancy(TypeTree currentInterface, List<Type> superInterfacesTypes, Set<ClassJavaType> superTypes) {
Type interfaceType = currentInterface.symbolType();
for (ClassJavaType superType : superTypes) {
TypeSymbol superTypeSymbol = superType.symbol();
if (superTypeSymbol.interfaces().contains(interfaceType)) {
String typeOfParentMsg = "implemented by a super class";
if (superTypeSymbol.isInterface() && superInterfacesTypes.contains(superType)) {
typeOfParentMsg = "already extended by \"" + superTypeSymbol.name() + "\"";
}
reportIssue(currentInterface, "\"" + interfaceType.name() + "\" is " + typeOfParentMsg + "; there is no need to implement it here.");
break;
}
}
}
use of org.sonar.java.resolve.ClassJavaType in project sonar-java by SonarSource.
the class UselessExtendsCheck method visitNode.
@Override
public void visitNode(Tree tree) {
if (!hasSemantic()) {
return;
}
ClassTree classTree = (ClassTree) tree;
checkExtendsObject(classTree);
ListTree<TypeTree> superInterfaces = classTree.superInterfaces();
if (superInterfaces.isEmpty()) {
return;
}
Set<ClassJavaType> superTypes = ((JavaSymbol.TypeJavaSymbol) classTree.symbol()).superTypes();
List<Type> superInterfacesTypes = getTypes(superInterfaces);
Set<String> reportedNames = new HashSet<>();
for (TypeTree superInterface : superInterfaces) {
String superInterfaceName = extractInterfaceName(superInterface);
if (isDuplicate(superInterfaces, superInterface) && !reportedNames.add(superInterfaceName)) {
// add an issue on a duplicated interface the second time it is encountered
reportIssue(superInterface, "\"" + superInterfaceName + "\" is listed multiple times.");
}
if (!superInterface.symbolType().isUnknown()) {
checkRedundancy(superInterface, superInterfacesTypes, superTypes);
}
}
}
Aggregations