Search in sources :

Example 96 with IdentifierTree

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

the class TypeAndReferenceSolver method resolveMethodReference.

private void resolveMethodReference(JavaSymbol.MethodJavaSymbol samMethod, MethodReferenceTreeImpl methodRefTree) {
    JavaType methodRefType = (JavaType) methodRefTree.symbolType();
    JavaType samReturnType = (JavaType) samMethod.returnType().type();
    List<JavaType> samMethodArgs = resolve.findSamMethodArgs(methodRefType);
    Resolution resolution = resolve.findMethodReference(semanticModel.getEnv(methodRefTree), samMethodArgs, methodRefTree);
    JavaSymbol methodSymbol = resolution.symbol();
    if (methodSymbol.isMethodSymbol()) {
        IdentifierTree methodIdentifier = methodRefTree.method();
        addMethodRefReference(methodIdentifier, methodSymbol);
        setMethodRefType(methodRefTree, methodRefType, resolution.type());
        JavaType capturedReturnType = resolve.resolveTypeSubstitution(samReturnType, methodRefType);
        JavaType refinedReturnType = ((MethodJavaType) methodIdentifier.symbolType()).resultType();
        if ("<init>".equals(methodSymbol.name)) {
            refinedReturnType = refinedTypeForConstructor(capturedReturnType, refinedReturnType);
        }
        if (refinedReturnType instanceof DeferredType) {
            ((DeferredType) refinedReturnType).setTree((AbstractTypedTree) methodRefTree.method());
        }
        refineType(methodRefTree, methodRefType, capturedReturnType, refinedReturnType);
    } else {
        handleNewArray(methodRefTree, methodRefType, samReturnType);
    }
}
Also used : IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 97 with IdentifierTree

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

the class TypeAndReferenceSolver method visitNewClass.

@Override
public void visitNewClass(NewClassTree tree) {
    NewClassTreeImpl newClassTreeImpl = (NewClassTreeImpl) tree;
    if (newClassTreeImpl.isTypeSet()) {
        return;
    }
    List<JavaType> typeArgumentsTypes = ImmutableList.of();
    if (tree.typeArguments() != null) {
        resolveAs((List<Tree>) tree.typeArguments(), JavaSymbol.TYP);
        typeArgumentsTypes = tree.typeArguments().stream().map(this::getType).collect(Collectors.toList());
    }
    resolveAs((List<ExpressionTree>) tree.arguments(), JavaSymbol.VAR);
    List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
    Resolve.Env newClassEnv = semanticModel.getEnv(tree);
    ExpressionTree enclosingExpression = tree.enclosingExpression();
    TypeTree typeTree = tree.identifier();
    IdentifierTree constructorIdentifier = newClassTreeImpl.getConstructorIdentifier();
    JavaType identifierType = resolveIdentifierType(newClassEnv, enclosingExpression, typeTree, constructorIdentifier.name());
    JavaSymbol.TypeJavaSymbol constructorIdentifierSymbol = (JavaSymbol.TypeJavaSymbol) identifierType.symbol();
    constructorIdentifierSymbol.addUsage(constructorIdentifier);
    parameterTypes = addImplicitOuterClassParameter(parameterTypes, constructorIdentifierSymbol);
    Resolution resolution = resolveConstructorSymbol(constructorIdentifier, identifierType, newClassEnv, parameterTypes, typeArgumentsTypes);
    JavaType constructedType = identifierType;
    if (resolution.symbol().isMethodSymbol()) {
        constructedType = ((MethodJavaType) resolution.type()).resultType;
        if (constructedType.isTagged(JavaType.DEFERRED)) {
            Tree parent = newClassTreeImpl.parent();
            if (parent.is(Tree.Kind.MEMBER_SELECT)) {
                constructedType = resolve.parametrizedTypeWithErasure((ParametrizedTypeJavaType) identifierType);
            } else {
                // will be resolved by type inference
                ((DeferredType) constructedType).setTree(newClassTreeImpl);
            }
        } else if (identifierType.symbol().isInterface()) {
            // constructor of interface always resolve to 'Object' no-arg constructor
            registerType(typeTree, identifierType);
        } else {
            registerType(typeTree, resolution.type());
        }
    }
    ClassTree classBody = tree.classBody();
    if (classBody != null) {
        constructedType = getAnonymousClassType(identifierType, constructedType, classBody);
    }
    registerType(tree, constructedType);
}
Also used : NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) 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) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 98 with IdentifierTree

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

the class TypeAndReferenceSolver method inferReturnTypeFromInferedArgs.

private void inferReturnTypeFromInferedArgs(MethodInvocationTree tree, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes, JavaType returnType, TypeSubstitution typeSubstitution) {
    List<JavaType> parameterTypes = getParameterTypes(tree.arguments());
    if (!parameterTypes.equals(argTypes)) {
        IdentifierTree identifier = null;
        Resolution resolution = null;
        Tree methodSelect = tree.methodSelect();
        if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
            JavaType type = getType(mset.expression());
            if (type.isTagged(JavaType.DEFERRED)) {
                throw new IllegalStateException("type of arg should not be defered anymore ??");
            }
            identifier = mset.identifier();
            resolution = resolve.findMethod(methodEnv, type, identifier.name(), parameterTypes, typeParamTypes);
        } else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
            identifier = (IdentifierTree) methodSelect;
            resolution = resolve.findMethod(methodEnv, identifier.name(), parameterTypes, typeParamTypes);
        }
        if (resolution != null && returnType != resolution.type() && resolution.symbol().isMethodSymbol()) {
            MethodJavaType methodType = (MethodJavaType) resolution.type();
            if (!methodType.resultType.isTagged(JavaType.DEFERRED)) {
                registerType(tree, resolve.applySubstitution(methodType.resultType, typeSubstitution));
                // update type associated to identifier as it may have been inferred deeper when re-resolving method with new parameter types
                registerType(identifier, methodType);
            }
        }
    } else {
        registerType(tree, resolve.applySubstitution(returnType, typeSubstitution));
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 99 with IdentifierTree

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

the class DebugMethodYieldsCheck method checkEndOfExecution.

@Override
public void checkEndOfExecution(CheckerContext context) {
    MethodBehavior mb = ((CheckerDispatcher) context).methodBehavior();
    IdentifierTree methodName = methodNames.pop();
    if (mb != null) {
        reportIssue(methodName, String.format("Method '%s' has %d method yields.", methodName.name(), mb.yields().size()), flowFromYield(mb, methodName));
    }
}
Also used : CheckerDispatcher(org.sonar.java.se.CheckerDispatcher) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 100 with IdentifierTree

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

the class NonNullSetToNullCheck method callsThisConstructor.

private static boolean callsThisConstructor(MethodTree constructor) {
    List<StatementTree> body = constructor.block().body();
    if (body.isEmpty()) {
        return false;
    }
    StatementTree firstStatement = body.get(0);
    if (!firstStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        return false;
    }
    ExpressionTree expression = ((ExpressionStatementTree) firstStatement).expression();
    if (!expression.is(Tree.Kind.METHOD_INVOCATION)) {
        return false;
    }
    ExpressionTree methodSelect = ((MethodInvocationTree) expression).methodSelect();
    return methodSelect.is(Tree.Kind.IDENTIFIER) && "this".equals(((IdentifierTree) methodSelect).name());
}
Also used : ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Aggregations

IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)142 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)52 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)50 Symbol (org.sonar.plugins.java.api.semantic.Symbol)32 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)29 Tree (org.sonar.plugins.java.api.tree.Tree)27 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)20 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)15 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)10 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)9 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)8 ArrayList (java.util.ArrayList)7 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)7 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7