summaryrefslogtreecommitdiff
path: root/plugins/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java
blob: d116c3ce30fe3aa4199c8ad3a21170ceb8cfef37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.intellij.refactoring.typeMigration;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import com.intellij.structuralsearch.MatchOptions;
import com.intellij.structuralsearch.plugin.replace.ReplaceOptions;
import com.intellij.structuralsearch.plugin.replace.impl.Replacer;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;

/**
 * Created by IntelliJ IDEA.
 * User: db
 * Date: Sep 28, 2004
 * Time: 7:13:53 PM
 * To change this template use File | Settings | File Templates.
 */
public class TypeConversionDescriptor extends TypeConversionDescriptorBase {
  private static final Logger LOG = Logger.getInstance("#" + TypeConversionDescriptor.class.getName());

  private String myStringToReplace = null;
  private String myReplaceByString = "$";
  private PsiExpression myExpression;

  public TypeConversionDescriptor(@NonNls final String stringToReplace, @NonNls final String replaceByString) {
    myStringToReplace = stringToReplace;
    myReplaceByString = replaceByString;
  }

  public TypeConversionDescriptor(@NonNls final String stringToReplace, @NonNls final String replaceByString, final PsiExpression expression) {
    myStringToReplace = stringToReplace;
    myReplaceByString = replaceByString;
    myExpression = expression;
  }

  public void setStringToReplace(String stringToReplace) {
    myStringToReplace = stringToReplace;
  }

  public void setReplaceByString(String replaceByString) {
    myReplaceByString = replaceByString;
  }

  public String getStringToReplace() {
    return myStringToReplace;
  }

  public String getReplaceByString() {
    return myReplaceByString;
  }

  public PsiExpression getExpression() {
    return myExpression;
  }

  public void setExpression(final PsiExpression expression) {
    myExpression = expression;
  }

  @Override
  public void replace(PsiExpression expression) {
    if (getExpression() != null) expression = getExpression();
    final Project project = expression.getProject();
    final ReplaceOptions options = new ReplaceOptions();
    options.setMatchOptions(new MatchOptions());
    final Replacer replacer = new Replacer(project, null);
    try {
      final String replacement = replacer.testReplace(expression.getText(), getStringToReplace(), getReplaceByString(), options);
      try {
        JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression.replace(
          JavaPsiFacade.getInstance(project).getElementFactory().createExpressionFromText(replacement, expression)));
      }
      catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
    catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }

  @Override
  public String toString() {
    StringBuffer buf = new StringBuffer();
    if (myReplaceByString != null) {
      buf.append(myReplaceByString);
    }
    if (myStringToReplace != null) {
      if (buf.length() > 0) buf.append(" ");
      buf.append(myStringToReplace);
    }
    if (myExpression != null) {
      if (buf.length() > 0) buf.append(" ");
      buf.append(myExpression.getText());
    }
    return buf.toString();
  }
}