use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodUsage.
@Override
public boolean processChangeMethodUsage(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
GrCall callExpression = GroovyRefactoringUtil.getCallExpressionByMethodReference(usage.getElement());
if (callExpression == null)
return true;
GrArgumentList argList = callExpression.getArgumentList();
GrExpression[] oldArgs = argList.getExpressionArguments();
final GrExpression anchor;
if (!data.getMethodToSearchFor().isVarArgs()) {
anchor = getLast(oldArgs);
} else {
final PsiParameter[] parameters = data.getMethodToSearchFor().getParameterList().getParameters();
if (parameters.length > oldArgs.length) {
anchor = getLast(oldArgs);
} else {
final int lastNonVararg = parameters.length - 2;
anchor = lastNonVararg >= 0 ? oldArgs[lastNonVararg] : null;
}
}
PsiMethod method = PsiTreeUtil.getParentOfType(argList, PsiMethod.class);
GrClosureSignature signature = GrClosureSignatureUtil.createSignature(callExpression);
if (signature == null)
signature = GrClosureSignatureUtil.createSignature(data.getMethodToSearchFor(), PsiSubstitutor.EMPTY);
final GrClosureSignatureUtil.ArgInfo<PsiElement>[] actualArgs = GrClosureSignatureUtil.mapParametersToArguments(signature, callExpression.getNamedArguments(), callExpression.getExpressionArguments(), callExpression.getClosureArguments(), callExpression, false, true);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(data.getProject());
if (method != null && IntroduceParameterUtil.isMethodInUsages(data, method, usages)) {
argList.addAfter(factory.createExpressionFromText(data.getParameterName()), anchor);
} else {
final PsiElement _expr = data.getParameterInitializer().getExpression();
PsiElement initializer = ExpressionConverter.getExpression(_expr, GroovyLanguage.INSTANCE, data.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);
}
final PsiMethod methodToReplaceIn = data.getMethodToReplaceIn();
new OldReferencesResolver(callExpression, newArg, methodToReplaceIn, data.getReplaceFieldsWithGetters(), initializer, signature, actualArgs, methodToReplaceIn.getParameterList().getParameters()).resolve();
ChangeContextUtil.clearContextInfo(initializer);
//newArg can be replaced by OldReferenceResolver
if (newArg.isValid()) {
JavaCodeStyleManager.getInstance(newArg.getProject()).shortenClassReferences(newArg);
CodeStyleManager.getInstance(data.getProject()).reformat(newArg);
}
}
if (actualArgs == null) {
removeParamsFromUnresolvedCall(callExpression, data);
} else {
removeParametersFromCall(actualArgs, data.getParametersToRemove());
}
if (argList.getAllArguments().length == 0 && PsiImplUtil.hasClosureArguments(callExpression)) {
final GrArgumentList emptyArgList = ((GrMethodCallExpression) factory.createExpressionFromText("foo{}")).getArgumentList();
argList.replace(emptyArgList);
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GrIntroduceLocalVariableProcessor method resolveLocalConflicts.
private static void resolveLocalConflicts(@NotNull PsiElement tempContainer, @NotNull String varName) {
for (PsiElement child : tempContainer.getChildren()) {
if (child instanceof GrReferenceExpression && !child.getText().contains(".")) {
PsiReference psiReference = child.getReference();
if (psiReference != null) {
final PsiElement resolved = psiReference.resolve();
if (resolved != null) {
String fieldName = getFieldName(resolved);
if (fieldName != null && varName.equals(fieldName)) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tempContainer.getProject());
((GrReferenceExpression) child).replaceWithExpression(factory.createExpressionFromText("this." + child.getText()), true);
}
}
}
} else {
resolveLocalConflicts(child, varName);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyOverrideImplementUtil method setupAnnotations.
private static void setupAnnotations(@NotNull GrTypeDefinition aClass, @NotNull PsiMethod method, @NotNull GrMethod result) {
if (OverrideImplementUtil.isInsertOverride(method, aClass)) {
result.getModifierList().addAnnotation(CommonClassNames.JAVA_LANG_OVERRIDE);
}
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
final PsiParameter[] originalParams = method.getParameterList().getParameters();
GrParameter[] parameters = result.getParameters();
for (int i = 0; i < parameters.length; i++) {
GrParameter parameter = parameters[i];
PsiParameter original = originalParams[i];
for (PsiAnnotation annotation : original.getModifierList().getAnnotations()) {
final GrModifierList modifierList = parameter.getModifierList();
String qname = annotation.getQualifiedName();
if (qname != null && modifierList.findAnnotation(qname) == null) {
if (annotation instanceof GrAnnotation) {
modifierList.add(annotation);
} else {
modifierList.add(factory.createAnnotationFromText(annotation.getText()));
}
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class TryCatchFinallySurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrTryCatchStatement tryStatement = (GrTryCatchStatement) factory.createStatementFromText("try {\n} catch(exception e){\n} finally{\n}", context);
addStatements(tryStatement.getTryBlock(), elements);
return tryStatement;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class WhileSurrounder method doSurroundElements.
@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrWhileStatement whileStatement = (GrWhileStatement) factory.createStatementFromText("while(a){\n}", context);
addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
return whileStatement;
}
Aggregations