update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / refactoring / introduceparameterobject / usageInfo / ReplaceParameterAssignmentWithCall.java
blobf9b486b00ca11fef31c1b18d274d1d6a661ed12c
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.refactoring.introduceparameterobject.usageInfo;
18 import com.intellij.psi.PsiAssignmentExpression;
19 import com.intellij.psi.PsiExpression;
20 import com.intellij.psi.PsiReferenceExpression;
21 import com.intellij.psi.util.PsiTreeUtil;
22 import com.intellij.refactoring.psi.MutationUtils;
23 import com.intellij.refactoring.util.FixableUsageInfo;
24 import com.intellij.util.IncorrectOperationException;
26 public class ReplaceParameterAssignmentWithCall extends FixableUsageInfo {
27 private final PsiReferenceExpression expression;
28 private final String newParameterName;
29 private final String setterName;
30 private final String getterName;
32 public ReplaceParameterAssignmentWithCall(PsiReferenceExpression element, String newParameterName, String setterName, String getterName) {
33 super(element);
34 this.setterName = setterName;
35 this.getterName = getterName;
36 this.newParameterName = newParameterName;
37 expression = element;
40 public void fixUsage() throws IncorrectOperationException {
41 final PsiAssignmentExpression assignment = PsiTreeUtil.getParentOfType(expression, PsiAssignmentExpression.class);
42 assert assignment != null;
43 final PsiExpression rhs = assignment.getRExpression();
44 if (rhs == null) {
45 return;
47 final String rhsText = rhs.getText();
48 final String operator = assignment.getOperationSign().getText();
49 final String newExpression;
50 if ("=".equals(operator)) {
51 newExpression = newParameterName + '.' + setterName + '(' + rhsText + ')';
53 else {
54 final String strippedOperator = operator.substring(0, operator.length() - 1);
55 newExpression =
56 newParameterName + '.' + setterName + '(' + newParameterName + '.' + getterName + "()" + strippedOperator + rhsText + ')';
58 MutationUtils.replaceExpression(newExpression, assignment);