Search in sources :

Example 1 with GrReflectedMethod

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

the class GroovyMethodInliner method prepareNewMethod.

/**
   * Prepare temporary method with non-conflicting local names
   */
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
    if (method instanceof GrReflectedMethod) {
        method = ((GrReflectedMethod) method).getBaseMethod();
    }
    GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
    if (qualifier != null) {
        Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
        GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
    }
    ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
    collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
    // there are only local variables and parameters (possible of inner closures)
    for (PsiNamedElement namedElement : innerDefinitions) {
        String name = namedElement.getName();
        if (name != null) {
            String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
            if (!newName.equals(namedElement.getName())) {
                final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
                for (PsiReference ref : refs) {
                    PsiElement element = ref.getElement();
                    if (element instanceof GrReferenceExpression) {
                        GrExpression newExpr = factory.createExpressionFromText(newName);
                        ((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
                    }
                }
                namedElement.setName(newName);
            }
        }
    }
    GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
    return newMethod;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) ArrayList(java.util.ArrayList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with GrReflectedMethod

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

the class GrChangeSignatureHandler method invoke.

private static void invoke(PsiMethod method, final Project project) {
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
        return;
    if (method instanceof GrReflectedMethod)
        method = ((GrReflectedMethod) method).getBaseMethod();
    PsiMethod newMethod = SuperMethodWarningUtil.checkSuperMethod(method, RefactoringBundle.message("to.refactor"));
    if (newMethod == null)
        return;
    if (!newMethod.equals(method)) {
        ChangeSignatureUtil.invokeChangeSignatureOn(newMethod, project);
        return;
    }
    if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method))
        return;
    //todo
    if (!(method instanceof GrMethod))
        return;
    final GrChangeSignatureDialog dialog = new GrChangeSignatureDialog(project, new GrMethodDescriptor((GrMethod) method), true, null);
    dialog.show();
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 3 with GrReflectedMethod

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

the class ClosureGenerator method generate.

public void generate(@NotNull GrClosableBlock closure) {
    builder.append("new ");
    TypeWriter.writeTypeForNew(builder, closure.getType(), closure);
    builder.append('(');
    final CharSequence owner = getOwner(closure);
    builder.append(owner);
    builder.append(", ");
    builder.append(owner);
    builder.append(") {\n");
    generateClosureMainMethod(closure);
    final ClassItemGeneratorImpl generator = new ClassItemGeneratorImpl(context);
    final GrMethod method = generateClosureMethod(closure);
    final GrReflectedMethod[] reflectedMethods = method.getReflectedMethods();
    if (reflectedMethods.length > 0) {
        for (GrReflectedMethod reflectedMethod : reflectedMethods) {
            if (reflectedMethod.getSkippedParameters().length > 0) {
                generator.writeMethod(builder, reflectedMethod);
                builder.append('\n');
            }
        }
    }
    builder.append('}');
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 4 with GrReflectedMethod

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

the class XmlMarkupBuilderNonCodeMemberContributor method processDynamicMethods.

@Override
boolean processDynamicMethods(@NotNull PsiType qualifierType, @NotNull PsiClass clazz, @NotNull String name, @NotNull PsiElement place, @NotNull Processor<PsiElement> processor) {
    GrLightMethodBuilder res;
    // ()
    res = createMethod(name, clazz, place);
    if (!processor.process(res))
        return false;
    // (Closure)
    res = createMethod(name, clazz, place);
    res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
    if (!processor.process(res))
        return false;
    // (Object, Closure)
    res = createMethod(name, clazz, place);
    res.addParameter("value", JAVA_LANG_OBJECT, false);
    res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
    if (!processor.process(res))
        return false;
    // (Map, Closure)
    res = createMethod(name, clazz, place);
    res.addParameter("attributes", JAVA_UTIL_MAP, false);
    res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, false).putUserData(DELEGATES_TO_KEY, FQN);
    if (!processor.process(res))
        return false;
    // (Map)
    // (Map, Object)
    // (Map, Object, Closure)
    res = createMethod(name, clazz, place);
    res.addParameter("attributes", JAVA_UTIL_MAP, false);
    res.addParameter("value", JAVA_LANG_OBJECT, true);
    res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, true).putUserData(DELEGATES_TO_KEY, FQN);
    for (GrReflectedMethod method : res.getReflectedMethods()) {
        if (!processor.process(method))
            return false;
    }
    // (Object)
    // (Object, Map)
    // (Object, Map, Closure)
    res = createMethod(name, clazz, place);
    res.addParameter("value", JAVA_LANG_OBJECT, false);
    res.addParameter("attributes", JAVA_UTIL_MAP, true);
    res.addAndGetParameter("body", GROOVY_LANG_CLOSURE, true).putUserData(DELEGATES_TO_KEY, FQN);
    for (GrReflectedMethod method : res.getReflectedMethods()) {
        if (!processor.process(method))
            return false;
    }
    return true;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrLightMethodBuilder(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)

Example 5 with GrReflectedMethod

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

the class GrReflectedMethodImpl method doCreateReflectedMethods.

@NotNull
public static GrReflectedMethod[] doCreateReflectedMethods(@NotNull GrMethod targetMethod, @Nullable PsiClassType categoryType, @NotNull GrParameter[] parameters) {
    int count = 0;
    for (GrParameter parameter : parameters) {
        if (parameter.isOptional())
            count++;
    }
    if (count == 0 && categoryType == null)
        return GrReflectedMethod.EMPTY_ARRAY;
    final GrReflectedMethod[] methods = new GrReflectedMethod[count + 1];
    for (int i = 0; i <= count; i++) {
        methods[i] = new GrReflectedMethodImpl(targetMethod, parameters, count - i, categoryType);
    }
    return methods;
}
Also used : GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrReflectedMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)4 NotNull (org.jetbrains.annotations.NotNull)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)2 GrConstructorInvocation (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation)2 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)2 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)2 GrClosureParameter (org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter)2 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 PsiMethod (com.intellij.psi.PsiMethod)1 LightElement (com.intellij.psi.impl.light.LightElement)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1 GrLightMethodBuilder (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightMethodBuilder)1