use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrCreateFieldForParameterIntention method performRefactoring.
@Override
protected void performRefactoring(Project project, PsiClass targetClass, PsiMethod method, PsiParameter myParameter, PsiType type, String fieldName, boolean methodStatic, boolean isFinal) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
if (targetClass.findFieldByName(fieldName, false) == null) {
String[] modifiers = getModifiers(methodStatic, isFinal);
GrVariableDeclaration fieldDeclaration = factory.createFieldDeclaration(modifiers, fieldName, null, type);
GrVariableDeclaration inserted = (GrVariableDeclaration) targetClass.add(fieldDeclaration);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(inserted);
}
GrOpenBlock block = ((GrMethod) method).getBlock();
if (block == null)
return;
GrAssignmentExpression assignment = createAssignment(targetClass, myParameter, fieldName, methodStatic, factory);
GrStatement anchor = getAnchor(block);
GrStatement statement = block.addStatementBefore(assignment, anchor);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(statement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrCreateFieldForParameterIntention method checkAssignmentToFieldExists.
private static boolean checkAssignmentToFieldExists(PsiParameter parameter) {
for (PsiReference reference : ReferencesSearch.search(parameter).findAll()) {
PsiElement element = reference.getElement();
if (element instanceof GrReferenceExpression && element.getParent() instanceof GrAssignmentExpression && ((GrAssignmentExpression) element.getParent()).getRValue() == element) {
GrAssignmentExpression parent = ((GrAssignmentExpression) element.getParent());
GrExpression value = parent.getLValue();
if (value instanceof GrReferenceExpression && ((GrReferenceExpression) value).resolve() instanceof PsiField)
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrVariableInliner method getConflicts.
@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
GrExpression expr = (GrExpression) reference.getElement();
if (expr.getParent() instanceof GrAssignmentExpression) {
GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
if (expr.equals(parent.getLValue())) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
}
}
if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
final PsiElement parent = expr.getParent();
if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
}
}
}
return conflicts;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GroovyInlineLocalProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
final GrExpression initializer = mySettings.getInitializer();
GrExpression initializerToUse = GrIntroduceHandlerBase.insertExplicitCastIfNeeded(myLocal, mySettings.getInitializer());
for (UsageInfo usage : usages) {
GrVariableInliner.inlineReference(usage, myLocal, initializerToUse);
}
final PsiElement initializerParent = initializer.getParent();
if (initializerParent instanceof GrAssignmentExpression) {
initializerParent.delete();
return;
}
if (initializerParent instanceof GrVariable) {
final Collection<PsiReference> all = ReferencesSearch.search(myLocal).findAll();
if (!all.isEmpty()) {
initializer.delete();
return;
}
}
final PsiElement owner = myLocal.getParent().getParent();
if (owner instanceof GrVariableDeclarationOwner) {
((GrVariableDeclarationOwner) owner).removeVariable(myLocal);
} else {
myLocal.delete();
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method generateAssignment.
private void generateAssignment(@NotNull GrVariable field, @Nullable GrStatement anchor, @Nullable GrStatementOwner defaultContainer, @Nullable PsiElement occurrenceToDelete) {
if (myInitializer == null || defaultContainer == null)
return;
GrAssignmentExpression init = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(myContext.getProject()).createExpressionFromText(mySettings.getName() + " = " + myInitializer.getText());
GrStatementOwner block;
if (anchor != null) {
anchor = GroovyRefactoringUtil.addBlockIntoParent(anchor);
LOG.assertTrue(anchor.getParent() instanceof GrStatementOwner);
block = (GrStatementOwner) anchor.getParent();
} else {
block = defaultContainer;
}
init = (GrAssignmentExpression) block.addStatementBefore(init, anchor);
replaceOccurrence(field, init.getLValue(), (PsiClass) myContext.getScope());
if (occurrenceToDelete != null) {
occurrenceToDelete.delete();
}
}
Aggregations