Search in sources :

Example 66 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter 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 67 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyValidationUtil method validateNewParameterName.

public static boolean validateNewParameterName(GrParameter variable, MultiMap<PsiElement, String> conflicts, @NotNull String varName) {
    GrParameterList list = PsiTreeUtil.getParentOfType(variable, GrParameterList.class);
    GrParametersOwner owner = PsiTreeUtil.getParentOfType(variable, GrParametersOwner.class);
    assert owner != null;
    for (GrParameter parameter : list.getParameters()) {
        if (parameter.equals(variable))
            continue;
        validateVariableOccurrencesDownImpl(parameter, conflicts, varName);
    }
    validateVariableOccurrencesDown(owner, list, conflicts, varName);
    PsiElement parent = owner.getParent();
    validateVariableOccurrencesUp(parent, owner, conflicts, varName, parent instanceof GroovyFile);
    return conflicts.isEmpty();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 68 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyValidationUtil method validateVariableOccurrencesUp.

private static void validateVariableOccurrencesUp(PsiElement parent, PsiElement lastParent, MultiMap<PsiElement, String> conflicts, @NotNull String varName, final boolean containerIsFile) {
    if (!containerIsFile && (parent instanceof PsiFile) || parent == null)
        return;
    PsiElement child = parent.getFirstChild();
    while (child != null && child != lastParent) {
        // Upper variable declarations
        if (child instanceof GrVariableDeclaration) {
            for (GrVariable variable : ((GrVariableDeclaration) child).getVariables()) {
                if (varName.equals(variable.getName())) {
                    addConflict(varName, variable, conflicts);
                }
            }
        }
        child = child.getNextSibling();
    }
    if (parent instanceof GrParametersOwner) {
        //method or closure parameters
        GrParametersOwner owner = (GrParametersOwner) parent;
        for (GrParameter parameter : owner.getParameters()) {
            if (varName.equals(parameter.getName())) {
                addConflict(varName, parameter, conflicts);
            }
        }
    } else if (parent instanceof GrForStatement) {
        // For statement binding
        GrForStatement statement = (GrForStatement) parent;
        GrForClause clause = statement.getClause();
        if (clause != null) {
            final GrVariable variable = clause.getDeclaredVariable();
            if (variable != null && varName.equals(variable.getName())) {
                addConflict(varName, variable, conflicts);
            }
        }
    }
    if (parent instanceof PsiFile)
        return;
    validateVariableOccurrencesUp(parent.getParent(), parent, conflicts, varName, false);
}
Also used : GrForClause(org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause) PsiFile(com.intellij.psi.PsiFile) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Example 69 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GrChangeSignatureConflictSearcher method addMethodConflicts.

private void addMethodConflicts(MultiMap<PsiElement, String> conflicts) {
    try {
        GrMethod prototype;
        final PsiMethod method = myChangeInfo.getMethod();
        if (!(method instanceof GrMethod))
            return;
        PsiManager manager = method.getManager();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(manager.getProject());
        final CanonicalTypes.Type returnType = myChangeInfo.getNewReturnType();
        String newMethodName = myChangeInfo.getNewName();
        if (method.isConstructor()) {
            prototype = factory.createConstructorFromText("foo", ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", method);
        } else {
            prototype = factory.createMethodFromText("", "foo", returnType != null ? returnType.getTypeText() : null, ArrayUtil.EMPTY_STRING_ARRAY, method);
        }
        prototype.setName(newMethodName);
        JavaParameterInfo[] parameters = myChangeInfo.getNewParameters();
        for (JavaParameterInfo info : parameters) {
            GrParameter param;
            if (info instanceof GrParameterInfo) {
                param = factory.createParameter(info.getName(), info.getTypeText(), ((GrParameterInfo) info).getDefaultInitializer(), (GroovyPsiElement) method);
            } else {
                param = factory.createParameter(info.getName(), info.getTypeText(), (GroovyPsiElement) method);
            }
            prototype.getParameterList().add(param);
        }
        ConflictsUtil.checkMethodConflicts(method.getContainingClass(), method, prototype, conflicts);
        GrMethodConflictUtil.checkMethodConflicts(method.getContainingClass(), prototype, ((GrMethod) method), conflicts, true);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : CanonicalTypes(com.intellij.refactoring.util.CanonicalTypes) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) 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) IncorrectOperationException(com.intellij.util.IncorrectOperationException) JavaParameterInfo(com.intellij.refactoring.changeSignature.JavaParameterInfo)

Example 70 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyOverrideImplementUtil method setupTraitMethodBody.

private static void setupTraitMethodBody(Project project, GrMethod resultMethod, GrTraitMethod traitMethod) {
    PsiClass traitClass = traitMethod.getPrototype().getContainingClass();
    StringBuilder builder = new StringBuilder();
    builder.append("\nreturn ");
    builder.append(traitClass.getQualifiedName());
    builder.append(".super.");
    builder.append(traitMethod.getName());
    builder.append("(");
    GrParameter[] parameters = resultMethod.getParameters();
    for (GrParameter parameter : parameters) {
        builder.append(parameter.getName()).append(",");
    }
    if (parameters.length > 0) {
        builder.replace(builder.length() - 1, builder.length(), ")\n");
    } else {
        builder.append(")\n");
    }
    GroovyFile file = GroovyPsiElementFactory.getInstance(project).createGroovyFile(builder, false, null);
    GrOpenBlock block = resultMethod.getBlock();
    block.getNode().addChildren(file.getFirstChild().getNode(), null, block.getRBrace().getNode());
}
Also used : GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6