Search in sources :

Example 6 with GrReflectedMethod

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

the class GroovyResolverProcessorImpl method collapseReflectedMethods.

private static List<GroovyResolveResult> collapseReflectedMethods(Collection<GroovyResolveResult> candidates) {
    Set<GrMethod> visited = ContainerUtil.newHashSet();
    List<GroovyResolveResult> collapsed = ContainerUtil.newArrayList();
    for (GroovyResolveResult result : candidates) {
        PsiElement element = result.getElement();
        if (element instanceof GrReflectedMethod) {
            GrMethod baseMethod = ((GrReflectedMethod) element).getBaseMethod();
            if (visited.add(baseMethod)) {
                collapsed.add(PsiImplUtil.reflectedToBase(result, baseMethod, (GrReflectedMethod) element));
            }
        } else {
            collapsed.add(result);
        }
    }
    return collapsed;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) 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 7 with GrReflectedMethod

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

the class GrUnusedDefaultParameterInspection method isInitializerUnused.

/**
   * Consider following method:
   * <pre>
   *   def foo(a = 1, b = 2, c = 3) {}
   * </pre>
   * Its reflected methods:
   * <pre>
   *   def foo(a, b, c) {}
   *   def foo(a, b) {}
   *   def foo(a) {}
   *   def foo() {}
   * </pre>
   * Initializer for '<code>a</code>' is used only when <code>foo</code> called without arguments,
   * we do not care if <code>foo</code> is called with one, two ot three arguments.
   * <p>
   * In case of <code>b</code> we search <code>foo()</code> or <code>foo(1)</code> calls.
   * <p>
   * The general idea: search usages of last N reflected methods where N is number of current parameter among other default parameters.
   */
private static boolean isInitializerUnused(@NotNull GrParameter parameter, @NotNull GrMethod method) {
    int optionalParameterNumber = 0;
    for (GrParameter someParameter : method.getParameters()) {
        if (someParameter.isOptional())
            optionalParameterNumber++;
        if (someParameter == parameter)
            break;
    }
    GrReflectedMethod[] reflectedMethods = method.getReflectedMethods();
    for (int i = reflectedMethods.length - optionalParameterNumber; i < reflectedMethods.length; i++) {
        GrReflectedMethod reflectedMethod = reflectedMethods[i];
        if (FindSuperElementsHelper.findSuperElements(reflectedMethod).length > 0)
            return false;
        if (MethodReferencesSearch.search(reflectedMethod).findFirst() != null)
            return false;
    }
    return true;
}
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)

Example 8 with GrReflectedMethod

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

the class GroovyParameterInfoHandler method updateUI.

@Override
public void updateUI(Object o, @NotNull ParameterInfoUIContext context) {
    CodeInsightSettings settings = CodeInsightSettings.getInstance();
    if (o == null)
        return;
    Object element;
    if (o instanceof GroovyResolveResult) {
        element = ((GroovyResolveResult) o).getElement();
        if (element == null || !((PsiElement) element).isValid()) {
            context.setUIComponentEnabled(false);
            return;
        }
    } else if (o instanceof GrClosureSignature) {
        if (!((GrClosureSignature) o).isValid()) {
            context.setUIComponentEnabled(false);
            return;
        }
        element = o;
    } else {
        return;
    }
    int highlightStartOffset = -1;
    int highlightEndOffset = -1;
    final int currentParameter = context.getCurrentParameterIndex();
    StringBuilder buffer = new StringBuilder();
    if (element instanceof PsiMethod) {
        PsiMethod method = (PsiMethod) element;
        if (method instanceof GrReflectedMethod)
            method = ((GrReflectedMethod) method).getBaseMethod();
        if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
            if (!method.isConstructor()) {
                PsiType returnType = PsiUtil.getSmartReturnType(method);
                if (returnType != null) {
                    buffer.append(returnType.getPresentableText());
                    buffer.append(' ');
                }
            }
            buffer.append(method.getName());
            buffer.append('(');
        }
        PsiParameter[] params = method.getParameterList().getParameters();
        params = updateConstructorParams(method, params, context.getParameterOwner());
        int numParams = params.length;
        if (numParams > 0) {
            LOG.assertTrue(o instanceof GroovyResolveResult, o.getClass());
            final PsiSubstitutor substitutor = ((GroovyResolveResult) o).getSubstitutor();
            for (int j = 0; j < numParams; j++) {
                PsiParameter param = params[j];
                int startOffset = buffer.length();
                appendParameterText(param, substitutor, buffer);
                int endOffset = buffer.length();
                if (j < numParams - 1) {
                    buffer.append(", ");
                }
                if (context.isUIComponentEnabled() && (j == currentParameter || (j == numParams - 1 && param.isVarArgs() && currentParameter >= numParams))) {
                    highlightStartOffset = startOffset;
                    highlightEndOffset = endOffset;
                }
            }
        } else {
            buffer.append("no parameters");
        }
        if (settings.SHOW_FULL_SIGNATURES_IN_PARAMETER_INFO) {
            buffer.append(")");
        }
    } else if (element instanceof PsiClass) {
        buffer.append("no parameters");
    } else if (element instanceof GrClosureSignature) {
        GrClosureParameter[] parameters = ((GrClosureSignature) element).getParameters();
        if (parameters.length > 0) {
            for (int i = 0; i < parameters.length; i++) {
                if (i > 0)
                    buffer.append(", ");
                int startOffset = buffer.length();
                final PsiType psiType = parameters[i].getType();
                if (psiType == null) {
                    buffer.append("def");
                } else {
                    buffer.append(psiType.getPresentableText());
                }
                buffer.append(' ').append(parameters[i].getName() != null ? parameters[i].getName() : "<unknown>");
                int endOffset = buffer.length();
                if (context.isUIComponentEnabled() && (i == currentParameter || (i == parameters.length - 1 && ((GrClosureSignature) element).isVarargs() && currentParameter >= parameters.length))) {
                    highlightStartOffset = startOffset;
                    highlightEndOffset = endOffset;
                }
                final GrExpression initializer = parameters[i].getDefaultInitializer();
                if (initializer != null) {
                    buffer.append(" = ").append(initializer.getText());
                }
            }
        } else {
            buffer.append("no parameters");
        }
    }
    final boolean isDeprecated = o instanceof PsiDocCommentOwner && ((PsiDocCommentOwner) o).isDeprecated();
    context.setupUIComponentPresentation(buffer.toString(), highlightStartOffset, highlightEndOffset, !context.isUIComponentEnabled(), isDeprecated, false, context.getDefaultParameterColor());
}
Also used : CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 9 with GrReflectedMethod

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

the class GroovyParameterInfoHandler method updateParameterInfo.

@Override
public void updateParameterInfo(@NotNull GroovyPsiElement place, @NotNull UpdateParameterInfoContext context) {
    final PsiElement parameterOwner = context.getParameterOwner();
    if (parameterOwner != place) {
        context.removeHint();
        return;
    }
    int offset = context.getEditor().getCaretModel().getOffset();
    offset = CharArrayUtil.shiftForward(context.getEditor().getDocument().getText(), offset, " \t\n");
    final int currIndex = getCurrentParameterIndex(place, offset);
    context.setCurrentParameter(currIndex);
    final Object[] objects = context.getObjectsToView();
    Outer: for (int i = 0; i < objects.length; i++) {
        PsiType[] parameterTypes = null;
        PsiType[] argTypes = null;
        PsiSubstitutor substitutor = null;
        if (objects[i] instanceof GroovyResolveResult) {
            final GroovyResolveResult resolveResult = (GroovyResolveResult) objects[i];
            PsiNamedElement namedElement = (PsiNamedElement) resolveResult.getElement();
            if (namedElement instanceof GrReflectedMethod)
                namedElement = ((GrReflectedMethod) namedElement).getBaseMethod();
            substitutor = resolveResult.getSubstitutor();
            assert namedElement != null;
            if (!namedElement.isValid()) {
                context.setUIComponentEnabled(i, false);
                continue Outer;
            }
            if (namedElement instanceof PsiMethod) {
                final PsiMethod method = (PsiMethod) namedElement;
                PsiParameter[] parameters = method.getParameterList().getParameters();
                parameters = updateConstructorParams(method, parameters, context.getParameterOwner());
                parameterTypes = PsiType.createArray(parameters.length);
                for (int j = 0; j < parameters.length; j++) {
                    parameterTypes[j] = parameters[j].getType();
                }
                argTypes = PsiUtil.getArgumentTypes(place, false);
            }
            if (argTypes == null)
                continue;
        } else if (objects[i] instanceof GrClosureSignature) {
            final GrClosureSignature signature = (GrClosureSignature) objects[i];
            argTypes = PsiUtil.getArgumentTypes(place, false);
            parameterTypes = PsiType.createArray(signature.getParameterCount());
            int j = 0;
            for (GrClosureParameter parameter : signature.getParameters()) {
                parameterTypes[j++] = parameter.getType();
            }
        } else {
            continue Outer;
        }
        assert argTypes != null;
        if (argTypes.length > currIndex) {
            if (parameterTypes.length <= currIndex) {
                context.setUIComponentEnabled(i, false);
                continue;
            } else {
                for (int j = 0; j < currIndex; j++) {
                    PsiType argType = argTypes[j];
                    final PsiType paramType = substitutor != null ? substitutor.substitute(parameterTypes[j]) : parameterTypes[j];
                    if (!TypesUtil.isAssignableByMethodCallConversion(paramType, argType, place)) {
                        context.setUIComponentEnabled(i, false);
                        break Outer;
                    }
                }
            }
        }
        context.setUIComponentEnabled(i, true);
    }
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) GrClosureParameter(org.jetbrains.plugins.groovy.lang.psi.api.types.GrClosureParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 10 with GrReflectedMethod

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

the class PsiImplUtil method getChainingConstructorInvocation.

@Nullable
public static GrConstructorInvocation getChainingConstructorInvocation(GrMethod constructor) {
    if (constructor instanceof GrReflectedMethod && ((GrReflectedMethod) constructor).getSkippedParameters().length > 0)
        return null;
    LOG.assertTrue(constructor.isConstructor());
    GrOpenBlock body = constructor.getBlock();
    if (body == null)
        return null;
    GrStatement[] statements = body.getStatements();
    if (statements.length > 0 && statements[0] instanceof GrConstructorInvocation) {
        return (GrConstructorInvocation) statements[0];
    }
    return null;
}
Also used : GrConstructorInvocation(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrConstructorInvocation) GrReflectedMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrReflectedMethod) GrOpenBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) Nullable(org.jetbrains.annotations.Nullable)

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