Search in sources :

Example 86 with GrMethod

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

the class TraitMethodImplementor method createImplementationPrototypes.

@NotNull
@Override
public PsiMethod[] createImplementationPrototypes(PsiClass inClass, PsiMethod method) throws IncorrectOperationException {
    if (!(inClass instanceof GrTypeDefinition && method instanceof GrTraitMethod))
        return PsiMethod.EMPTY_ARRAY;
    final PsiClass containingClass = method.getContainingClass();
    PsiSubstitutor substitutor = inClass.isInheritor(containingClass, true) ? TypeConversionUtil.getSuperClassSubstitutor(containingClass, inClass, PsiSubstitutor.EMPTY) : PsiSubstitutor.EMPTY;
    return new GrMethod[] { GroovyOverrideImplementUtil.generateTraitMethodPrototype((GrTypeDefinition) inClass, (GrTraitMethod) method, substitutor) };
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiClass(com.intellij.psi.PsiClass) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiSubstitutor(com.intellij.psi.PsiSubstitutor) GrTraitMethod(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrTraitMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with GrMethod

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

the class GrAnnotatorImpl method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (FileIndexFacade.getInstance(element.getProject()).isInLibrarySource(element.getContainingFile().getVirtualFile()))
        return;
    if (element instanceof GroovyPsiElement) {
        final GroovyAnnotator annotator = new GroovyAnnotator(holder);
        ((GroovyPsiElement) element).accept(annotator);
        if (PsiUtil.isCompileStatic(element)) {
            final GroovyStaticTypeCheckVisitor typeCheckVisitor = myTypeCheckVisitorThreadLocal.get();
            assert typeCheckVisitor != null;
            typeCheckVisitor.accept((GroovyPsiElement) element, holder);
        }
    } else if (element instanceof PsiComment) {
        String text = element.getText();
        if (text.startsWith("/*") && !(text.endsWith("*/"))) {
            TextRange range = element.getTextRange();
            holder.createErrorAnnotation(TextRange.create(range.getEndOffset() - 1, range.getEndOffset()), GroovyBundle.message("doc.end.expected"));
        }
    } else {
        final PsiElement parent = element.getParent();
        if (parent instanceof GrMethod) {
            if (element.equals(((GrMethod) parent).getNameIdentifierGroovy()) && ((GrMethod) parent).getReturnTypeElementGroovy() == null) {
                GroovyAnnotator.checkMethodReturnType((GrMethod) parent, element, holder);
            }
        } else if (parent instanceof GrField) {
            final GrField field = (GrField) parent;
            if (element.equals(field.getNameIdentifierGroovy())) {
                final GrAccessorMethod[] getters = field.getGetters();
                for (GrAccessorMethod getter : getters) {
                    GroovyAnnotator.checkMethodReturnType(getter, field.getNameIdentifierGroovy(), holder);
                }
                final GrAccessorMethod setter = field.getSetter();
                if (setter != null) {
                    GroovyAnnotator.checkMethodReturnType(setter, field.getNameIdentifierGroovy(), holder);
                }
            }
        }
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) PsiComment(com.intellij.psi.PsiComment) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyStaticTypeCheckVisitor(org.jetbrains.plugins.groovy.codeInspection.type.GroovyStaticTypeCheckVisitor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 88 with GrMethod

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

the class GrMethodBaseImpl method setName.

@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
    PsiElement nameElement = getNameIdentifierGroovy();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(nameElement.getProject());
    PsiElement newNameElement;
    if (PsiNameHelper.getInstance(getProject()).isIdentifier(name)) {
        try {
            GrMethod method = factory.createMethod(name, null);
            newNameElement = method.getNameIdentifierGroovy();
        } catch (IncorrectOperationException e) {
            newNameElement = factory.createLiteralFromValue(name).getFirstChild();
        }
    } else {
        newNameElement = factory.createLiteralFromValue(name).getFirstChild();
    }
    nameElement.replace(newNameElement);
    return this;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 89 with GrMethod

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

the class DuplicatesUtil method collectMethodDuplicates.

public static void collectMethodDuplicates(Map<GrMethod, List<GrMethod>> map, HashSet<GrMethod> duplicateMethodsWarning, HashSet<GrMethod> duplicateMethodsErrors) {
    for (GrMethod method : map.keySet()) {
        List<GrMethod> duplicateMethods = map.get(method);
        if (duplicateMethods != null && duplicateMethods.size() > 1) {
            HashMap<PsiType, GrMethod> duplicateMethodsToReturnTypeMap = new HashMap<>();
            for (GrMethod duplicateMethod : duplicateMethods) {
                GrTypeElement typeElement = duplicateMethod.getReturnTypeElementGroovy();
                PsiType methodReturnType;
                if (typeElement != null) {
                    methodReturnType = typeElement.getType();
                } else {
                    methodReturnType = PsiType.NULL;
                }
                duplicateMethodsWarning.add(duplicateMethod);
                GrMethod grMethodWithType = duplicateMethodsToReturnTypeMap.get(methodReturnType);
                if (grMethodWithType != null) {
                    duplicateMethodsErrors.add(duplicateMethod);
                    duplicateMethodsErrors.add(grMethodWithType);
                    duplicateMethodsWarning.remove(duplicateMethod);
                    duplicateMethodsWarning.remove(grMethodWithType);
                }
                duplicateMethodsToReturnTypeMap.put(methodReturnType, duplicateMethod);
            }
        }
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) THashMap(gnu.trove.THashMap) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiType(com.intellij.psi.PsiType)

Example 90 with GrMethod

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

the class IntentionUtils method createTemplateForMethod.

public static void createTemplateForMethod(PsiType[] argTypes, ChooseTypeExpression[] paramTypesExpressions, PsiMethod method, PsiClass owner, TypeConstraint[] constraints, boolean isConstructor, @NotNull final PsiElement context) {
    final Project project = owner.getProject();
    PsiTypeElement typeElement = method.getReturnTypeElement();
    ChooseTypeExpression expr = new ChooseTypeExpression(constraints, PsiManager.getInstance(project), context.getResolveScope(), method.getLanguage() == GroovyLanguage.INSTANCE);
    TemplateBuilderImpl builder = new TemplateBuilderImpl(method);
    if (!isConstructor) {
        assert typeElement != null;
        builder.replaceElement(typeElement, expr);
    }
    PsiParameter[] parameters = method.getParameterList().getParameters();
    assert parameters.length == argTypes.length;
    for (int i = 0; i < parameters.length; i++) {
        PsiParameter parameter = parameters[i];
        PsiTypeElement parameterTypeElement = parameter.getTypeElement();
        builder.replaceElement(parameterTypeElement, paramTypesExpressions[i]);
        builder.replaceElement(parameter.getNameIdentifier(), new ParameterNameExpression(null));
    }
    PsiCodeBlock body = method.getBody();
    if (body != null) {
        PsiElement lbrace = body.getLBrace();
        assert lbrace != null;
        builder.setEndVariableAfter(lbrace);
    } else {
        builder.setEndVariableAfter(method.getParameterList());
    }
    method = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(method);
    Template template = builder.buildTemplate();
    final PsiFile targetFile = owner.getContainingFile();
    final Editor newEditor = positionCursor(project, targetFile, method);
    TextRange range = method.getTextRange();
    newEditor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    TemplateManager manager = TemplateManager.getInstance(project);
    TemplateEditingListener templateListener = new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            ApplicationManager.getApplication().runWriteAction(() -> {
                PsiDocumentManager.getInstance(project).commitDocument(newEditor.getDocument());
                final int offset = newEditor.getCaretModel().getOffset();
                PsiMethod method1 = PsiTreeUtil.findElementOfClassAtOffset(targetFile, offset - 1, PsiMethod.class, false);
                if (context instanceof PsiMethod) {
                    final PsiTypeParameter[] typeParameters = ((PsiMethod) context).getTypeParameters();
                    if (typeParameters.length > 0) {
                        for (PsiTypeParameter typeParameter : typeParameters) {
                            if (CreateMethodFromUsageFix.checkTypeParam(method1, typeParameter)) {
                                final JVMElementFactory factory = JVMElementFactories.getFactory(method1.getLanguage(), method1.getProject());
                                PsiTypeParameterList list = method1.getTypeParameterList();
                                if (list == null) {
                                    PsiTypeParameterList newList = factory.createTypeParameterList();
                                    list = (PsiTypeParameterList) method1.addAfter(newList, method1.getModifierList());
                                }
                                list.add(factory.createTypeParameter(typeParameter.getName(), typeParameter.getExtendsList().getReferencedTypes()));
                            }
                        }
                    }
                }
                if (method1 != null) {
                    try {
                        final boolean hasNoReturnType = method1.getReturnTypeElement() == null && method1 instanceof GrMethod;
                        if (hasNoReturnType) {
                            ((GrMethod) method1).setReturnType(PsiType.VOID);
                        }
                        if (method1.getBody() != null) {
                            FileTemplateManager templateManager = FileTemplateManager.getInstance(project);
                            FileTemplate fileTemplate = templateManager.getCodeTemplate(GroovyTemplates.GROOVY_FROM_USAGE_METHOD_BODY);
                            PsiClass containingClass = method1.getContainingClass();
                            LOG.assertTrue(!containingClass.isInterface() || GrTraitUtil.isTrait(containingClass), "Interface bodies should be already set up");
                            CreateFromUsageUtils.setupMethodBody(method1, containingClass, fileTemplate);
                        }
                        if (hasNoReturnType) {
                            ((GrMethod) method1).setReturnType(null);
                        }
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                    CreateFromUsageUtils.setupEditor(method1, newEditor);
                }
            });
        }
    };
    manager.startTemplate(newEditor, template, templateListener);
}
Also used : FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) ChooseTypeExpression(org.jetbrains.plugins.groovy.template.expressions.ChooseTypeExpression) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) FileTemplateManager(com.intellij.ide.fileTemplates.FileTemplateManager) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) TextRange(com.intellij.openapi.util.TextRange) TypeConstraint(org.jetbrains.plugins.groovy.lang.psi.expectedTypes.TypeConstraint) Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) ParameterNameExpression(org.jetbrains.plugins.groovy.template.expressions.ParameterNameExpression)

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