Search in sources :

Example 76 with GrMethod

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

the class GrFieldMember method generateGetter.

@Override
@Nullable
public GroovyGenerationInfo<GrMethod> generateGetter() {
    PsiField field = getElement();
    final GrMethod method = createMethodIfNotExists(field, GroovyPropertyUtils.generateGetterPrototype(field));
    return method != null ? new GroovyGenerationInfo<>(method) : null;
}
Also used : PsiField(com.intellij.psi.PsiField) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with GrMethod

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

the class GroovyGenerateConstructorHandler method chooseOriginalMembers.

@Override
@Nullable
protected ClassMember[] chooseOriginalMembers(PsiClass aClass, Project project) {
    final ClassMember[] classMembers = chooseOriginalMembersImpl(aClass, project);
    if (classMembers == null)
        return null;
    List<ClassMember> res = new ArrayList<>();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    for (ClassMember classMember : classMembers) {
        if (classMember instanceof PsiMethodMember) {
            final PsiMethod method = ((PsiMethodMember) classMember).getElement();
            PsiMethod copy = factory.createMethodFromText(GroovyToJavaGenerator.generateMethodStub(method), method);
            if (method instanceof GrMethod) {
                GrParameter[] parameters = ((GrMethod) method).getParameterList().getParameters();
                PsiParameter[] copyParameters = copy.getParameterList().getParameters();
                for (int i = 0; i < parameters.length; i++) {
                    if (parameters[i].getTypeElementGroovy() == null) {
                        copyParameters[i].setName(DEF_PSEUDO_ANNO + parameters[i].getName());
                    }
                }
            }
            res.add(new PsiMethodMember(copy));
        } else if (classMember instanceof PsiFieldMember) {
            final PsiField field = ((PsiFieldMember) classMember).getElement();
            String prefix = field instanceof GrField && ((GrField) field).getTypeElementGroovy() == null ? DEF_PSEUDO_ANNO : "";
            res.add(new PsiFieldMember(factory.createFieldFromText(field.getType().getCanonicalText() + " " + prefix + field.getName(), aClass)));
        }
    }
    return res.toArray(new ClassMember[res.size()]);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Nullable(org.jetbrains.annotations.Nullable)

Example 78 with GrMethod

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

the class GroovyGenerateConstructorHandler method generateMemberPrototypes.

@Override
@NotNull
protected List<? extends GenerationInfo> generateMemberPrototypes(PsiClass aClass, ClassMember[] members) throws IncorrectOperationException {
    final List<? extends GenerationInfo> list = super.generateMemberPrototypes(aClass, members);
    List<PsiGenerationInfo<GrMethod>> grConstructors = new ArrayList<>();
    final Project project = aClass.getProject();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    for (GenerationInfo generationInfo : list) {
        final PsiMember constructorMember = generationInfo.getPsiMember();
        assert constructorMember instanceof PsiMethod;
        final PsiMethod constructor = (PsiMethod) constructorMember;
        final PsiCodeBlock block = constructor.getBody();
        assert block != null;
        final String constructorName = aClass.getName();
        final String body = StringUtil.replace(StringUtil.replace(block.getText(), DEF_PSEUDO_ANNO, ""), ";", "");
        final PsiParameterList list1 = constructor.getParameterList();
        List<String> parametersNames = new ArrayList<>();
        List<String> parametersTypes = new ArrayList<>();
        for (PsiParameter parameter : list1.getParameters()) {
            final String fullName = parameter.getName();
            parametersNames.add(StringUtil.trimStart(fullName, DEF_PSEUDO_ANNO));
            parametersTypes.add(fullName.startsWith(DEF_PSEUDO_ANNO) ? null : parameter.getType().getCanonicalText());
        }
        final String[] paramNames = ArrayUtil.toStringArray(parametersNames);
        final String[] paramTypes = ArrayUtil.toStringArray(parametersTypes);
        assert constructorName != null;
        GrMethod grConstructor = factory.createConstructorFromText(constructorName, paramTypes, paramNames, body);
        PsiReferenceList throwsList = grConstructor.getThrowsList();
        for (PsiJavaCodeReferenceElement element : constructor.getThrowsList().getReferenceElements()) {
            throwsList.add(element);
        }
        codeStyleManager.shortenClassReferences(grConstructor);
        grConstructors.add(new GroovyGenerationInfo<>(grConstructor));
    }
    return grConstructors;
}
Also used : ArrayList(java.util.ArrayList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) GroovyGenerationInfo(org.jetbrains.plugins.groovy.actions.generate.GroovyGenerationInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with GrMethod

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

the class CategoryTransformationSupport method applyTransformation.

@Override
public void applyTransformation(@NotNull TransformationContext context) {
    PsiClassType type = GdkMethodUtil.getCategoryType(context.getCodeClass());
    if (type == null)
        return;
    for (GrMethod method : context.getCodeClass().getCodeMethods()) {
        context.removeMethod(method);
        context.addMethods(GrReflectedMethodImpl.doCreateReflectedMethods(method, type, method.getParameters()));
    }
}
Also used : PsiClassType(com.intellij.psi.PsiClassType) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 80 with GrMethod

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

the class GrChangeSignatureUsageProcessor method generateDelegate.

private static boolean generateDelegate(JavaChangeInfo grInfo) {
    final GrMethod method = (GrMethod) grInfo.getMethod();
    final PsiClass psiClass = method.getContainingClass();
    GrMethod newMethod = (GrMethod) method.copy();
    newMethod = (GrMethod) psiClass.addAfter(newMethod, method);
    StringBuilder buffer = new StringBuilder();
    buffer.append("\n");
    if (method.isConstructor()) {
        buffer.append("this");
    } else {
        if (!PsiType.VOID.equals(method.getReturnType())) {
            buffer.append("return ");
        }
        buffer.append(GrChangeSignatureUtil.getNameWithQuotesIfNeeded(grInfo.getNewName(), method.getProject()));
    }
    generateParametersForDelegateCall(grInfo, method, buffer);
    final GrCodeBlock codeBlock = GroovyPsiElementFactory.getInstance(method.getProject()).createMethodBodyFromText(buffer.toString());
    newMethod.setBlock(codeBlock);
    newMethod.getModifierList().setModifierProperty(PsiModifier.ABSTRACT, false);
    CodeStyleManager.getInstance(method.getProject()).reformat(newMethod);
    return processPrimaryMethodInner(grInfo, method, null);
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

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