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