Search in sources :

Example 51 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GroovyTypeCheckVisitor method checkMethodApplicability.

private <T extends GroovyPsiElement> boolean checkMethodApplicability(@NotNull final GroovyResolveResult methodResolveResult, boolean checkUnknownArgs, @NotNull final CallInfo<T> info) {
    final PsiElement element = methodResolveResult.getElement();
    if (!(element instanceof PsiMethod))
        return true;
    if (element instanceof GrBuilderMethod)
        return true;
    final PsiMethod method = (PsiMethod) element;
    if ("call".equals(method.getName()) && info.getInvokedExpression() instanceof GrReferenceExpression) {
        final GrExpression qualifierExpression = ((GrReferenceExpression) info.getInvokedExpression()).getQualifierExpression();
        if (qualifierExpression != null) {
            final PsiType type = qualifierExpression.getType();
            if (type instanceof GrClosureType) {
                GrClosureSignatureUtil.ApplicabilityResult result = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), (GrClosureType) type, info.getInvokedExpression());
                switch(result) {
                    case inapplicable:
                        highlightInapplicableMethodUsage(methodResolveResult, info, method);
                        return false;
                    case //q(1,2)
                    canBeApplicable:
                        if (checkUnknownArgs) {
                            highlightUnknownArgs(info);
                        }
                        return !checkUnknownArgs;
                    default:
                        return true;
                }
            }
        }
    }
    if (method instanceof GrGdkMethod && info.getInvokedExpression() instanceof GrReferenceExpression) {
        final GrReferenceExpression invoked = (GrReferenceExpression) info.getInvokedExpression();
        final GrExpression qualifier = PsiImplUtil.getRuntimeQualifier(invoked);
        if (qualifier == null && method.getName().equals("call")) {
            GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(invoked.getProject());
            final GrReferenceExpression callRef = factory.createReferenceExpressionFromText("qualifier.call", invoked);
            callRef.setQualifier(invoked);
            return checkMethodApplicability(methodResolveResult, checkUnknownArgs, new DelegatingCallInfo<T>(info) {

                @Nullable
                @Override
                public GrExpression getInvokedExpression() {
                    return callRef;
                }

                @NotNull
                @Override
                public GroovyResolveResult advancedResolve() {
                    return methodResolveResult;
                }

                @NotNull
                @Override
                public GroovyResolveResult[] multiResolve() {
                    return new GroovyResolveResult[] { methodResolveResult };
                }

                @Nullable
                @Override
                public PsiType getQualifierInstanceType() {
                    return info.getInvokedExpression().getType();
                }
            });
        }
        final PsiMethod staticMethod = ((GrGdkMethod) method).getStaticMethod();
        PsiType qualifierType = info.getQualifierInstanceType();
        if (method.hasModifierProperty(PsiModifier.STATIC)) {
            qualifierType = ResolveUtil.unwrapClassType(qualifierType);
        }
        //check methods processed by @Category(ClassWhichProcessMethod) annotation
        if (qualifierType != null && !GdkMethodUtil.isCategoryMethod(staticMethod, qualifierType, qualifier, methodResolveResult.getSubstitutor()) && !checkCategoryQualifier(invoked, qualifier, staticMethod, methodResolveResult.getSubstitutor())) {
            registerError(info.getHighlightElementForCategoryQualifier(), ProblemHighlightType.GENERIC_ERROR, GroovyInspectionBundle.message("category.method.0.cannot.be.applied.to.1", method.getName(), qualifierType.getCanonicalText()));
            return false;
        }
    }
    if (info.getArgumentTypes() == null)
        return true;
    GrClosureSignatureUtil.ApplicabilityResult applicable = PsiUtil.isApplicableConcrete(info.getArgumentTypes(), method, methodResolveResult.getSubstitutor(), info.getCall(), false);
    switch(applicable) {
        case inapplicable:
            highlightInapplicableMethodUsage(methodResolveResult, info, method);
            return false;
        case canBeApplicable:
            if (checkUnknownArgs) {
                highlightUnknownArgs(info);
            }
            return !checkUnknownArgs;
        default:
            return true;
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrBuilderMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrBuilderMethod) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable) GrClosureType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClosureType)

Example 52 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrCastFix method doCast.

static void doCast(@NotNull Project project, @NotNull PsiType type, @NotNull GrExpression expr) {
    if (!type.isValid())
        return;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final GrSafeCastExpression cast = (GrSafeCastExpression) factory.createExpressionFromText("foo as String");
    final GrTypeElement typeElement = factory.createTypeElement(type);
    cast.getOperand().replaceWithExpression(expr, true);
    cast.getCastTypeElement().replace(typeElement);
    final GrExpression replaced = expr.replaceWithExpression(cast, true);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(replaced);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

Example 53 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.

private static boolean canBeSimplified(PsiElement element) {
    if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
        return false;
    if (element instanceof GrCodeReferenceElement) {
        if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
            return false;
    } else if (element instanceof GrReferenceExpression) {
        if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
            return false;
    } else {
        return false;
    }
    final GrReferenceElement ref = (GrReferenceElement) element;
    if (ref.getQualifier() == null)
        return false;
    if (!(ref.getContainingFile() instanceof GroovyFileBase))
        return false;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiClass))
        return false;
    final String name = ((PsiClass) resolved).getName();
    if (name == null)
        return false;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
    final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
    final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
    if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
        return true;
    }
    final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
    if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
        return false;
    }
    return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 54 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GroovyCodeFragment method createSingleImport.

@Nullable
private GrImportStatement createSingleImport(@NotNull String qname, @Nullable String importedName) {
    final PsiClass aClass = JavaPsiFacade.getInstance(getProject()).findClass(qname, getResolveScope());
    final boolean isStatic = aClass == null;
    final String className = PsiNameHelper.getShortClassName(qname);
    final String alias = importedName == null || className.equals(importedName) ? null : importedName;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
    try {
        return factory.createImportStatement(qname, isStatic, false, alias, this);
    } catch (IncorrectOperationException e) {
        return null;
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 55 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class JavaStylePropertiesUtil method genRefForGetter.

private static GrExpression genRefForGetter(GrMethodCall call, String accessorName) {
    String name = GroovyPropertyUtils.getPropertyNameByGetterName(accessorName, true);
    GrReferenceExpression refExpr = (GrReferenceExpression) call.getInvokedExpression();
    String oldNameStr = refExpr.getReferenceNameElement().getText();
    String newRefExpr = StringUtil.trimEnd(refExpr.getText(), oldNameStr) + name;
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(call.getProject());
    return factory.createExpressionFromText(newRefExpr, call);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6