Search in sources :

Example 31 with GrReferenceExpression

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

the class TypeProvider method inferMethodParameters.

@NotNull
private PsiType[] inferMethodParameters(@NotNull GrMethod method) {
    PsiType[] psiTypes = inferredTypes.get(method);
    if (psiTypes != null)
        return psiTypes;
    final GrParameter[] parameters = method.getParameters();
    final TIntArrayList paramInds = new TIntArrayList(parameters.length);
    final PsiType[] types = PsiType.createArray(parameters.length);
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i].getTypeElementGroovy() == null) {
            paramInds.add(i);
        } else {
            types[i] = parameters[i].getType();
        }
    }
    if (!paramInds.isEmpty()) {
        final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, PsiSubstitutor.EMPTY);
        MethodReferencesSearch.search(method, true).forEach(psiReference -> {
            final PsiElement element = psiReference.getElement();
            final PsiManager manager = element.getManager();
            final GlobalSearchScope resolveScope = element.getResolveScope();
            if (element instanceof GrReferenceExpression) {
                final GrCall call = (GrCall) element.getParent();
                final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
                if (argInfos == null)
                    return true;
                paramInds.forEach(new TIntProcedure() {

                    @Override
                    public boolean execute(int i) {
                        PsiType type = GrClosureSignatureUtil.getTypeByArg(argInfos[i], manager, resolveScope);
                        types[i] = TypesUtil.getLeastUpperBoundNullable(type, types[i], manager);
                        return true;
                    }
                });
            }
            return true;
        });
    }
    paramInds.forEach(new TIntProcedure() {

        @Override
        public boolean execute(int i) {
            if (types[i] == null || types[i] == PsiType.NULL) {
                types[i] = parameters[i].getType();
            }
            return true;
        }
    });
    inferredTypes.put(method, types);
    return types;
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) TIntArrayList(gnu.trove.TIntArrayList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with GrReferenceExpression

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

the class GroovyReferenceCopyPasteProcessor method findReferencesToRestore.

@NotNull
@Override
protected GrReferenceElement[] findReferencesToRestore(PsiFile file, RangeMarker bounds, ReferenceData[] referenceData) {
    PsiManager manager = file.getManager();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(manager.getProject());
    PsiResolveHelper helper = facade.getResolveHelper();
    GrReferenceElement[] refs = new GrReferenceElement[referenceData.length];
    for (int i = 0; i < referenceData.length; i++) {
        ReferenceData data = referenceData[i];
        PsiClass refClass = facade.findClass(data.qClassName, file.getResolveScope());
        if (refClass == null)
            continue;
        int startOffset = data.startOffset + bounds.getStartOffset();
        int endOffset = data.endOffset + bounds.getStartOffset();
        PsiElement element = file.findElementAt(startOffset);
        if (element != null && element.getParent() instanceof GrReferenceElement && !PsiUtil.isThisOrSuperRef(element.getParent())) {
            GrReferenceElement reference = (GrReferenceElement) element.getParent();
            TextRange range = reference.getTextRange();
            if (range.getStartOffset() == startOffset && range.getEndOffset() == endOffset) {
                if (data.staticMemberName == null) {
                    PsiClass refClass1 = helper.resolveReferencedClass(reference.getText(), reference);
                    if (refClass1 == null || !manager.areElementsEquivalent(refClass, refClass1)) {
                        refs[i] = reference;
                    }
                } else {
                    if (reference instanceof GrReferenceExpression) {
                        PsiElement referent = resolveReferenceIgnoreOverriding(reference);
                        if (!(referent instanceof PsiNamedElement) || !data.staticMemberName.equals(((PsiNamedElement) referent).getName()) || !(referent instanceof PsiMember) || ((PsiMember) referent).getContainingClass() == null || !data.qClassName.equals(((PsiMember) referent).getContainingClass().getQualifiedName())) {
                            refs[i] = reference;
                        }
                    }
                }
            }
        }
    }
    return refs;
}
Also used : TextRange(com.intellij.openapi.util.TextRange) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) ReferenceData(com.intellij.codeInsight.editorActions.ReferenceData) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with GrReferenceExpression

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

the class GroovyStaticImportMethodFix method isAvailable.

@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
    myCandidates = null;
    if (!file.getManager().isInProject(file))
        return false;
    GrReferenceExpression invokedExpression = getMethodExpression();
    if (invokedExpression == null || invokedExpression.getQualifierExpression() != null)
        return false;
    return !getCandidates().isEmpty();
}
Also used : GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 34 with GrReferenceExpression

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

the class GroovyStaticImportMethodFix method getMethodsToImport.

@NotNull
private List<PsiMethod> getMethodsToImport() {
    PsiShortNamesCache cache = PsiShortNamesCache.getInstance(myMethodCall.getProject());
    GrMethodCall element = myMethodCall.getElement();
    LOG.assertTrue(element != null);
    GrReferenceExpression reference = getMethodExpression(element);
    LOG.assertTrue(reference != null);
    GrArgumentList argumentList = element.getArgumentList();
    String name = reference.getReferenceName();
    ArrayList<PsiMethod> list = new ArrayList<>();
    if (name == null)
        return list;
    GlobalSearchScope scope = element.getResolveScope();
    PsiMethod[] methods = cache.getMethodsByNameIfNotMoreThan(name, scope, 20);
    List<PsiMethod> applicableList = new ArrayList<>();
    for (PsiMethod method : methods) {
        ProgressManager.checkCanceled();
        if (JavaCompletionUtil.isInExcludedPackage(method, false))
            continue;
        if (!method.hasModifierProperty(PsiModifier.STATIC))
            continue;
        PsiFile file = method.getContainingFile();
        if (file instanceof PsiClassOwner && //do not show methods from default package
        !((PsiClassOwner) file).getPackageName().isEmpty() && PsiUtil.isAccessible(element, method)) {
            list.add(method);
            if (PsiUtil.isApplicable(PsiUtil.getArgumentTypes(element, true), method, PsiSubstitutor.EMPTY, element, false)) {
                applicableList.add(method);
            }
        }
    }
    List<PsiMethod> result = applicableList.isEmpty() ? list : applicableList;
    Collections.sort(result, new PsiProximityComparator(argumentList));
    return result;
}
Also used : GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) ArrayList(java.util.ArrayList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiShortNamesCache(com.intellij.psi.search.PsiShortNamesCache) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiProximityComparator(com.intellij.psi.util.proximity.PsiProximityComparator) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with GrReferenceExpression

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

the class GrVariableJoinLinesHandler method tryJoinStatements.

@Override
public int tryJoinStatements(@NotNull GrStatement first, @NotNull GrStatement second) {
    if (first instanceof GrVariableDeclaration && !((GrVariableDeclaration) first).isTuple() && second instanceof GrAssignmentExpression) {
        final GrExpression lvalue = ((GrAssignmentExpression) second).getLValue();
        final GrExpression rValue = ((GrAssignmentExpression) second).getRValue();
        if (lvalue instanceof GrReferenceExpression && rValue != null) {
            final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
            if (ArrayUtil.contains(resolved, ((GrVariableDeclaration) first).getVariables())) {
                assert resolved instanceof GrVariable;
                if (((GrVariable) resolved).getInitializerGroovy() == null) {
                    ((GrVariable) resolved).setInitializerGroovy(rValue);
                    second.delete();
                    GrExpression newInitializer = ((GrVariable) resolved).getInitializerGroovy();
                    assert newInitializer != null;
                    return newInitializer.getTextRange().getEndOffset();
                }
            }
        }
    }
    return CANNOT_JOIN;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) 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)

Aggregations

GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)177 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)92 PsiElement (com.intellij.psi.PsiElement)56 Nullable (org.jetbrains.annotations.Nullable)28 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)27 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)26 NotNull (org.jetbrains.annotations.NotNull)25 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)22 GrMethodCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall)22 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)17 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)16 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)15 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)14 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)14 PsiType (com.intellij.psi.PsiType)13 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)12 Project (com.intellij.openapi.project.Project)11 ArrayList (java.util.ArrayList)11 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)11