use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrIntroduceParameterProcessor method generateDelegate.
private void generateDelegate(GrMethod toReplaceIn, PsiMethod toSearchFor, boolean methodsToProcessAreDifferent) {
GroovyIntroduceParameterUtil.generateDelegate(toReplaceIn, myParameterInitializer, myProject);
if (methodsToProcessAreDifferent) {
final GrMethod method = GroovyIntroduceParameterUtil.generateDelegate(toSearchFor, myParameterInitializer, myProject);
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && containingClass.isInterface()) {
final GrOpenBlock block = method.getBlock();
if (block != null) {
block.delete();
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method insertDeclaration.
private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
if (original instanceof GrField) {
final PsiClass containingClass = ((GrField) original).getContainingClass();
LOG.assertTrue(containingClass != null);
return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
}
final GrStatementOwner block;
if (original instanceof PsiParameter) {
final PsiElement container = original.getParent().getParent();
if (container instanceof GrMethod) {
block = ((GrMethod) container).getBlock();
} else if (container instanceof GrClosableBlock) {
block = (GrCodeBlock) container;
} else if (container instanceof GrForStatement) {
final GrStatement body = ((GrForStatement) container).getBody();
if (body instanceof GrBlockStatement) {
block = ((GrBlockStatement) body).getBlock();
} else {
GrBlockStatement blockStatement = myFactory.createBlockStatement();
LOG.assertTrue(blockStatement != null);
if (body != null) {
blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
blockStatement = (GrBlockStatement) body.replace(blockStatement);
} else {
blockStatement = (GrBlockStatement) container.add(blockStatement);
}
block = blockStatement.getBlock();
}
} else {
throw new IncorrectOperationException();
}
LOG.assertTrue(block != null);
return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
}
PsiElement parent = original.getParent();
LOG.assertTrue(parent instanceof GrVariableDeclaration);
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrIfStatement) {
if (((GrIfStatement) pparent).getThenBranch() == parent) {
block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
} else {
block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
}
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else if (pparent instanceof GrLoopStatement) {
block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else {
LOG.assertTrue(pparent instanceof GrStatementOwner);
block = (GrStatementOwner) pparent;
}
return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class MoveGroovyMemberHandler method doMove.
@Override
@NotNull
public PsiMember doMove(@NotNull MoveMembersOptions options, @NotNull PsiMember member, PsiElement anchor, @NotNull PsiClass targetClass) {
GroovyChangeContextUtil.encodeContextInfo(member);
final PsiDocComment docComment;
if (member instanceof PsiDocCommentOwner) {
docComment = ((PsiDocCommentOwner) member).getDocComment();
} else {
docComment = null;
}
PsiMember moved;
if (options.makeEnumConstant() && member instanceof GrVariable && EnumConstantsUtil.isSuitableForEnumConstant(((PsiVariable) member).getType(), targetClass)) {
final GrEnumConstant prototype = createEnumConstant(member.getName(), ((GrVariable) member).getInitializerGroovy(), member.getProject());
moved = (PsiMember) addEnumConstant(targetClass, prototype, anchor);
member.delete();
} else if (member instanceof GrEnumConstant) {
moved = (PsiMember) addEnumConstant(targetClass, (GrEnumConstant) member, null);
} else if (member instanceof GrField) {
if (anchor != null)
anchor = anchor.getParent();
final GrVariableDeclaration parent = (GrVariableDeclaration) member.getParent();
GrVariableDeclaration movedDeclaration = (GrVariableDeclaration) targetClass.addAfter(parent, anchor);
int number = ArrayUtil.find(parent.getMembers(), member);
final GrMember[] members = movedDeclaration.getMembers();
for (int i = 0; i < number; i++) {
members[i].delete();
}
for (int i = number + 1; i < members.length; i++) {
members[i].delete();
}
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = movedDeclaration.getModifierList();
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
}
moved = movedDeclaration.getMembers()[0];
} else if (member instanceof GrMethod) {
moved = (PsiMember) targetClass.addAfter(member, anchor);
if (member.getContainingClass().isInterface() && !targetClass.isInterface()) {
//might need to make modifiers explicit, see IDEADEV-11416
final PsiModifierList list = moved.getModifierList();
assert list != null;
list.setModifierProperty(PsiModifier.STATIC, member.hasModifierProperty(PsiModifier.STATIC));
list.setModifierProperty(PsiModifier.FINAL, member.hasModifierProperty(PsiModifier.FINAL));
VisibilityUtil.setVisibility(list, VisibilityUtil.getVisibilityModifier(member.getModifierList()));
}
} else {
moved = (PsiMember) targetClass.addAfter(member, anchor);
}
if (docComment != null) {
PsiElement insertedDocComment = targetClass.addBefore(docComment, moved);
PsiElement prevSibling = insertedDocComment.getPrevSibling();
addLineFeedIfNeeded(prevSibling);
docComment.delete();
}
member.delete();
return moved;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod 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.api.statements.typedef.members.GrMethod in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method processChangeMethodSignature.
@Override
public boolean processChangeMethodSignature(IntroduceParameterData data, UsageInfo usage, UsageInfo[] usages) throws IncorrectOperationException {
if (!(usage.getElement() instanceof GrMethod) || !isGroovyUsage(usage))
return true;
GrMethod method = (GrMethod) usage.getElement();
final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(data.getParameterName(), method.getBlock());
final MethodJavaDocHelper javaDocHelper = new MethodJavaDocHelper(method);
final PsiParameter[] parameters = method.getParameterList().getParameters();
data.getParametersToRemove().forEachDescending(new TIntProcedure() {
@Override
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;
}
});
addParameter(method, javaDocHelper, data.getForcedType(), data.getParameterName(), data.isDeclareFinal(), data.getProject());
fieldConflictsResolver.fix();
return false;
}
Aggregations