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"));
}
}
});
}
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();
}
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;
}
});
}
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;
}
});
}
Aggregations