Search in sources :

Example 1 with TypeSymbol

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

the class AnnotationArgumentOrderCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    List<String> declarationNames = new ArrayList<>();
    for (Symbol symbol : annotationSymbol.memberSymbols()) {
        declarationNames.add(symbol.name());
    }
    List<String> annotationArguments = new ArrayList<>();
    for (ExpressionTree argument : annotationTree.arguments()) {
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            annotationArguments.add(nameTree.name());
        }
    }
    declarationNames.retainAll(annotationArguments);
    if (!declarationNames.equals(annotationArguments)) {
        reportIssue(annotationTree.annotationType(), "Reorder annotation arguments to match the order of declaration.");
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayList(java.util.ArrayList) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 2 with TypeSymbol

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

the class AnnotationDefaultArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
    for (ExpressionTree argument : annotationTree.arguments()) {
        Tree valueSet = argument;
        // Single element annotation : JLS8 9.7.3 : one param must be named value.
        String paramName = "value";
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            paramName = nameTree.name();
            valueSet = assignmentTree.expression();
        }
        if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
            reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
        }
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LiteralUtils(org.sonar.java.model.LiteralUtils) Map(java.util.Map) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 3 with TypeSymbol

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

the class PrivateFieldUsedLocallyCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TypeSymbol classSymbol = ((ClassTree) tree).symbol();
    Set<Symbol> fieldsReadOnAnotherInstance = FieldsReadOnAnotherInstanceVisitor.getFrom(tree);
    classSymbol.memberSymbols().stream().filter(PrivateFieldUsedLocallyCheck::isPrivateField).filter(s -> !(s.isFinal() && s.isStatic())).filter(s -> !hasAnnotation(s)).filter(s -> !s.usages().isEmpty()).filter(s -> !fieldsReadOnAnotherInstance.contains(s)).forEach(s -> checkPrivateField(s, classSymbol));
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) HashSet(java.util.HashSet) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) CFG(org.sonar.java.cfg.CFG) ImmutableList(com.google.common.collect.ImmutableList) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ProgramState.isField(org.sonar.java.se.ProgramState.isField) LiveVariables(org.sonar.java.cfg.LiveVariables) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 4 with TypeSymbol

use of org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol 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)

Aggregations

TypeSymbol (org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)4 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Nullable (javax.annotation.Nullable)2 Rule (org.sonar.check.Rule)2 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)2 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)2 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CheckForNull (javax.annotation.CheckForNull)1 CFG (org.sonar.java.cfg.CFG)1 LiveVariables (org.sonar.java.cfg.LiveVariables)1