Search in sources :

Example 1 with MethodInvocationTreeImpl

use of org.sonar.java.model.expression.MethodInvocationTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method applySelectors.

private static ExpressionTree applySelectors(ExpressionTree primary, Optional<List<Tuple<Optional<InternalSyntaxToken>, ExpressionTree>>> selectors) {
    ExpressionTree result = primary;
    if (selectors.isPresent()) {
        for (Tuple<Optional<InternalSyntaxToken>, ExpressionTree> tuple : selectors.get()) {
            Optional<InternalSyntaxToken> dotTokenOptional = tuple.first();
            ExpressionTree selector = tuple.second();
            if (dotTokenOptional.isPresent()) {
                InternalSyntaxToken dotToken = dotTokenOptional.get();
                if (selector.is(Kind.IDENTIFIER)) {
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) selector;
                    result = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                } else {
                    MethodInvocationTreeImpl methodInvocation = (MethodInvocationTreeImpl) selector;
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) methodInvocation.methodSelect();
                    MemberSelectExpressionTreeImpl memberSelect = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                    result = new MethodInvocationTreeImpl(memberSelect, methodInvocation.typeArguments(), (ArgumentListTreeImpl) methodInvocation.arguments());
                }
            } else if (selector.is(Kind.NEW_CLASS)) {
                NewClassTreeImpl newClass = (NewClassTreeImpl) selector;
                result = newClass.completeWithEnclosingExpression(result);
            } else if (selector.is(Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTreeImpl arrayAccess = (ArrayAccessExpressionTreeImpl) selector;
                result = arrayAccess.complete(result);
            } else if (selector.is(Kind.MEMBER_SELECT)) {
                MemberSelectExpressionTreeImpl memberSelect = (MemberSelectExpressionTreeImpl) selector;
                result = memberSelect.completeWithExpression(result);
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return result;
}
Also used : Optional(com.sonar.sslr.api.typed.Optional) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) ArrayAccessExpressionTreeImpl(org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl) TypeArgumentListTreeImpl(org.sonar.java.model.expression.TypeArgumentListTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl)

Example 2 with MethodInvocationTreeImpl

use of org.sonar.java.model.expression.MethodInvocationTreeImpl in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method visitMethodInvocation.

@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    MethodInvocationTreeImpl mit = (MethodInvocationTreeImpl) tree;
    Resolve.Env methodEnv = semanticModel.getEnv(tree);
    if (mit.isTypeSet() && mit.symbol().isMethodSymbol()) {
        TypeSubstitution typeSubstitution = inferedSubstitution(mit);
        List<JavaType> argTypes = getParameterTypes(tree.arguments());
        JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) mit.symbol();
        List<JavaType> formals = methodSymbol.parameterTypes().stream().map(t -> (JavaType) t).collect(Collectors.toList());
        List<JavaType> inferedArgTypes = resolve.resolveTypeSubstitution(formals, typeSubstitution);
        int size = inferedArgTypes.size();
        IntStream.range(0, argTypes.size()).forEach(i -> {
            JavaType arg = argTypes.get(i);
            Type formal = inferedArgTypes.get(Math.min(i, size - 1));
            if (formal != arg) {
                AbstractTypedTree argTree = (AbstractTypedTree) mit.arguments().get(i);
                argTree.setInferedType(formal);
                argTree.accept(this);
            }
        });
        List<JavaType> typeParamTypes = getParameterTypes(tree.typeArguments());
        JavaType resultType = ((MethodJavaType) mit.symbol().type()).resultType;
        // if result type is a type var defined by the method we are solving, use the target type.
        if (resultType.symbol.owner == mit.symbol()) {
            resultType = (JavaType) mit.symbolType();
        }
        inferReturnTypeFromInferedArgs(tree, methodEnv, argTypes, typeParamTypes, resultType, typeSubstitution);
        return;
    }
    scan(tree.arguments());
    scan(tree.typeArguments());
    List<JavaType> argTypes = getParameterTypes(tree.arguments());
    List<JavaType> typeParamTypes = getParameterTypes(tree.typeArguments());
    Resolve.Resolution resolution = resolveMethodSymbol(tree.methodSelect(), methodEnv, argTypes, typeParamTypes);
    JavaSymbol symbol;
    JavaType returnType;
    if (resolution == null) {
        returnType = symbols.deferedType(mit);
        symbol = Symbols.unknownSymbol;
    } else {
        symbol = resolution.symbol();
        returnType = resolution.type();
        if (symbol.isMethodSymbol()) {
            MethodJavaType methodType = (MethodJavaType) resolution.type();
            returnType = methodType.resultType;
        }
    }
    mit.setSymbol(symbol);
    if (returnType != null && returnType.isTagged(JavaType.DEFERRED)) {
        ((DeferredType) returnType).setTree(mit);
    }
    registerType(tree, returnType);
    if (resolution != null) {
        inferArgumentTypes(argTypes, resolution);
        inferReturnTypeFromInferedArgs(tree, methodEnv, argTypes, typeParamTypes, returnType, new TypeSubstitution());
    }
}
Also used : AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Resolution(org.sonar.java.resolve.Resolve.Resolution) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Map(java.util.Map) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ImmutableSet(com.google.common.collect.ImmutableSet) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) Collection(java.util.Collection) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) JavaKeyword(org.sonar.java.ast.api.JavaKeyword) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) Collectors(java.util.stream.Collectors) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) List(java.util.List) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) Optional(java.util.Optional) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) IntStream(java.util.stream.IntStream) ParenthesizedTreeImpl(org.sonar.java.model.expression.ParenthesizedTreeImpl) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) 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) ArrayList(java.util.ArrayList) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ImmutableList(com.google.common.collect.ImmutableList) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ConditionalExpressionTreeImpl(org.sonar.java.model.expression.ConditionalExpressionTreeImpl) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) LambdaExpressionTreeImpl(org.sonar.java.model.expression.LambdaExpressionTreeImpl) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Nullable(javax.annotation.Nullable) 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) MethodReferenceTreeImpl(org.sonar.java.model.expression.MethodReferenceTreeImpl) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) Maps(com.google.common.collect.Maps) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TypeArgumentListTreeImpl(org.sonar.java.model.expression.TypeArgumentListTreeImpl) CheckForNull(javax.annotation.CheckForNull) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) Type(org.sonar.plugins.java.api.semantic.Type)

Example 3 with MethodInvocationTreeImpl

use of org.sonar.java.model.expression.MethodInvocationTreeImpl in project sonar-java by SonarSource.

the class TreeFactory method newIdentifierOrMethodInvocation.

public ExpressionTree newIdentifierOrMethodInvocation(Optional<TypeArgumentListTreeImpl> typeArguments, InternalSyntaxToken identifierToken, Optional<ArgumentListTreeImpl> arguments) {
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    ExpressionTree result = identifier;
    if (arguments.isPresent()) {
        result = new MethodInvocationTreeImpl(identifier, typeArguments.orNull(), arguments.get());
    }
    return result;
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl)

Aggregations

IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)3 MethodInvocationTreeImpl (org.sonar.java.model.expression.MethodInvocationTreeImpl)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2 TypeArgumentListTreeImpl (org.sonar.java.model.expression.TypeArgumentListTreeImpl)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Maps (com.google.common.collect.Maps)1 Optional (com.sonar.sslr.api.typed.Optional)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 JavaKeyword (org.sonar.java.ast.api.JavaKeyword)1