use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceHandlerBase method insertExplicitCastIfNeeded.
public static GrExpression insertExplicitCastIfNeeded(GrVariable variable, GrExpression initializer) {
PsiType ltype = findLValueType(initializer);
PsiType rtype = initializer.getType();
GrExpression rawExpr = (GrExpression) PsiUtil.skipParentheses(initializer, false);
if (ltype == null || TypesUtil.isAssignableWithoutConversions(ltype, rtype, initializer) || !TypesUtil.isAssignable(ltype, rtype, initializer)) {
return rawExpr;
} else {
// implicit coercion should be replaced with explicit cast
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(variable.getProject());
GrSafeCastExpression cast = (GrSafeCastExpression) factory.createExpressionFromText("a as B");
cast.getOperand().replaceWithExpression(rawExpr, false);
cast.getCastTypeElement().replace(factory.createTypeElement(ltype));
return cast;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory 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 org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method processChangedMethodCall.
private static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings) {
if (element.getParent() instanceof GrMethodCallExpression) {
GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(settings.getProject());
GrExpression expression = factory.createExpressionFromText(settings.getName(), null);
final GrArgumentList argList = methodCall.getArgumentList();
final PsiElement[] exprs = argList.getAllArguments();
if (exprs.length > 0) {
argList.addAfter(expression, exprs[exprs.length - 1]);
} else {
argList.add(expression);
}
removeParametersFromCall(methodCall, settings);
} else {
LOG.error(element.getParent());
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceParameterProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
//PsiType initializerType = mySettings.getSelectedType();
// Changing external occurrences (the tricky part)
IntroduceParameterUtil.processUsages(usages, this);
final GrMethod toReplaceIn = (GrMethod) mySettings.getToReplaceIn();
final PsiMethod toSearchFor = (PsiMethod) mySettings.getToSearchFor();
final boolean methodsToProcessAreDifferent = toReplaceIn != toSearchFor;
if (mySettings.generateDelegate()) {
generateDelegate(toReplaceIn, toSearchFor, methodsToProcessAreDifferent);
}
// Changing signature of initial method
// (signature of myMethodToReplaceIn will be either changed now or have already been changed)
//LOG.assertTrue(initializerType == null || initializerType.isValid());
final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(mySettings.getName(), toReplaceIn.getBlock());
processMethodSignature(usages, toReplaceIn, toSearchFor, methodsToProcessAreDifferent);
processUsages(usages, factory);
processStringPart();
processVar();
fieldConflictsResolver.fix();
}
Aggregations