Search in sources :

Example 21 with GrMethod

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

the class GroovyOverrideImplementUtil method generateTraitMethodPrototype.

public static GrMethod generateTraitMethodPrototype(GrTypeDefinition aClass, GrTraitMethod method, PsiSubstitutor substitutor) {
    final Project project = aClass.getProject();
    final GrMethod result = (GrMethod) GenerateMembersUtil.substituteGenericMethod(method, substitutor, aClass);
    setupModifierList(result);
    setupTraitMethodBody(project, result, method);
    setupReturnType(result, method);
    setupAnnotations(aClass, method, result);
    GroovyChangeContextUtil.encodeContextInfo(result);
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 22 with GrMethod

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

the class GroovyOverrideImplementUtil method generateMethodPrototype.

public static GrMethod generateMethodPrototype(GrTypeDefinition aClass, PsiMethod method, PsiSubstitutor substitutor) {
    final Project project = aClass.getProject();
    final boolean isAbstract = method.hasModifierProperty(PsiModifier.ABSTRACT);
    String templName = isAbstract ? JavaTemplateUtil.TEMPLATE_IMPLEMENTED_METHOD_BODY : JavaTemplateUtil.TEMPLATE_OVERRIDDEN_METHOD_BODY;
    final FileTemplate template = FileTemplateManager.getInstance(method.getProject()).getCodeTemplate(templName);
    final GrMethod result = (GrMethod) GenerateMembersUtil.substituteGenericMethod(method, substitutor, aClass);
    setupModifierList(result);
    setupOverridingMethodBody(project, method, result, template, substitutor);
    setupReturnType(result, method);
    setupAnnotations(aClass, method, result);
    GroovyChangeContextUtil.encodeContextInfo(result);
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 23 with GrMethod

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

the class GroovyParameterInfoHandler method updateConstructorParams.

private static PsiParameter[] updateConstructorParams(PsiMethod method, PsiParameter[] params, PsiElement place) {
    if (GrInnerClassConstructorUtil.isInnerClassConstructorUsedOutsideOfItParent(method, place)) {
        GrMethod grMethod = (GrMethod) method;
        params = GrInnerClassConstructorUtil.addEnclosingInstanceParam(grMethod, method.getContainingClass().getContainingClass(), grMethod.getParameters(), true);
    }
    return params;
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 24 with GrMethod

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

the class CreateParameterFromUsageFix method findScope.

private void findScope(@NotNull final GrReferenceExpression ref, @NotNull final Editor editor, final Project project) {
    PsiElement place = ref;
    final List<GrMethod> scopes = new ArrayList<>();
    while (true) {
        final GrMethod parent = PsiTreeUtil.getParentOfType(place, GrMethod.class);
        if (parent == null)
            break;
        scopes.add(parent);
        place = parent;
    }
    if (scopes.size() == 1) {
        final GrMethod owner = scopes.get(0);
        final PsiMethod toSearchFor;
        toSearchFor = SuperMethodWarningUtil.checkSuperMethod(owner, RefactoringBundle.message("to.refactor"));
        //if it is null, refactoring was canceled
        if (toSearchFor == null)
            return;
        showDialog(toSearchFor, ref, project);
    } else if (scopes.size() > 1) {
        myEnclosingMethodsPopup = MethodOrClosureScopeChooser.create(scopes, editor, this, (owner, element) -> {
            showDialog((PsiMethod) owner, ref, project);
            return null;
        });
        myEnclosingMethodsPopup.showInBestPositionFor(editor);
    }
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) PsiElement(com.intellij.psi.PsiElement)

Example 25 with GrMethod

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

the class CreateParameterFromUsageFix method showDialog.

private static void showDialog(final PsiMethod method, final GrReferenceExpression ref, final Project project) {
    final String name = ref.getReferenceName();
    final List<PsiType> types = GroovyExpectedTypesProvider.getDefaultExpectedTypes(ref);
    PsiType unboxed = types.isEmpty() ? null : TypesUtil.unboxPrimitiveTypeWrapper(types.get(0));
    @NotNull final PsiType type = unboxed != null ? unboxed : PsiType.getJavaLangObject(ref.getManager(), ref.getResolveScope());
    if (method instanceof GrMethod) {
        GrMethodDescriptor descriptor = new GrMethodDescriptor((GrMethod) method);
        GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, descriptor, true, ref);
        List<GrParameterInfo> parameters = dialog.getParameters();
        parameters.add(createParameterInfo(name, type));
        dialog.setParameterInfos(parameters);
        dialog.show();
    } else if (method != null) {
        JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(project, method, false, ref);
        final List<ParameterInfoImpl> parameterInfos = new ArrayList<>(Arrays.asList(ParameterInfoImpl.fromMethod(method)));
        ParameterInfoImpl parameterInfo = new ParameterInfoImpl(-1, name, type, PsiTypesUtil.getDefaultValueOfType(type), false);
        if (!method.isVarArgs()) {
            parameterInfos.add(parameterInfo);
        } else {
            parameterInfos.add(parameterInfos.size() - 1, parameterInfo);
        }
        dialog.setParameterInfos(parameterInfos);
        dialog.show();
    }
}
Also used : GrMethodDescriptor(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrMethodDescriptor) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ParameterInfoImpl(com.intellij.refactoring.changeSignature.ParameterInfoImpl) NotNull(org.jetbrains.annotations.NotNull) GrChangeSignatureDialog(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrChangeSignatureDialog) GrParameterInfo(org.jetbrains.plugins.groovy.refactoring.changeSignature.GrParameterInfo) JavaChangeSignatureDialog(com.intellij.refactoring.changeSignature.JavaChangeSignatureDialog) ArrayList(java.util.ArrayList) List(java.util.List) PsiType(com.intellij.psi.PsiType)

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