Search in sources :

Example 1 with Resolution

use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method resolveConstructorSymbol.

private Resolve.Resolution resolveConstructorSymbol(IdentifierTree identifier, Type type, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeArgumentsTypes) {
    Resolve.Resolution resolution = resolve.findMethod(methodEnv, (JavaType) type, "<init>", argTypes, typeArgumentsTypes);
    JavaSymbol symbol = resolution.symbol();
    inferArgumentTypes(argTypes, resolution);
    associateReference(identifier, symbol);
    return resolution;
}
Also used : Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 2 with Resolution

use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method resolveAs.

public JavaSymbol resolveAs(Tree tree, int kind, Resolve.Env resolveEnv, boolean associateReference) {
    if (tree.is(Tree.Kind.IDENTIFIER, Tree.Kind.MEMBER_SELECT)) {
        JavaSymbol resolvedSymbol;
        IdentifierTree identifierTree;
        if (tree.is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) tree;
            if (JavaKeyword.CLASS.getValue().equals(mse.identifier().name())) {
                // resolve type of expression xxx.class
                return resolveClassType(tree, resolveEnv, mse);
            }
            identifierTree = mse.identifier();
            List<AnnotationTree> identifierAnnotations = identifierTree.annotations();
            scan(identifierAnnotations);
            completeMetadata((JavaSymbol) identifierTree.symbol(), identifierAnnotations);
            Resolve.Resolution res = getSymbolOfMemberSelectExpression(mse, kind, resolveEnv);
            resolvedSymbol = res.symbol();
            JavaType resolvedType = resolve.resolveTypeSubstitution(res.type(), getType(mse.expression()));
            registerType(identifierTree, resolvedType);
            registerType(tree, resolvedType);
        } else {
            identifierTree = (IdentifierTree) tree;
            Resolve.Resolution resolution = resolve.findIdent(resolveEnv, identifierTree.name(), kind);
            resolvedSymbol = resolution.symbol();
            registerType(tree, resolution.type());
        }
        if (associateReference) {
            associateReference(identifierTree, resolvedSymbol);
        }
        return resolvedSymbol;
    }
    tree.accept(this);
    JavaType type = getType(tree);
    if (tree.is(Tree.Kind.INFERED_TYPE)) {
        type = symbols.deferedType((AbstractTypedTree) tree);
        registerType(tree, type);
    }
    if (type == null) {
        throw new IllegalStateException("Type not resolved " + tree);
    }
    return type.symbol;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree)

Example 3 with Resolution

use of org.sonar.java.resolve.Resolve.Resolution in project sonar-java by SonarSource.

the class TypeAndReferenceSolver method resolveMethodSymbol.

@CheckForNull
private Resolve.Resolution resolveMethodSymbol(Tree methodSelect, Resolve.Env methodEnv, List<JavaType> argTypes, List<JavaType> typeParamTypes) {
    Resolve.Resolution resolution;
    IdentifierTree identifier;
    if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
        MemberSelectExpressionTree mset = (MemberSelectExpressionTree) methodSelect;
        resolveAs(mset.expression(), JavaSymbol.TYP | JavaSymbol.VAR);
        JavaType type = getType(mset.expression());
        if (type.isTagged(JavaType.DEFERRED)) {
            return null;
        }
        identifier = mset.identifier();
        resolution = resolve.findMethod(methodEnv, type, identifier.name(), argTypes, typeParamTypes);
    } else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
        identifier = (IdentifierTree) methodSelect;
        resolution = resolve.findMethod(methodEnv, identifier.name(), argTypes, typeParamTypes);
    } else {
        throw new IllegalStateException("Method select in method invocation is not of the expected type " + methodSelect);
    }
    registerType(identifier, resolution.type());
    associateReference(identifier, resolution.symbol());
    return resolution;
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Resolution(org.sonar.java.resolve.Resolve.Resolution) CheckForNull(javax.annotation.CheckForNull)

Example 4 with Resolution

use of org.sonar.java.resolve.Resolve.Resolution 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 5 with Resolution

use of org.sonar.java.resolve.Resolve.Resolution 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)

Aggregations

Resolution (org.sonar.java.resolve.Resolve.Resolution)8 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)5 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)4 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)4 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)4 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)3 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)3 ArrayTypeTree (org.sonar.plugins.java.api.tree.ArrayTypeTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 BreakStatementTree (org.sonar.plugins.java.api.tree.BreakStatementTree)3 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)3 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)3 ContinueStatementTree (org.sonar.plugins.java.api.tree.ContinueStatementTree)3 EnumConstantTree (org.sonar.plugins.java.api.tree.EnumConstantTree)3 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 ImportTree (org.sonar.plugins.java.api.tree.ImportTree)3 InstanceOfTree (org.sonar.plugins.java.api.tree.InstanceOfTree)3