use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class MoveGroovyMemberHandler method createEnumConstant.
private static GrEnumConstant createEnumConstant(String constantName, GrExpression initializerExpr, Project project) throws IncorrectOperationException {
final GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
final String enumConstantText = initializerExpr != null ? constantName + "(" + initializerExpr.getText() + ")" : constantName;
return elementFactory.createEnumConstantFromText(enumConstantText);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class RenameAliasImportedMethodProcessor method renameElement.
@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
propertyAccess.add(usage);
} else {
methodAccess.add(usage);
}
}
super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
final String propertyName;
if (isGetter) {
propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
} else if (isSetter) {
propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
} else {
propertyName = null;
}
if (propertyName == null) {
//it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
for (UsageInfo info : propertyAccess) {
final PsiElement element = info.getElement();
if (element instanceof GrReferenceExpression) {
final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
if (isGetter) {
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
((GrReferenceExpression) element).replaceWithExpression(call, true);
} else {
final PsiElement parent = element.getParent();
assert parent instanceof GrAssignmentExpression;
final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
((GrAssignmentExpression) parent).replaceWithExpression(call, true);
}
}
}
} else {
for (UsageInfo usage : propertyAccess) {
final PsiReference ref = usage.getReference();
if (ref != null) {
ref.handleElementRename(propertyName);
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method processChangedMethodCall.
public static void processChangedMethodCall(PsiElement element, GrIntroduceParameterSettings settings, Project project) {
if (!(element.getParent() instanceof GrMethodCallExpression)) {
LOG.error(element.getParent());
return;
}
GrMethodCallExpression methodCall = (GrMethodCallExpression) element.getParent();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String name = settings.getName();
LOG.assertTrue(name != null);
GrExpression expression = factory.createExpressionFromText(name, 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);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method generateDelegate.
public static GrMethod generateDelegate(PsiMethod prototype, IntroduceParameterData.ExpressionWrapper initializer, Project project) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrMethod result;
if (prototype instanceof GrMethod) {
result = (GrMethod) prototype.copy();
} else {
StringBuilder builder = new StringBuilder();
builder.append(prototype.getModifierList().getText()).append(' ');
if (prototype.getReturnTypeElement() != null) {
builder.append(prototype.getReturnTypeElement().getText());
}
builder.append(' ').append(prototype.getName());
builder.append(prototype.getParameterList().getText());
builder.append("{}");
result = factory.createMethodFromText(builder.toString());
}
StringBuilder call = new StringBuilder();
call.append("def foo(){\n");
final GrParameter[] parameters = result.getParameters();
call.append(prototype.getName());
if (initializer.getExpression() instanceof GrClosableBlock) {
if (parameters.length > 0) {
call.append('(');
for (GrParameter parameter : parameters) {
call.append(parameter.getName()).append(", ");
}
call.replace(call.length() - 2, call.length(), ")");
}
call.append(initializer.getText());
} else {
call.append('(');
for (GrParameter parameter : parameters) {
call.append(parameter.getName()).append(", ");
}
call.append(initializer.getText());
call.append(")");
}
call.append("\n}");
final GrOpenBlock block = factory.createMethodFromText(call.toString()).getBlock();
result.getBlock().replace(block);
final PsiElement parent = prototype.getParent();
final GrMethod method = (GrMethod) parent.addBefore(result, prototype);
JavaCodeStyleManager.getInstance(method.getProject()).shortenClassReferences(method);
return method;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method addParameter.
@NotNull
public static GrParameter addParameter(@NotNull GrParametersOwner parametersOwner, @Nullable MethodJavaDocHelper javaDocHelper, @NotNull PsiType forcedType, @NotNull String parameterName, boolean isFinal, @NotNull Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String typeText = forcedType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || forcedType == PsiType.NULL || PsiType.VOID.equals(forcedType) ? null : forcedType.getCanonicalText();
GrParameter parameter = factory.createParameter(parameterName, typeText, parametersOwner);
parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, isFinal);
final PsiParameter anchorParameter = getAnchorParameter(parametersOwner);
final GrParameterList parameterList = parametersOwner.getParameterList();
parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
if (javaDocHelper != null) {
final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
javaDocHelper.addParameterAfter(parameterName, tagForAnchorParameter);
}
return parameter;
}
Aggregations