Search in sources :

Example 46 with GrMethod

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

the class AddReturnTypeFix method findMethod.

@Nullable
private static GrMethod findMethod(PsiFile file, final int offset) {
    final PsiElement at = file.findElementAt(offset);
    if (at == null)
        return null;
    if (at.getParent() instanceof GrReturnStatement) {
        final GrReturnStatement returnStatement = ((GrReturnStatement) at.getParent());
        final PsiElement word = returnStatement.getReturnWord();
        if (!word.getTextRange().contains(offset))
            return null;
        final GroovyPsiElement returnOwner = PsiTreeUtil.getParentOfType(returnStatement, GrClosableBlock.class, GrMethod.class);
        if (returnOwner instanceof GrMethod) {
            final GrTypeElement returnTypeElement = ((GrMethod) returnOwner).getReturnTypeElementGroovy();
            if (returnTypeElement == null) {
                return (GrMethod) returnOwner;
            }
        }
        return null;
    }
    final GrMethod method = PsiTreeUtil.getParentOfType(at, GrMethod.class, false, GrTypeDefinition.class, GrClosableBlock.class);
    if (method != null && GrHighlightUtil.getMethodHeaderTextRange(method).contains(offset)) {
        if (method.getReturnTypeElementGroovy() == null) {
            return method;
        }
    }
    return null;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrReturnStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 47 with GrMethod

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

the class GrMethodBodyFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (!(psiElement instanceof GrMethod))
        return;
    GrMethod method = (GrMethod) psiElement;
    final PsiClass aClass = method.getContainingClass();
    if (aClass != null && aClass.isInterface() || method.hasModifierProperty(PsiModifier.ABSTRACT))
        return;
    final GrCodeBlock body = method.getBlock();
    final Document doc = editor.getDocument();
    if (body != null) {
        // See IDEADEV-1093. This is quite hacky heuristic but it seem to be best we can do.
        String bodyText = body.getText();
        if (StringUtil.startsWithChar(bodyText, '{')) {
            final GrStatement[] statements = body.getStatements();
            if (statements.length > 0) {
            //          [todo]
            //          if (statements[0] instanceof PsiDeclarationStatement) {
            //            if (PsiTreeUtil.getDeepestLast(statements[0]) instanceof PsiErrorElement) {
            //              if (method.getContainingClass().getRBrace() == null) {
            //                doc.insertString(body.getTextRange().getStartOffset() + 1, "\n}");
            //              }
            //            }
            //          }
            }
        }
        return;
    }
    int endOffset = method.getTextRange().getEndOffset();
    if (StringUtil.endsWithChar(method.getText(), ';')) {
        doc.deleteString(endOffset - 1, endOffset);
        endOffset--;
    }
    doc.insertString(endOffset, "{\n}");
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiClass(com.intellij.psi.PsiClass) Document(com.intellij.openapi.editor.Document) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 48 with GrMethod

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

the class AnnotatedMembersSearcher method execute.

@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
    final PsiClass annClass = p.getAnnotationClass();
    assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
    final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            return annClass.getQualifiedName();
        }
    });
    assert annotationFQN != null;
    final SearchScope scope = p.getScope();
    final List<PsiModifierListOwner> candidates;
    if (scope instanceof GlobalSearchScope) {
        candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope) scope));
    } else {
        candidates = new ArrayList<>();
        for (final PsiElement element : ((LocalSearchScope) scope).getScope()) {
            ApplicationManager.getApplication().runReadAction(() -> {
                if (element instanceof GroovyPsiElement) {
                    ((GroovyPsiElement) element).accept(new GroovyRecursiveElementVisitor() {

                        @Override
                        public void visitMethod(@NotNull GrMethod method) {
                            candidates.add(method);
                        }

                        @Override
                        public void visitField(@NotNull GrField field) {
                            candidates.add(field);
                        }
                    });
                }
            });
        }
    }
    for (final PsiModifierListOwner candidate : candidates) {
        boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {

            @Override
            public Boolean compute() {
                if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
                    PsiModifierList list = candidate.getModifierList();
                    if (list != null) {
                        for (PsiAnnotation annotation : list.getAnnotations()) {
                            if ((p.isApproximate() || annotationFQN.equals(annotation.getQualifiedName())) && !consumer.process(candidate)) {
                                return false;
                            }
                        }
                    }
                }
                return true;
            }
        });
        if (!accepted)
            return false;
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 49 with GrMethod

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

the class GDKSuperMethodSearcher method execute.

@Override
public boolean execute(@NotNull SuperMethodsSearch.SearchParameters queryParameters, @NotNull Processor<MethodSignatureBackedByPsiMethod> consumer) {
    final PsiMethod method = queryParameters.getMethod();
    if (!(method instanceof GrMethod)) {
        return true;
    }
    if (method.hasModifierProperty(PsiModifier.STATIC))
        return true;
    final PsiClass psiClass = method.getContainingClass();
    if (psiClass == null)
        return true;
    final HierarchicalMethodSignature hierarchicalSignature = method.getHierarchicalMethodSignature();
    if (!hierarchicalSignature.getSuperSignatures().isEmpty())
        return true;
    final Project project = method.getProject();
    final String name = method.getName();
    final MethodResolverProcessor processor = new MethodResolverProcessor(name, ((GrMethod) method), false, null, null, PsiType.EMPTY_ARRAY);
    ResolveUtil.processNonCodeMembers(JavaPsiFacade.getElementFactory(project).createType(psiClass), processor, (GrMethod) method, ResolveState.initial());
    final GroovyResolveResult[] candidates = processor.getCandidates();
    final PsiManager psiManager = PsiManager.getInstance(project);
    final MethodSignature signature = method.getHierarchicalMethodSignature();
    List<PsiMethod> goodSupers = new ArrayList<>();
    for (GroovyResolveResult candidate : candidates) {
        final PsiElement element = candidate.getElement();
        if (element instanceof PsiMethod) {
            final PsiMethod m = (PsiMethod) element;
            if (!isTheSameMethod(method, psiManager, m) && PsiImplUtil.isExtendsSignature(m.getHierarchicalMethodSignature(), signature)) {
                goodSupers.add(m);
            }
        }
    }
    if (goodSupers.isEmpty())
        return true;
    List<PsiMethod> result = new ArrayList<>(goodSupers.size());
    result.add(goodSupers.get(0));
    final Comparator<PsiMethod> comparator = (o1, o2) -> {
        //compare by first parameter type
        final PsiType type1 = getRealType(o1);
        final PsiType type2 = getRealType(o2);
        if (TypesUtil.isAssignableByMethodCallConversion(type1, type2, o1)) {
            return -1;
        } else if (TypesUtil.isAssignableByMethodCallConversion(type2, type1, o1)) {
            return 1;
        }
        return 0;
    };
    Outer: for (PsiMethod current : goodSupers) {
        for (Iterator<PsiMethod> i = result.iterator(); i.hasNext(); ) {
            PsiMethod m = i.next();
            final int res = comparator.compare(m, current);
            if (res > 0) {
                continue Outer;
            } else if (res < 0) {
                i.remove();
            }
        }
        result.add(current);
    }
    for (PsiMethod psiMethod : result) {
        if (!consumer.process(getRealMethod(psiMethod).getHierarchicalMethodSignature())) {
            return false;
        }
    }
    return true;
}
Also used : TypeConversionUtil(com.intellij.psi.util.TypeConversionUtil) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor) Iterator(java.util.Iterator) PsiImplUtil(org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil) QueryExecutor(com.intellij.util.QueryExecutor) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) TypesUtil(org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil) ResolveUtil(org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) MethodSignature(com.intellij.psi.util.MethodSignature) SuperMethodsSearch(com.intellij.psi.search.searches.SuperMethodsSearch) Processor(com.intellij.util.Processor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Project(com.intellij.openapi.project.Project) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Comparator(java.util.Comparator) MethodSignature(com.intellij.psi.util.MethodSignature) MethodSignatureBackedByPsiMethod(com.intellij.psi.util.MethodSignatureBackedByPsiMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) Iterator(java.util.Iterator) MethodResolverProcessor(org.jetbrains.plugins.groovy.lang.resolve.processors.MethodResolverProcessor)

Example 50 with GrMethod

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

the class GroovyConstructorUsagesSearcher method processImplicitConstructorCall.

private static void processImplicitConstructorCall(@NotNull final PsiMember usage, final Processor<PsiReference> processor, final PsiMethod constructor) {
    if (constructor instanceof GrMethod) {
        GrParameter[] grParameters = (GrParameter[]) constructor.getParameterList().getParameters();
        if (grParameters.length > 0 && !grParameters[0].isOptional())
            return;
    } else if (constructor.getParameterList().getParameters().length > 0)
        return;
    PsiManager manager = constructor.getManager();
    if (manager.areElementsEquivalent(usage, constructor) || manager.areElementsEquivalent(constructor.getContainingClass(), usage.getContainingClass()))
        return;
    processor.process(new LightMemberReference(manager, usage, PsiSubstitutor.EMPTY) {

        @Override
        public PsiElement getElement() {
            return usage;
        }

        @Override
        public TextRange getRangeInElement() {
            if (usage instanceof PsiClass) {
                PsiIdentifier identifier = ((PsiClass) usage).getNameIdentifier();
                if (identifier != null)
                    return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
            } else if (usage instanceof PsiMethod) {
                PsiIdentifier identifier = ((PsiMethod) usage).getNameIdentifier();
                if (identifier != null)
                    return TextRange.from(identifier.getStartOffsetInParent(), identifier.getTextLength());
            }
            return super.getRangeInElement();
        }
    });
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) LightMemberReference(com.intellij.psi.impl.light.LightMemberReference) TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)134 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)24 PsiElement (com.intellij.psi.PsiElement)22 NotNull (org.jetbrains.annotations.NotNull)21 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)18 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)17 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)16 ArrayList (java.util.ArrayList)15 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)15 Nullable (org.jetbrains.annotations.Nullable)12 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)12 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)12 IncorrectOperationException (com.intellij.util.IncorrectOperationException)10 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)10 Project (com.intellij.openapi.project.Project)9 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)8 GrReturnStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement)8