Search in sources :

Example 1 with ClassJavaType

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 + "'.");
        });
    }
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassJavaType(org.sonar.java.resolve.ClassJavaType)

Example 2 with ClassJavaType

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));
}
Also used : ClassJavaType(org.sonar.java.resolve.ClassJavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ClassJavaType(org.sonar.java.resolve.ClassJavaType) Test(org.junit.Test)

Example 3 with ClassJavaType

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!");
}
Also used : ClassJavaType(org.sonar.java.resolve.ClassJavaType) Type(org.sonar.plugins.java.api.semantic.Type) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ClassJavaType(org.sonar.java.resolve.ClassJavaType) Test(org.junit.Test)

Example 4 with ClassJavaType

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;
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ClassJavaType(org.sonar.java.resolve.ClassJavaType) ClassJavaType(org.sonar.java.resolve.ClassJavaType) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 5 with ClassJavaType

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);
        }
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Type(org.sonar.plugins.java.api.semantic.Type) ClassJavaType(org.sonar.java.resolve.ClassJavaType) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ClassJavaType(org.sonar.java.resolve.ClassJavaType) HashSet(java.util.HashSet)

Aggregations

ClassJavaType (org.sonar.java.resolve.ClassJavaType)5 Type (org.sonar.plugins.java.api.semantic.Type)4 Test (org.junit.Test)2 JavaSymbol (org.sonar.java.resolve.JavaSymbol)2 HashSet (java.util.HashSet)1 ParametrizedTypeJavaType (org.sonar.java.resolve.ParametrizedTypeJavaType)1 TypeSymbol (org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1