Search in sources :

Example 31 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method completeMetadata.

private static void completeMetadata(JavaSymbol symbol, List<AnnotationTree> annotations) {
    for (AnnotationTree tree : annotations) {
        AnnotationInstanceResolve annotationInstance = new AnnotationInstanceResolve((JavaSymbol.TypeJavaSymbol) tree.symbolType().symbol());
        symbol.metadata().addAnnotation(annotationInstance);
        Arguments arguments = tree.arguments();
        if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
            for (ExpressionTree expressionTree : arguments) {
                AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
                // TODO: Store more precise value than the expression (real value in case of literal, symbol for enums, array of values, solve constants?)
                annotationInstance.addValue(new AnnotationValueResolve(((IdentifierTree) aet.variable()).name(), aet.expression()));
            }
        } else {
            // Constant
            addConstantValue(tree, annotationInstance);
        }
    }
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 32 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitVariable.

@Override
public void visitVariable(VariableTree tree) {
    scan(tree.modifiers());
    completeMetadata(((VariableTreeImpl) tree).getSymbol(), tree.modifiers().annotations());
    // skip type, it has been resolved in second pass
    ExpressionTree initializer = tree.initializer();
    if (initializer != null) {
        resolveAs(initializer, JavaSymbol.VAR);
        if (((JavaType) initializer.symbolType()).isTagged(JavaType.DEFERRED)) {
            setInferedType(tree.type().symbolType(), (DeferredType) initializer.symbolType());
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

Example 33 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitAnnotation.

@Override
public void visitAnnotation(AnnotationTree tree) {
    if (((AbstractTypedTree) tree.annotationType()).isTypeSet()) {
        // This handle the case where type and its annotation is shared between two variables.
        return;
    }
    resolveAs(tree.annotationType(), JavaSymbol.TYP);
    Arguments arguments = tree.arguments();
    if (arguments.size() > 1 || (!arguments.isEmpty() && arguments.get(0).is(Tree.Kind.ASSIGNMENT))) {
        // resolve by identifying correct identifier in assignment.
        for (ExpressionTree expressionTree : arguments) {
            AssignmentExpressionTree aet = (AssignmentExpressionTree) expressionTree;
            IdentifierTree variable = (IdentifierTree) aet.variable();
            JavaSymbol identInType = resolve.findMethod(semanticModel.getEnv(tree), getType(tree.annotationType()), variable.name(), ImmutableList.<JavaType>of()).symbol();
            associateReference(variable, identInType);
            JavaType type = identInType.type;
            if (type == null) {
                type = Symbols.unknownType;
            }
            registerType(variable, type);
            resolveAs(aet.expression(), JavaSymbol.VAR);
        }
    } else {
        for (ExpressionTree expressionTree : arguments) {
            resolveAs(expressionTree, JavaSymbol.VAR);
        }
    }
    registerType(tree, getType(tree.annotationType()));
}
Also used : Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 34 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class ParameterNullnessCheck method reportIssue.

private void reportIssue(Tree syntaxNode, ExpressionTree argument, JavaSymbol.MethodJavaSymbol methodSymbol) {
    String declarationMessage = "constructor declaration";
    if (!methodSymbol.isConstructor()) {
        declarationMessage = "method '" + methodSymbol.getName() + "' declaration";
    }
    String message = String.format("Annotate the parameter with @javax.annotation.Nullable in %s, or make sure that null can not be passed as argument.", declarationMessage);
    Tree reportTree;
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        reportTree = ExpressionUtils.methodName((MethodInvocationTree) syntaxNode);
    } else {
        reportTree = ((NewClassTree) syntaxNode).identifier();
    }
    Flow.Builder secondaryBuilder = Flow.builder();
    MethodTree declarationTree = methodSymbol.declaration();
    if (declarationTree != null) {
        secondaryBuilder.add(new JavaFileScannerContext.Location(StringUtils.capitalize(declarationMessage) + ".", declarationTree.simpleName()));
    }
    secondaryBuilder.add(new JavaFileScannerContext.Location("Argument can be null.", argument));
    reportIssue(reportTree, message, Collections.singleton(secondaryBuilder.build()));
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Flow(org.sonar.java.se.Flow)

Example 35 with ExpressionTree

use of org.sonar.plugins.java.api.tree.ExpressionTree in project sonar-java by SonarSource.

the class FirstPass method visitCompilationUnit.

@Override
public void visitCompilationUnit(CompilationUnitTree tree) {
    JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage;
    PackageDeclarationTree packageDeclaration = tree.packageDeclaration();
    if (packageDeclaration != null) {
        ExpressionTree packageName = packageDeclaration.packageName();
        PackageResolverVisitor packageResolver = new PackageResolverVisitor();
        packageName.accept(packageResolver);
        compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK);
        semanticModel.associateSymbol(packageName, compilationUnitPackage);
    }
    compilationUnitPackage.members = new Scope(compilationUnitPackage);
    env = new Resolve.Env();
    env.packge = compilationUnitPackage;
    env.scope = compilationUnitPackage.members;
    env.namedImports = new Scope.ImportScope(compilationUnitPackage);
    env.starImports = resolve.createStarImportScope(compilationUnitPackage);
    env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage);
    semanticModel.associateEnv(tree, env);
    scan(tree.types());
    restoreEnvironment(tree);
    resolveImports(tree.imports());
    completeSymbols();
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13