Search in sources :

Example 6 with Resolution

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

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

the class TypeAndReferenceSolver method resolveIdentifierType.

private JavaType resolveIdentifierType(Resolve.Env newClassEnv, @Nullable ExpressionTree enclosingExpression, TypeTree typeTree, String typeName) {
    if (enclosingExpression != null) {
        resolveAs(enclosingExpression, JavaSymbol.VAR);
        Resolution idType = resolve.findIdentInType(newClassEnv, (JavaSymbol.TypeJavaSymbol) enclosingExpression.symbolType().symbol(), typeName, JavaSymbol.TYP);
        JavaType type = idType.type();
        if (typeTree.is(Tree.Kind.PARAMETERIZED_TYPE)) {
            TypeArguments typeArguments = ((ParameterizedTypeTree) typeTree).typeArguments();
            scan(typeArguments);
            type = parametrizedTypeWithTypeArguments(type.symbol, typeArguments);
        }
        registerType(typeTree, type);
    } else {
        resolveAs(typeTree, JavaSymbol.TYP, newClassEnv, false);
    }
    return (JavaType) typeTree.symbolType();
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeArguments(org.sonar.plugins.java.api.tree.TypeArguments) Resolution(org.sonar.java.resolve.Resolve.Resolution)

Example 8 with Resolution

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

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