Search in sources :

Example 6 with TIntProcedure

use of gnu.trove.TIntProcedure in project intellij-community by JetBrains.

the class TypeProvider method inferMethodParameters.

@NotNull
private PsiType[] inferMethodParameters(@NotNull GrMethod method) {
    PsiType[] psiTypes = inferredTypes.get(method);
    if (psiTypes != null)
        return psiTypes;
    final GrParameter[] parameters = method.getParameters();
    final TIntArrayList paramInds = new TIntArrayList(parameters.length);
    final PsiType[] types = PsiType.createArray(parameters.length);
    for (int i = 0; i < parameters.length; i++) {
        if (parameters[i].getTypeElementGroovy() == null) {
            paramInds.add(i);
        } else {
            types[i] = parameters[i].getType();
        }
    }
    if (!paramInds.isEmpty()) {
        final GrClosureSignature signature = GrClosureSignatureUtil.createSignature(method, PsiSubstitutor.EMPTY);
        MethodReferencesSearch.search(method, true).forEach(psiReference -> {
            final PsiElement element = psiReference.getElement();
            final PsiManager manager = element.getManager();
            final GlobalSearchScope resolveScope = element.getResolveScope();
            if (element instanceof GrReferenceExpression) {
                final GrCall call = (GrCall) element.getParent();
                final GrClosureSignatureUtil.ArgInfo<PsiElement>[] argInfos = GrClosureSignatureUtil.mapParametersToArguments(signature, call);
                if (argInfos == null)
                    return true;
                paramInds.forEach(new TIntProcedure() {

                    @Override
                    public boolean execute(int i) {
                        PsiType type = GrClosureSignatureUtil.getTypeByArg(argInfos[i], manager, resolveScope);
                        types[i] = TypesUtil.getLeastUpperBoundNullable(type, types[i], manager);
                        return true;
                    }
                });
            }
            return true;
        });
    }
    paramInds.forEach(new TIntProcedure() {

        @Override
        public boolean execute(int i) {
            if (types[i] == null || types[i] == PsiType.NULL) {
                types[i] = parameters[i].getType();
            }
            return true;
        }
    });
    inferredTypes.put(method, types);
    return types;
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) GrCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCall) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) TIntArrayList(gnu.trove.TIntArrayList) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GrClosureSignature(org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with TIntProcedure

use of gnu.trove.TIntProcedure in project intellij-community by JetBrains.

the class ResolverTree method reduceCyclicVariables.

private void reduceCyclicVariables() {
    final Set<PsiTypeVariable> nodes = new HashSet<>();
    final Set<Constraint> candidates = new HashSet<>();
    final Map<PsiTypeVariable, Set<PsiTypeVariable>> ins = new HashMap<>();
    final Map<PsiTypeVariable, Set<PsiTypeVariable>> outs = new HashMap<>();
    for (final Constraint constraint : myConstraints) {
        final PsiType left = constraint.getLeft();
        final PsiType right = constraint.getRight();
        if (left instanceof PsiTypeVariable && right instanceof PsiTypeVariable) {
            final PsiTypeVariable leftVar = (PsiTypeVariable) left;
            final PsiTypeVariable rightVar = (PsiTypeVariable) right;
            candidates.add(constraint);
            nodes.add(leftVar);
            nodes.add(rightVar);
            Set<PsiTypeVariable> in = ins.get(leftVar);
            Set<PsiTypeVariable> out = outs.get(rightVar);
            if (in == null) {
                final Set<PsiTypeVariable> newIn = new HashSet<>();
                newIn.add(rightVar);
                ins.put(leftVar, newIn);
            } else {
                in.add(rightVar);
            }
            if (out == null) {
                final Set<PsiTypeVariable> newOut = new HashSet<>();
                newOut.add(leftVar);
                outs.put(rightVar, newOut);
            } else {
                out.add(leftVar);
            }
        }
    }
    final DFSTBuilder<PsiTypeVariable> dfstBuilder = new DFSTBuilder<>(new Graph<PsiTypeVariable>() {

        @Override
        public Collection<PsiTypeVariable> getNodes() {
            return nodes;
        }

        @Override
        public Iterator<PsiTypeVariable> getIn(final PsiTypeVariable n) {
            final Set<PsiTypeVariable> in = ins.get(n);
            if (in == null) {
                return EmptyIterator.getInstance();
            }
            return in.iterator();
        }

        @Override
        public Iterator<PsiTypeVariable> getOut(final PsiTypeVariable n) {
            final Set<PsiTypeVariable> out = outs.get(n);
            if (out == null) {
                return EmptyIterator.getInstance();
            }
            return out.iterator();
        }
    });
    final TIntArrayList sccs = dfstBuilder.getSCCs();
    final Map<PsiTypeVariable, Integer> index = new HashMap<>();
    sccs.forEach(new TIntProcedure() {

        int myTNumber;

        @Override
        public boolean execute(int size) {
            for (int j = 0; j < size; j++) {
                index.put(dfstBuilder.getNodeByTNumber(myTNumber + j), myTNumber);
            }
            myTNumber += size;
            return true;
        }
    });
    for (final Constraint constraint : candidates) {
        if (index.get(constraint.getLeft()).equals(index.get(constraint.getRight()))) {
            myConstraints.remove(constraint);
        }
    }
    Binding binding = myBindingFactory.create();
    for (final PsiTypeVariable fromVar : index.keySet()) {
        final PsiTypeVariable toVar = dfstBuilder.getNodeByNNumber(index.get(fromVar).intValue());
        if (!fromVar.equals(toVar)) {
            binding = binding.compose(myBindingFactory.create(fromVar, toVar));
            if (binding == null) {
                break;
            }
        }
    }
    if (binding != null && binding.nonEmpty()) {
        myCurrentBinding = myCurrentBinding.compose(binding);
        myConstraints = apply(binding);
    }
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint) TObjectIntHashMap(gnu.trove.TObjectIntHashMap) TIntArrayList(gnu.trove.TIntArrayList) Constraint(com.intellij.refactoring.typeCook.deductive.builder.Constraint) EmptyIterator(com.intellij.util.containers.EmptyIterator) DFSTBuilder(com.intellij.util.graph.DFSTBuilder)

Example 8 with TIntProcedure

use of gnu.trove.TIntProcedure in project intellij-community by JetBrains.

the class AddOnDemandStaticImportAction method invoke.

public static void invoke(final Project project, PsiFile file, final Editor editor, PsiElement element) {
    final PsiJavaCodeReferenceElement refExpr = (PsiJavaCodeReferenceElement) element.getParent();
    final PsiClass aClass = (PsiClass) refExpr.resolve();
    if (aClass == null) {
        return;
    }
    final PsiClass containingClass = PsiUtil.getTopLevelClass(refExpr);
    if (aClass != containingClass) {
        PsiImportList importList = ((PsiJavaFile) file).getImportList();
        if (importList == null) {
            return;
        }
        boolean alreadyImported = false;
        for (PsiImportStaticStatement statement : importList.getImportStaticStatements()) {
            if (!statement.isOnDemand())
                continue;
            PsiClass staticResolve = statement.resolveTargetClass();
            if (aClass == staticResolve) {
                alreadyImported = true;
                break;
            }
        }
        if (!alreadyImported) {
            PsiImportStaticStatement importStaticStatement = JavaPsiFacade.getInstance(file.getProject()).getElementFactory().createImportStaticStatement(aClass, "*");
            importList.add(importStaticStatement);
        }
    }
    List<PsiFile> roots = file.getViewProvider().getAllFiles();
    for (final PsiFile root : roots) {
        PsiElement copy = root.copy();
        final PsiManager manager = root.getManager();
        final TIntArrayList expressionToDequalifyOffsets = new TIntArrayList();
        copy.accept(new JavaRecursiveElementWalkingVisitor() {

            int delta;

            @Override
            public void visitReferenceElement(PsiJavaCodeReferenceElement expression) {
                if (isParameterizedReference(expression))
                    return;
                PsiElement qualifierExpression = expression.getQualifier();
                if (qualifierExpression instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                    try {
                        PsiElement resolved = expression.resolve();
                        int end = expression.getTextRange().getEndOffset();
                        qualifierExpression.delete();
                        delta += end - expression.getTextRange().getEndOffset();
                        PsiElement after = expression.resolve();
                        if (manager.areElementsEquivalent(after, resolved)) {
                            expressionToDequalifyOffsets.add(expression.getTextRange().getStartOffset() + delta);
                        }
                    } catch (IncorrectOperationException e) {
                        LOG.error(e);
                    }
                }
                super.visitElement(expression);
            }
        });
        expressionToDequalifyOffsets.forEachDescending(new TIntProcedure() {

            @Override
            public boolean execute(int offset) {
                PsiJavaCodeReferenceElement expression = PsiTreeUtil.findElementOfClassAtOffset(root, offset, PsiJavaCodeReferenceElement.class, false);
                if (expression == null) {
                    return false;
                }
                PsiElement qualifierExpression = expression.getQualifier();
                if (qualifierExpression instanceof PsiJavaCodeReferenceElement && ((PsiJavaCodeReferenceElement) qualifierExpression).isReferenceTo(aClass)) {
                    qualifierExpression.delete();
                    if (editor != null) {
                        HighlightManager.getInstance(project).addRangeHighlight(editor, expression.getTextRange().getStartOffset(), expression.getTextRange().getEndOffset(), EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES), false, null);
                    }
                }
                return true;
            }
        });
    }
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) TIntArrayList(gnu.trove.TIntArrayList) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 9 with TIntProcedure

use of gnu.trove.TIntProcedure in project intellij-community by JetBrains.

the class JavaIntroduceParameterMethodUsagesProcessor method removeParametersFromCall.

private static void removeParametersFromCall(@NotNull final PsiExpressionList argList, TIntArrayList parametersToRemove, PsiMethod method) {
    final int parametersCount = method.getParameterList().getParametersCount();
    final PsiExpression[] exprs = argList.getExpressions();
    parametersToRemove.forEachDescending(new TIntProcedure() {

        public boolean execute(int paramNum) {
            try {
                //parameter was introduced before varargs
                if (method.isVarArgs() && paramNum == parametersCount - 1) {
                    for (int i = paramNum + 1; i < exprs.length; i++) {
                        exprs[i].delete();
                    }
                } else if (paramNum < exprs.length) {
                    exprs[paramNum].delete();
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 10 with TIntProcedure

use of gnu.trove.TIntProcedure in project intellij-community by JetBrains.

the class JavaIntroduceParameterMethodUsagesProcessor method processChangeMethodSignature.

public boolean processChangeMethodSignature(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
    if (!(usage.getElement() instanceof PsiMethod) || !isJavaUsage(usage))
        return true;
    PsiMethod method = (PsiMethod) usage.getElement();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(data.getParameterName(), method.getBody());
    final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(method);
    PsiElementFactory factory = JavaPsiFacade.getInstance(data.getProject()).getElementFactory();
    final PsiClass superClass = data.getMethodToSearchFor().getContainingClass();
    final PsiClass containingClass = method.getContainingClass();
    final PsiSubstitutor substitutor = superClass != null && containingClass != null ? TypeConversionUtil.getSuperClassSubstitutor(superClass, containingClass, PsiSubstitutor.EMPTY) : PsiSubstitutor.EMPTY;
    PsiParameter parameter = factory.createParameter(data.getParameterName(), substitutor.substitute(data.getForcedType()));
    PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, data.isDeclareFinal());
    final PsiParameterList parameterList = method.getParameterList();
    final PsiParameter[] parameters = parameterList.getParameters();
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                PsiDocTag tag = javaDocHelper.getTagForParameter(param);
                if (tag != null) {
                    tag.delete();
                }
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiParameter anchorParameter = getAnchorParameter(method);
    parameter = (PsiParameter) parameterList.addAfter(parameter, anchorParameter);
    JavaCodeStyleManager.getInstance(data.getProject()).shortenClassReferences(parameter);
    final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
    javaDocHelper.addParameterAfter(data.getParameterName(), tagForAnchorParameter);
    fieldConflictsResolver.fix();
    return false;
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) PsiDocTag(com.intellij.psi.javadoc.PsiDocTag) MethodJavaDocHelper(com.intellij.refactoring.util.javadoc.MethodJavaDocHelper) FieldConflictsResolver(com.intellij.refactoring.util.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

TIntProcedure (gnu.trove.TIntProcedure)24 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 TIntArrayList (gnu.trove.TIntArrayList)5 NotNull (org.jetbrains.annotations.NotNull)3 GrClosureSignature (org.jetbrains.plugins.groovy.lang.psi.api.signatures.GrClosureSignature)3 PsiDocTag (com.intellij.psi.javadoc.PsiDocTag)2 MethodJavaDocHelper (com.intellij.refactoring.util.javadoc.MethodJavaDocHelper)2 DFSTBuilder (com.intellij.util.graph.DFSTBuilder)2 TIntHashSet (gnu.trove.TIntHashSet)2 ArrayList (java.util.ArrayList)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)2 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)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 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)2 ConditionalGotoInstruction (com.intellij.codeInspection.dataFlow.instructions.ConditionalGotoInstruction)1 GotoInstruction (com.intellij.codeInspection.dataFlow.instructions.GotoInstruction)1