Search in sources :

Example 81 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method visitParameterList.

@Override
public void visitParameterList(@NotNull final GrParameterList parameterList) {
    super.visitParameterList(parameterList);
    PsiElement parent = parameterList.getParent();
    if (!(parent instanceof GrClosableBlock))
        return;
    GrParameter[] parameters = parameterList.getParameters();
    if (parameters.length > 0) {
        List<PsiType[]> signatures = ClosureParamsEnhancer.findFittingSignatures((GrClosableBlock) parent);
        final List<PsiType> paramTypes = ContainerUtil.map(parameters, parameter -> parameter.getType());
        if (signatures.size() > 1) {
            final PsiType[] fittingSignature = ContainerUtil.find(signatures, types -> {
                for (int i = 0; i < types.length; i++) {
                    if (!TypesUtil.isAssignableByMethodCallConversion(paramTypes.get(i), types[i], parameterList)) {
                        return false;
                    }
                }
                return true;
            });
            if (fittingSignature == null) {
                registerError(parameterList, GroovyInspectionBundle.message("no.applicable.signature.found"), null, ProblemHighlightType.GENERIC_ERROR);
            }
        } else if (signatures.size() == 1) {
            PsiType[] types = signatures.get(0);
            for (int i = 0; i < types.length; i++) {
                GrTypeElement typeElement = parameters[i].getTypeElementGroovy();
                if (typeElement == null)
                    continue;
                PsiType expected = types[i];
                PsiType actual = paramTypes.get(i);
                if (!TypesUtil.isAssignableByMethodCallConversion(actual, expected, parameterList)) {
                    registerError(typeElement, GroovyInspectionBundle.message("expected.type.0", expected.getCanonicalText(false), actual.getCanonicalText(false)), null, ProblemHighlightType.GENERIC_ERROR);
                }
            }
        }
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 82 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GroovyRefactoringUtil method verifySafeCopyExpressionSubElement.

private static int verifySafeCopyExpressionSubElement(PsiElement element) {
    int result = RefactoringUtil.EXPR_COPY_SAFE;
    if (element == null)
        return result;
    if (element instanceof GrNamedElement) {
        return RefactoringUtil.EXPR_COPY_SAFE;
    }
    if (element instanceof GrMethodCallExpression) {
        result = RefactoringUtil.EXPR_COPY_UNSAFE;
    }
    if (element instanceof GrNewExpression) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (element instanceof GrAssignmentExpression) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (element instanceof GrClosableBlock) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    if (isPlusPlusOrMinusMinus(element)) {
        return RefactoringUtil.EXPR_COPY_PROHIBITED;
    }
    PsiElement[] children = element.getChildren();
    for (PsiElement child : children) {
        int childResult = verifySafeCopyExpressionSubElement(child);
        result = Math.max(result, childResult);
    }
    return result;
}
Also used : GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 83 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrClosureDelegateTypeCalculator method calculateType.

@Nullable
protected PsiType calculateType(@NotNull GrExpression expression, @NotNull PsiMethod method) {
    if (!"getDelegate".equals(method.getName()) || method.getParameterList().getParametersCount() != 0)
        return null;
    final GrClosableBlock closure = PsiTreeUtil.getParentOfType(expression, GrClosableBlock.class);
    if (closure == null)
        return null;
    final PsiClass closureClass = JavaPsiFacade.getInstance(expression.getProject()).findClass(GroovyCommonClassNames.GROOVY_LANG_CLOSURE, expression.getResolveScope());
    if (closureClass == null || !closureClass.equals(method.getContainingClass()))
        return null;
    final DelegatesToInfo info = GrDelegatesToUtilKt.getDelegatesToInfo(closure);
    if (info == null)
        return null;
    return info.getTypeToDelegate();
}
Also used : GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) DelegatesToInfo(org.jetbrains.plugins.groovy.lang.resolve.delegatesTo.DelegatesToInfo) Nullable(org.jetbrains.annotations.Nullable)

Example 84 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GrMethodCallExpressionImpl method replaceClosureArgument.

@Override
public GrExpression replaceClosureArgument(@NotNull GrClosableBlock closure, @NotNull GrExpression newExpr) throws IncorrectOperationException {
    if (newExpr instanceof GrClosableBlock) {
        return closure.replaceWithExpression(newExpr, true);
    }
    final GrClosableBlock[] closureArguments = getClosureArguments();
    final int i = ArrayUtil.find(closureArguments, closure);
    GrArgumentList argList = getArgumentList();
    if (argList.getText().isEmpty()) {
        argList = (GrArgumentList) argList.replace(GroovyPsiElementFactory.getInstance(getProject()).createArgumentList());
    }
    for (int j = 0; j < i; j++) {
        argList.add(closureArguments[j]);
        closureArguments[j].delete();
    }
    final GrExpression result = (GrExpression) argList.add(newExpr);
    closure.delete();
    return result;
}
Also used : GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 85 with GrClosableBlock

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock in project intellij-community by JetBrains.

the class GriffonPropertyListenerAnnotationChecker method checkArgumentList.

@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
    if (!"griffon.transform.PropertyListener".equals(annotation.getQualifiedName()))
        return false;
    final GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
    if (attributes.length != 1)
        return false;
    final GrAnnotationNameValuePair attribute = attributes[0];
    final GrAnnotationMemberValue value = attribute.getValue();
    final PsiAnnotationOwner owner = annotation.getOwner();
    if (owner instanceof GrField) {
        if (value instanceof GrClosableBlock) {
            return true;
        }
    } else if (owner instanceof GrTypeDefinition) {
        if (value instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) value).resolve();
            if (resolved instanceof GrField) {
                final PsiClass containingClass = ((GrField) resolved).getContainingClass();
                if (annotation.getManager().areElementsEquivalent((PsiElement) owner, containingClass)) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) PsiAnnotationOwner(com.intellij.psi.PsiAnnotationOwner) PsiClass(com.intellij.psi.PsiClass) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)116 PsiElement (com.intellij.psi.PsiElement)32 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)31 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)26 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)26 Nullable (org.jetbrains.annotations.Nullable)23 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)23 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)17 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)15 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)15 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)14 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)13 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 ArrayList (java.util.ArrayList)11 NotNull (org.jetbrains.annotations.NotNull)10 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)10 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)10 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)10 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9