diff options
author | Tor Norbye <tnorbye@google.com> | 2014-07-25 20:12:40 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2014-07-24 20:55:36 +0000 |
commit | 6044be7330c93bc8dacb0f63e62da8029f39a16f (patch) | |
tree | 0ba72f5de1949e0527874a799baa224cbe1537e0 /java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java | |
parent | b03a5855292feb8c331815f883fe64372aacd872 (diff) | |
parent | e5266e2343c8d275d79fa0be725180d0fe3a993c (diff) | |
download | idea-6044be7330c93bc8dacb0f63e62da8029f39a16f.tar.gz |
Merge "Merge remote-tracking branch 'aosp/upstream-master' into merge"
Diffstat (limited to 'java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java')
-rw-r--r-- | java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java b/java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java new file mode 100644 index 000000000000..d116c3ce30fe --- /dev/null +++ b/java/typeMigration/src/com/intellij/refactoring/typeMigration/TypeConversionDescriptor.java @@ -0,0 +1,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(); + } +} |