use of org.jetbrains.plugins.groovy.refactoring.introduce.parameter.java2groovy.OldReferencesResolver in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method processExternalUsage.
private static void processExternalUsage(UsageInfo usage, GrIntroduceParameterSettings settings, PsiElement expression) {
final PsiElement element = usage.getElement();
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (callExpression == null) {
final PsiElement parent = element.getParent();
if (parent instanceof GrReferenceExpression && element == ((GrReferenceExpression) parent).getQualifier() && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(parent);
}
}
if (callExpression == null)
return;
//check for x.getFoo()(args)
if (callExpression instanceof GrMethodCall) {
final GrExpression invoked = ((GrMethodCall) callExpression).getInvokedExpression();
if (invoked instanceof GrReferenceExpression) {
final GroovyResolveResult result = ((GrReferenceExpression) invoked).advancedResolve();
final PsiElement resolved = result.getElement();
if (resolved instanceof GrAccessorMethod && !result.isInvokedOnProperty()) {
PsiElement actualCallExpression = callExpression.getParent();
if (actualCallExpression instanceof GrCall) {
callExpression = (GrCall) actualCallExpression;
}
}
}
}
GrArgumentList argList = callExpression.getArgumentList();
LOG.assertTrue(argList != null);
GrExpression[] oldArgs = argList.getExpressionArguments();
GrClosableBlock toReplaceIn = (GrClosableBlock) settings.getToReplaceIn();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
final GrExpression anchor = getAnchorForArgument(oldArgs, toReplaceIn.isVarArgs(), toReplaceIn.getParameterList());
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(toReplaceIn);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, true, true);
if (PsiTreeUtil.isAncestor(toReplaceIn, callExpression, false)) {
argList.addAfter(factory.createExpressionFromText(settings.getName()), anchor);
} else {
PsiElement initializer = ExpressionConverter.getExpression(expression, GroovyLanguage.INSTANCE, settings.getProject());
LOG.assertTrue(initializer instanceof GrExpression);
GrExpression newArg = GroovyIntroduceParameterUtil.addClosureToCall(initializer, argList);
if (newArg == null) {
final PsiElement dummy = argList.addAfter(factory.createExpressionFromText("1"), anchor);
newArg = ((GrExpression) dummy).replaceWithExpression((GrExpression) initializer, true);
}
new OldReferencesResolver(callExpression, newArg, toReplaceIn, settings.replaceFieldsWithGetters(), initializer, signature, actualArgs, toReplaceIn.getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newarg can be replaced by OldReferenceResolve
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(settings.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
GroovyIntroduceParameterUtil.removeParamsFromUnresolvedCall(callExpression, toReplaceIn.getParameters(), settings.parametersToRemove());
} else {
GroovyIntroduceParameterUtil.removeParametersFromCall(actualArgs, settings.parametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
}
Aggregations