Search in sources :

Example 21 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.

private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
    final PsiElement namedElem = getReferencedElement(owner);
    if (namedElem == null)
        return true;
    final Ref<Boolean> result = new Ref<>(true);
    final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {

        @Override
        public void run(@NotNull final ProgressIndicator indicator) {
            final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
            final Processor<PsiReference> consumer = psiReference -> {
                references.add(psiReference);
                return true;
            };
            ReferencesSearch.search(namedElem).forEach(consumer);
            boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

                @Override
                public Boolean compute() {
                    return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
                }
            });
            if (isProperty) {
                final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {

                    @Override
                    public GrAccessorMethod[] compute() {
                        return ((GrField) namedElem).getGetters();
                    }
                });
                for (GrAccessorMethod getter : getters) {
                    MethodReferencesSearch.search(getter).forEach(consumer);
                }
            }
            for (final PsiReference reference : references) {
                ApplicationManager.getApplication().runReadAction(() -> {
                    final PsiElement element = reference.getElement();
                    if (element != null) {
                        occurrences.add(element);
                    }
                });
            }
        }

        @Override
        public void onCancel() {
            result.set(false);
        }

        @Override
        public void onThrowable(@NotNull Throwable error) {
            super.onThrowable(error);
            result.set(false);
        }

        @Override
        public void onSuccess() {
            result.set(true);
        }
    };
    ProgressManager.getInstance().run(task);
    return result.get().booleanValue();
}
Also used : GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) HashSet(com.intellij.util.containers.HashSet) GrMethodImpl(org.jetbrains.plugins.groovy.lang.psi.impl.statements.typedef.members.GrMethodImpl) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Task(com.intellij.openapi.progress.Task) MethodReferencesSearch(com.intellij.psi.search.searches.MethodReferencesSearch) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Logger(com.intellij.openapi.diagnostic.Logger) MultiMap(com.intellij.util.containers.MultiMap) ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrClosureSignatureUtil(org.jetbrains.plugins.groovy.lang.psi.impl.signatures.GrClosureSignatureUtil) Collection(java.util.Collection) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ConflictsDialog(com.intellij.refactoring.ui.ConflictsDialog) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ApplicationManager(com.intellij.openapi.application.ApplicationManager) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) Ref(com.intellij.openapi.util.Ref) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrParametersOwner(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrParametersOwner) NonNls(org.jetbrains.annotations.NonNls) Computable(com.intellij.openapi.util.Computable) ContainerUtil(com.intellij.util.containers.ContainerUtil) ArrayList(java.util.ArrayList) GroovyIntentionsBundle(org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle) Intention(org.jetbrains.plugins.groovy.intentions.base.Intention) GrMapType(org.jetbrains.plugins.groovy.lang.psi.impl.GrMapType) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) PsiElementPredicate(org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate) GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) StringUtil(com.intellij.openapi.util.text.StringUtil) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPropertyUtils(org.jetbrains.plugins.groovy.lang.psi.util.GroovyPropertyUtils) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) CommandProcessor(com.intellij.openapi.command.CommandProcessor) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyValidationUtil(org.jetbrains.plugins.groovy.refactoring.GroovyValidationUtil) Collections(java.util.Collections) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) Task(com.intellij.openapi.progress.Task) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) Ref(com.intellij.openapi.util.Ref) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Example 22 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrAliasImportIntention method doRefactoring.

private static void doRefactoring(@NotNull Project project, @NotNull GrImportStatement importStatement, @NotNull PsiMember member) {
    if (member instanceof GrAccessorMethod && !importStatement.isOnDemand() && !importStatement.getImportedName().equals(member.getName())) {
        member = ((GrAccessorMethod) member).getProperty();
    }
    final GroovyFileBase file = (GroovyFileBase) importStatement.getContainingFile();
    final List<UsageInfo> usages = findUsages(member, file);
    GrImportStatement templateImport = createTemplateImport(project, importStatement, member, file);
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        if (!importStatement.isOnDemand()) {
            importStatement.delete();
        }
        updateRefs(usages, member.getName(), templateImport);
    } else {
        runTemplate(project, importStatement, member, file, usages, templateImport);
    }
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 23 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrClassImplUtil method doGetGetters.

@NotNull
private static GrAccessorMethod[] doGetGetters(GrField field) {
    PsiClass containingClass = field.getContainingClass();
    if (containingClass == null)
        return GrAccessorMethod.EMPTY_ARRAY;
    GrAccessorMethod getter = null;
    GrAccessorMethod booleanGetter = null;
    PsiMethod[] getters = containingClass.findMethodsByName(GroovyPropertyUtils.getGetterNameNonBoolean(field.getName()), false);
    for (PsiMethod method : getters) {
        if (method instanceof GrAccessorMethod) {
            getter = (GrAccessorMethod) method;
            break;
        }
    }
    PsiMethod[] booleanGetters = containingClass.findMethodsByName(GroovyPropertyUtils.getGetterNameBoolean(field.getName()), false);
    for (PsiMethod method : booleanGetters) {
        if (method instanceof GrAccessorMethod) {
            booleanGetter = (GrAccessorMethod) method;
            break;
        }
    }
    if (getter != null && booleanGetter != null) {
        return new GrAccessorMethod[] { getter, booleanGetter };
    } else if (getter != null) {
        return new GrAccessorMethod[] { getter };
    } else {
        return GrAccessorMethod.EMPTY_ARRAY;
    }
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GroovyPropertyUtils method getFieldAccessors.

public static List<GrAccessorMethod> getFieldAccessors(GrField field) {
    List<GrAccessorMethod> accessors = new ArrayList<>();
    final GrAccessorMethod[] getters = field.getGetters();
    Collections.addAll(accessors, getters);
    final GrAccessorMethod setter = field.getSetter();
    if (setter != null)
        accessors.add(setter);
    return accessors;
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) ArrayList(java.util.ArrayList)

Example 25 with GrAccessorMethod

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod in project intellij-community by JetBrains.

the class GrMainCompletionProvider method completeStaticMembers.

static StaticMemberProcessor completeStaticMembers(CompletionParameters parameters) {
    final PsiElement position = parameters.getPosition();
    final PsiElement originalPosition = parameters.getOriginalPosition();
    final StaticMemberProcessor processor = new StaticMemberProcessor(position) {

        @NotNull
        @Override
        protected LookupElement createLookupElement(@NotNull PsiMember member, @NotNull PsiClass containingClass, boolean shouldImport) {
            shouldImport |= originalPosition != null && PsiTreeUtil.isAncestor(containingClass, originalPosition, false);
            return createGlobalMemberElement(member, containingClass, shouldImport);
        }

        @Override
        protected LookupElement createLookupElement(@NotNull List<PsiMethod> overloads, @NotNull PsiClass containingClass, boolean shouldImport) {
            shouldImport |= originalPosition != null && PsiTreeUtil.isAncestor(containingClass, originalPosition, false);
            return new JavaGlobalMemberLookupElement(overloads, containingClass, QualifiedMethodInsertHandler.INSTANCE, StaticImportInsertHandler.INSTANCE, shouldImport);
        }

        @Override
        protected boolean isAccessible(PsiMember member) {
            boolean result = super.isAccessible(member);
            if (!result && member instanceof GrField) {
                GrAccessorMethod[] getters = ((GrField) member).getGetters();
                return getters.length > 0 && super.isAccessible(getters[0]);
            }
            return result;
        }
    };
    final PsiFile file = position.getContainingFile();
    if (file instanceof GroovyFile) {
        for (GrImportStatement statement : ((GroovyFile) file).getImportStatements()) {
            if (statement.isStatic()) {
                GrCodeReferenceElement importReference = statement.getImportReference();
                if (importReference != null) {
                    if (!statement.isOnDemand()) {
                        importReference = importReference.getQualifier();
                    }
                    if (importReference != null) {
                        final PsiElement target = importReference.resolve();
                        if (target instanceof PsiClass) {
                            processor.importMembersOf((PsiClass) target);
                        }
                    }
                }
            }
        }
    }
    return processor;
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) NotNull(org.jetbrains.annotations.NotNull) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) List(java.util.List) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrAccessorMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod)25 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)13 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)6 PsiElement (com.intellij.psi.PsiElement)5 ArrayList (java.util.ArrayList)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)5 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)4 MultiMap (com.intellij.util.containers.MultiMap)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 GrCall (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall)3 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 PsiMethod (com.intellij.psi.PsiMethod)2 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)2 UsageInfo (com.intellij.usageView.UsageInfo)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)2