Search in sources :

Example 36 with GrMethodCall

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

the class ImportMavenRepositoriesTask method findMavenRemoteRepositories.

@NotNull
private Collection<? extends MavenRemoteRepository> findMavenRemoteRepositories(@Nullable GrClosableBlock repositoriesBlock) {
    Set<MavenRemoteRepository> myRemoteRepositories = ContainerUtil.newHashSet();
    for (GrMethodCall repo : PsiTreeUtil.getChildrenOfTypeAsList(repositoriesBlock, GrMethodCall.class)) {
        final String expressionText = repo.getInvokedExpression().getText();
        if ("mavenCentral".equals(expressionText)) {
            myRemoteRepositories.add(mavenCentralRemoteRepository);
        } else if ("mavenRepo".equals(expressionText)) {
            for (GrNamedArgument namedArgument : repo.getNamedArguments()) {
                if ("url".equals(namedArgument.getLabelName())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(namedArgument.getExpression());
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                    break;
                }
            }
        } else if ("maven".equals(expressionText) && repo.getClosureArguments().length > 0) {
            List<GrApplicationStatement> applicationStatementList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrApplicationStatement.class);
            if (!applicationStatementList.isEmpty()) {
                GrApplicationStatement statement = applicationStatementList.get(0);
                if (statement == null)
                    continue;
                GrExpression expression = statement.getInvokedExpression();
                if ("url".equals(expression.getText())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getExpressionArguments()[0]);
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                }
            }
            List<GrAssignmentExpression> assignmentExpressionList = PsiTreeUtil.getChildrenOfTypeAsList(repo.getClosureArguments()[0], GrAssignmentExpression.class);
            if (!assignmentExpressionList.isEmpty()) {
                GrAssignmentExpression statement = assignmentExpressionList.get(0);
                if (statement == null)
                    continue;
                GrExpression expression = statement.getLValue();
                if ("url".equals(expression.getText())) {
                    URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getRValue());
                    if (urlArgumentValue != null) {
                        String textUri = urlArgumentValue.toString();
                        myRemoteRepositories.add(new MavenRemoteRepository(textUri, null, textUri, null, null, null));
                    }
                }
            }
        }
    }
    return myRemoteRepositories;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) MavenRemoteRepository(org.jetbrains.idea.maven.model.MavenRemoteRepository) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) URI(java.net.URI) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with GrMethodCall

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

the class MissingReturnInspection method getExpectedClosureReturnType.

@Nullable
public static PsiType getExpectedClosureReturnType(GrClosableBlock closure) {
    List<PsiType> expectedReturnTypes = new ArrayList<>();
    PsiElement parent = closure.getParent();
    if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall || parent instanceof GrMethodCall) {
        GrMethodCall call = (GrMethodCall) (parent instanceof GrArgumentList ? parent.getParent() : parent);
        GroovyResolveResult[] variants = call.getCallVariants(null);
        for (GroovyResolveResult variant : variants) {
            Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(variant, closure, true, true, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
            if (map != null) {
                Pair<PsiParameter, PsiType> pair = map.get(closure);
                if (pair == null)
                    continue;
                PsiParameter parameter = pair.getFirst();
                PsiType type = parameter.getType();
                if (TypesUtil.isPsiClassTypeToClosure(type)) {
                    PsiType[] parameters = ((PsiClassType) type).getParameters();
                    if (parameters.length == 1) {
                        expectedReturnTypes.add(parameters[0]);
                    }
                }
            }
        }
    } else {
        for (PsiType expectedType : GroovyExpectedTypesProvider.getDefaultExpectedTypes(closure)) {
            if (TypesUtil.isPsiClassTypeToClosure(expectedType)) {
                PsiType[] parameters = ((PsiClassType) expectedType).getParameters();
                if (parameters.length == 1) {
                    expectedReturnTypes.add(parameters[0]);
                }
            }
        }
    }
    for (PsiType type : expectedReturnTypes) {
        if (PsiType.VOID.equals(type))
            return PsiType.VOID;
    }
    return TypesUtil.getLeastUpperBoundNullable(expectedReturnTypes, closure.getManager());
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) Pair(com.intellij.openapi.util.Pair) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with GrMethodCall

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

the class UnnecessaryQualifiedReferenceInspection method isQualifiedStaticMethodWithUnnecessaryQualifier.

private static boolean isQualifiedStaticMethodWithUnnecessaryQualifier(GrReferenceExpression ref) {
    if (ref.getQualifier() == null)
        return false;
    final PsiElement resolved = ref.resolve();
    if (!(resolved instanceof PsiMember))
        return false;
    if (!((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))
        return false;
    PsiElement copyResolved;
    final PsiElement parent = ref.getParent();
    if (parent instanceof GrMethodCall) {
        final GrMethodCall copy = (GrMethodCall) parent.copy();
        GrReferenceExpression invoked = (GrReferenceExpression) copy.getInvokedExpression();
        assert invoked != null;
        invoked.setQualifier(null);
        copyResolved = ((GrReferenceExpression) copy.getInvokedExpression()).resolve();
    } else {
        final GrReferenceExpression copy = (GrReferenceExpression) ref.copy();
        copy.setQualifier(null);
        copyResolved = copy.resolve();
    }
    return ref.getManager().areElementsEquivalent(copyResolved, resolved);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 39 with GrMethodCall

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

the class GebUtil method calculateContentElements.

private static Map<String, PsiField> calculateContentElements(@NotNull PsiClass pageOrModuleClass) {
    PsiField contentField = pageOrModuleClass.findFieldByName("content", false);
    if (!(contentField instanceof GrField))
        return Collections.emptyMap();
    GrExpression initializer = ((GrField) contentField).getInitializerGroovy();
    if (!(initializer instanceof GrClosableBlock))
        return Collections.emptyMap();
    Map<String, PsiField> res = new HashMap<>();
    PsiType objectType = PsiType.getJavaLangObject(pageOrModuleClass.getManager(), pageOrModuleClass.getResolveScope());
    for (PsiElement e = initializer.getFirstChild(); e != null; e = e.getNextSibling()) {
        if (e instanceof GrMethodCall) {
            GrMethodCall methodCall = (GrMethodCall) e;
            GrExpression invokedExpression = methodCall.getInvokedExpression();
            if (!(invokedExpression instanceof GrReferenceExpression))
                continue;
            if (((GrReferenceExpression) invokedExpression).isQualified())
                continue;
            GrExpression[] arguments = PsiUtil.getAllArguments((GrCall) e);
            if (arguments.length == 0)
                continue;
            final GrClosableBlock block;
            if (arguments.length == 1 && arguments[0] instanceof GrClosableBlock) {
                block = (GrClosableBlock) arguments[0];
            } else if (arguments.length == 2 && arguments[0] == null && arguments[1] instanceof GrClosableBlock) {
                block = (GrClosableBlock) arguments[1];
            } else {
                continue;
            }
            GrLightField field = new GrLightField(pageOrModuleClass, ((GrReferenceExpression) invokedExpression).getReferenceName(), objectType, invokedExpression) {

                @Override
                public PsiType getTypeGroovy() {
                    return block.getReturnType();
                }

                @Override
                public PsiType getDeclaredType() {
                    return null;
                }
            };
            field.getModifierList().addModifier(GrModifierFlags.STATIC_MASK);
            res.put(field.getName(), field);
        }
    }
    return res;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) HashMap(java.util.HashMap) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrLightField(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightField)

Example 40 with GrMethodCall

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

the class ConfigSlurperMapContentProvider method getInfo.

@Nullable
private static Pair<ConfigSlurperSupport.PropertiesProvider, List<String>> getInfo(@NotNull GrExpression qualifier, @Nullable PsiElement resolve) {
    if (!InheritanceUtil.isInheritor(qualifier.getType(), GroovyCommonClassNames.GROOVY_UTIL_CONFIG_OBJECT)) {
        return null;
    }
    GrExpression resolvedQualifier = qualifier;
    PsiElement resolveResult = resolve;
    List<String> path = new ArrayList<>();
    while (resolveResult == null) {
        if (!(resolvedQualifier instanceof GrReferenceExpression))
            return null;
        GrReferenceExpression expr = (GrReferenceExpression) resolvedQualifier;
        path.add(expr.getReferenceName());
        resolvedQualifier = expr.getQualifierExpression();
        if (resolvedQualifier instanceof GrReferenceExpression) {
            resolveResult = ((GrReferenceExpression) resolvedQualifier).resolve();
        } else if (resolvedQualifier instanceof GrMethodCall) {
            resolveResult = ((GrMethodCall) resolvedQualifier).resolveMethod();
        } else {
            return null;
        }
    }
    Collections.reverse(path);
    ConfigSlurperSupport.PropertiesProvider propertiesProvider = null;
    for (ConfigSlurperSupport slurperSupport : ConfigSlurperSupport.EP_NAME.getExtensions()) {
        propertiesProvider = slurperSupport.getConfigSlurperInfo(resolvedQualifier, resolveResult);
        if (propertiesProvider != null)
            break;
    }
    if (propertiesProvider == null)
        return null;
    return Pair.create(propertiesProvider, path);
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)51 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)23 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)22 Nullable (org.jetbrains.annotations.Nullable)18 PsiElement (com.intellij.psi.PsiElement)17 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)9 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 NotNull (org.jetbrains.annotations.NotNull)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiFile (com.intellij.psi.PsiFile)3 PsiMethod (com.intellij.psi.PsiMethod)3 PsiType (com.intellij.psi.PsiType)3 ArrayList (java.util.ArrayList)3 GroovyMapContentProvider (org.jetbrains.plugins.groovy.extensions.GroovyMapContentProvider)3 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)3 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)3