Search in sources :

Example 61 with GrMethod

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

the class GroovyPsiElementFactoryImpl method createParameterList.

@NotNull
@Override
public PsiParameterList createParameterList(@NotNull @NonNls String[] names, @NotNull PsiType[] types) throws IncorrectOperationException {
    StringBuilder builder = StringBuilderSpinAllocator.alloc();
    builder.append("def foo(");
    for (int i = 0; i < names.length; i++) {
        String name = names[i];
        final PsiType type = types[i];
        if (type != null) {
            builder.append(type.getCanonicalText());
            builder.append(' ');
        }
        builder.append(name);
        builder.append(',');
    }
    if (names.length > 0) {
        builder.delete(builder.length() - 1, builder.length());
    }
    builder.append("){}");
    final GrMethod method = createMethodFromText(builder);
    return method.getParameterList();
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 62 with GrMethod

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

the class GroovyPsiElementFactoryImpl method createMethod.

@NotNull
@Override
public GrMethod createMethod(@NotNull @NonNls String name, PsiType returnType, PsiElement context) throws IncorrectOperationException {
    StringBuilder builder = StringBuilderSpinAllocator.alloc();
    try {
        builder.append("def <T>");
        if (returnType != null) {
            builder.append(returnType.getCanonicalText());
        }
        builder.append(' ');
        if (GroovyNamesUtil.isIdentifier(name)) {
            builder.append(name);
        } else {
            builder.append('"');
            builder.append(GrStringUtil.escapeSymbolsForGString(name, true, false));
            builder.append('"');
        }
        builder.append("(){}");
        GrMethod method = createMethodFromText(builder.toString(), context);
        PsiTypeParameterList typeParameterList = method.getTypeParameterList();
        assert typeParameterList != null;
        typeParameterList.getFirstChild().delete();
        typeParameterList.getFirstChild().delete();
        typeParameterList.getFirstChild().delete();
        if (returnType != null) {
            method.getModifierList().setModifierProperty(GrModifier.DEF, false);
        }
        return method;
    } finally {
        StringBuilderSpinAllocator.dispose(builder);
    }
}
Also used : GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) NotNull(org.jetbrains.annotations.NotNull)

Example 63 with GrMethod

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

the class GrParameterImpl method isMainMethodFirstUntypedParameter.

private boolean isMainMethodFirstUntypedParameter() {
    if (getTypeElementGroovy() != null)
        return false;
    if (!(getParent() instanceof GrParameterList))
        return false;
    if (isOptional())
        return false;
    GrParameterList parameterList = (GrParameterList) getParent();
    if (!(parameterList.getParent() instanceof GrMethod))
        return false;
    GrMethod method = (GrMethod) parameterList.getParent();
    return PsiImplUtil.isMainMethod(method);
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 64 with GrMethod

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

the class GroovyPropertyUtils method generateSetterPrototype.

public static GrMethod generateSetterPrototype(PsiField field) {
    Project project = field.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    String name = field.getName();
    boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    VariableKind kind = codeStyleManager.getVariableKind(field);
    String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
    String setName = getSetterName(field.getName());
    final PsiClass containingClass = field.getContainingClass();
    try {
        GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
        String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
        final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
        GrParameter param = factory.createParameter(parameterName, type);
        annotateWithNullableStuff(field, param);
        setMethod.getParameterList().add(param);
        PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
        @NonNls StringBuilder builder = new StringBuilder();
        if (name.equals(parameterName)) {
            if (!isStatic) {
                builder.append("this.");
            } else {
                String className = containingClass.getName();
                if (className != null) {
                    builder.append(className);
                    builder.append(".");
                }
            }
        }
        builder.append(name);
        builder.append("=");
        builder.append(parameterName);
        builder.append("\n");
        GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
        setMethod.getBlock().replace(body);
        return setMethod;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) NonNls(org.jetbrains.annotations.NonNls) 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) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VariableKind(com.intellij.psi.codeStyle.VariableKind) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 65 with GrMethod

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

the class GrTypeParameterListImpl method appendParenthesesIfNeeded.

private void appendParenthesesIfNeeded() {
    PsiElement first = getFirstChild();
    if (first == null) {
        getNode().addLeaf(GroovyTokenTypes.mLT, "<", null);
    }
    PsiElement last = getLastChild();
    if (last.getNode().getElementType() != GroovyTokenTypes.mGT) {
        getNode().addLeaf(GroovyTokenTypes.mGT, ">", null);
    }
    PsiElement parent = getParent();
    if (parent instanceof GrMethod) {
        GrModifierList list = ((GrMethod) parent).getModifierList();
        PsiElement[] modifiers = list.getModifiers();
        if (modifiers.length == 0) {
            list.setModifierProperty(GrModifier.DEF, true);
        }
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) StubBasedPsiElement(com.intellij.psi.StubBasedPsiElement) PsiElement(com.intellij.psi.PsiElement)

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