Search in sources :

Example 21 with TIntProcedure

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

the class UnusedDefInspection method check.

@Override
protected void check(@NotNull final GrControlFlowOwner owner, @NotNull final ProblemsHolder problemsHolder) {
    final Instruction[] flow = owner.getControlFlow();
    final ReachingDefinitionsDfaInstance dfaInstance = new ReachingDefinitionsDfaInstance(flow);
    final ReachingDefinitionsSemilattice lattice = new ReachingDefinitionsSemilattice();
    final DFAEngine<DefinitionMap> engine = new DFAEngine<>(flow, dfaInstance, lattice);
    final List<DefinitionMap> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null) {
        return;
    }
    final TIntHashSet unusedDefs = new TIntHashSet();
    for (Instruction instruction : flow) {
        if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
            unusedDefs.add(instruction.num());
        }
    }
    for (int i = 0; i < dfaResult.size(); i++) {
        final Instruction instruction = flow[i];
        if (instruction instanceof ReadWriteVariableInstruction) {
            final ReadWriteVariableInstruction varInst = (ReadWriteVariableInstruction) instruction;
            if (!varInst.isWrite()) {
                final String varName = varInst.getVariableName();
                DefinitionMap e = dfaResult.get(i);
                e.forEachValue(new TObjectProcedure<TIntHashSet>() {

                    @Override
                    public boolean execute(TIntHashSet reaching) {
                        reaching.forEach(new TIntProcedure() {

                            @Override
                            public boolean execute(int defNum) {
                                final String defName = ((ReadWriteVariableInstruction) flow[defNum]).getVariableName();
                                if (varName.equals(defName)) {
                                    unusedDefs.remove(defNum);
                                }
                                return true;
                            }
                        });
                        return true;
                    }
                });
            }
        }
    }
    final Set<PsiElement> checked = ContainerUtil.newHashSet();
    unusedDefs.forEach(new TIntProcedure() {

        @Override
        public boolean execute(int num) {
            final ReadWriteVariableInstruction instruction = (ReadWriteVariableInstruction) flow[num];
            final PsiElement element = instruction.getElement();
            process(element, checked, problemsHolder, GroovyInspectionBundle.message("unused.assignment.tooltip"));
            return true;
        }
    });
    owner.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitVariable(@NotNull GrVariable variable) {
            if (checked.contains(variable) || variable.getInitializerGroovy() != null)
                return;
            if (ReferencesSearch.search(variable, variable.getUseScope()).findFirst() == null) {
                process(variable, checked, problemsHolder, GroovyInspectionBundle.message("unused.variable"));
            }
        }
    });
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) TIntProcedure(gnu.trove.TIntProcedure) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) ReachingDefinitionsDfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsDfaInstance) TIntHashSet(gnu.trove.TIntHashSet) DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReachingDefinitionsSemilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsSemilattice) DefinitionMap(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.DefinitionMap) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 22 with TIntProcedure

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

the class GrIntroduceClosureParameterProcessor method changeSignature.

private static void changeSignature(GrClosableBlock block, GrIntroduceParameterSettings settings) {
    final String name = settings.getName();
    final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(name, block);
    final GrParameter[] parameters = block.getParameters();
    settings.parametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(final int paramNum) {
            try {
                PsiParameter param = parameters[paramNum];
                param.delete();
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
    final PsiType type = settings.getSelectedType();
    final String typeText = type == null ? null : type.getCanonicalText();
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
    GrParameter parameter = factory.createParameter(name, typeText, block);
    parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, settings.declareFinal());
    final GrParameterList parameterList = block.getParameterList();
    final PsiParameter anchorParameter = GroovyIntroduceParameterUtil.getAnchorParameter(parameterList, block.isVarArgs());
    parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
    if (block.getArrow() == null) {
        final PsiElement arrow = block.addAfter(factory.createClosureFromText("{->}").getArrow().copy(), parameterList);
        final PsiElement child = block.getFirstChild().getNextSibling();
        if (PsiImplUtil.isWhiteSpaceOrNls(child)) {
            final String text = child.getText();
            child.delete();
            block.addAfter(factory.createLineTerminator(text), arrow);
        }
    }
    JavaCodeStyleManager.getInstance(parameter.getProject()).shortenClassReferences(parameter);
    fieldConflictsResolver.fix();
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) TIntProcedure(gnu.trove.TIntProcedure) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) FieldConflictsResolver(org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.FieldConflictsResolver) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 23 with TIntProcedure

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

the class GroovyIntroduceParameterUtil method removeParamsFromUnresolvedCall.

public static void removeParamsFromUnresolvedCall(GrCall callExpression, PsiParameter[] parameters, TIntArrayList parametersToRemove) {
    final GrExpression[] arguments = callExpression.getExpressionArguments();
    final GrClosableBlock[] closureArguments = callExpression.getClosureArguments();
    final GrNamedArgument[] namedArguments = callExpression.getNamedArguments();
    final boolean hasNamedArgs;
    if (namedArguments.length > 0) {
        if (parameters.length > 0) {
            final PsiType type = parameters[0].getType();
            hasNamedArgs = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP);
        } else {
            hasNamedArgs = false;
        }
    } else {
        hasNamedArgs = false;
    }
    parametersToRemove.forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(int paramNum) {
            try {
                if (paramNum == 0 && hasNamedArgs) {
                    for (GrNamedArgument namedArgument : namedArguments) {
                        namedArgument.delete();
                    }
                } else {
                    if (hasNamedArgs)
                        paramNum--;
                    if (paramNum < arguments.length) {
                        arguments[paramNum].delete();
                    } else if (paramNum < arguments.length + closureArguments.length) {
                        closureArguments[paramNum - arguments.length].delete();
                    }
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) TIntProcedure(gnu.trove.TIntProcedure) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 24 with TIntProcedure

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

the class GroovyIntroduceParameterMethodUsagesProcessor method removeParamsFromUnresolvedCall.

private static void removeParamsFromUnresolvedCall(GrCall callExpression, IntroduceParameterData data) {
    final GrExpression[] arguments = callExpression.getExpressionArguments();
    final GrClosableBlock[] closureArguments = callExpression.getClosureArguments();
    final GrNamedArgument[] namedArguments = callExpression.getNamedArguments();
    final boolean hasNamedArgs;
    if (namedArguments.length > 0) {
        final PsiMethod method = data.getMethodToSearchFor();
        final PsiParameter[] parameters = method.getParameterList().getParameters();
        if (parameters.length > 0) {
            final PsiType type = parameters[0].getType();
            hasNamedArgs = InheritanceUtil.isInheritor(type, CommonClassNames.JAVA_UTIL_MAP);
        } else {
            hasNamedArgs = false;
        }
    } else {
        hasNamedArgs = false;
    }
    data.getParametersToRemove().forEachDescending(new TIntProcedure() {

        @Override
        public boolean execute(int paramNum) {
            try {
                if (paramNum == 0 && hasNamedArgs) {
                    for (GrNamedArgument namedArgument : namedArguments) {
                        namedArgument.delete();
                    }
                } else {
                    if (hasNamedArgs)
                        paramNum--;
                    if (paramNum < arguments.length) {
                        arguments[paramNum].delete();
                    } else if (paramNum < arguments.length + closureArguments.length) {
                        closureArguments[paramNum - arguments.length].delete();
                    }
                }
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
            return true;
        }
    });
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) TIntProcedure(gnu.trove.TIntProcedure) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) 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