discard converting from string to primitive during extract refactorings (IDEA-157549; IDEA-157987)

This commit is contained in:
Anna Kozlova
2016-06-29 16:18:27 +03:00
parent 153255f9d2
commit 2d142cdfd7
8 changed files with 46 additions and 31 deletions
@@ -353,14 +353,9 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
try {
text = file.getText().subSequence(startOffset, endOffset).toString();
String prefix = null;
String stripped = text;
if (startLiteralExpression != null) {
final int startExpressionOffset = startLiteralExpression.getTextOffset();
if (startOffset == startExpressionOffset) {
if (StringUtil.startsWithChar(text, '\"') || StringUtil.startsWithChar(text, '\'')) {
stripped = text.substring(1);
}
} else if (startOffset == startExpressionOffset + 1) {
if (startOffset == startExpressionOffset + 1) {
text = "\"" + text;
} else if (startOffset > startExpressionOffset + 1){
prefix = "\" + ";
@@ -371,11 +366,7 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
String suffix = null;
if (endLiteralExpression != null) {
final int endExpressionOffset = endLiteralExpression.getTextOffset() + endLiteralExpression.getTextLength();
if (endOffset == endExpressionOffset ) {
if (StringUtil.endsWithChar(stripped, '\"') || StringUtil.endsWithChar(stripped, '\'')) {
stripped = stripped.substring(0, stripped.length() - 1);
}
} else if (endOffset == endExpressionOffset - 1) {
if (endOffset == endExpressionOffset - 1) {
text += "\"";
} else if (endOffset < endExpressionOffset - 1) {
suffix = " + \"";
@@ -383,24 +374,6 @@ public abstract class IntroduceVariableBase extends IntroduceHandlerBase {
}
}
boolean primitive = false;
if (stripped.equals("true") || stripped.equals("false")) {
primitive = true;
}
else {
try {
Integer.parseInt(stripped);
primitive = true;
}
catch (NumberFormatException e1) {
//then not primitive
}
}
if (primitive) {
text = stripped;
}
if (literalExpression != null && text.equals(literalExpression.getText())) return literalExpression;
final PsiElement parent = literalExpression != null ? literalExpression : elementAt;
@@ -0,0 +1,5 @@
class Test {
void foo() {
String s = "5<selection>+5</selection>";
}
}
@@ -0,0 +1,7 @@
class Test {
public static final String xxx = "+5";
void foo() {
String s = "5" + xxx;
}
}
@@ -0,0 +1,8 @@
class Test {
void print(Stirng s) {
}
void foo() {
print("<selection>5</selection>");
}
}
@@ -0,0 +1,10 @@
class Test {
public static final String xxx = "5";
void print(Stirng s) {
}
void foo() {
print(xxx);
}
}
@@ -1,6 +1,6 @@
class A {
public void test() {
boolean str = true;
String str = "true";
String s = "ss" + str;
}
}
@@ -75,6 +75,18 @@ public class IntroduceConstantTest extends LightCodeInsightTestCase {
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testPartialStringLiteralConvertibleToInt() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testStringLiteralConvertibleToInt() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
new MockIntroduceConstantHandler(null).invoke(getProject(), getEditor(), getFile(), null);
checkResultByFile(BASE_PATH + getTestName(false) + "_after.java");
}
public void testPartialStringLiteralQualified() throws Exception {
configureByFile(BASE_PATH + getTestName(false) + ".java");
final PsiClass psiClass = ((PsiJavaFile)getFile()).getClasses()[0];
@@ -250,7 +250,7 @@ public class IntroduceVariableTest extends LightCodeInsightTestCase {
}
public void testSubPrimitiveLiteral() {
doTest(new MockIntroduceVariableHandler("str", false, false, false, "boolean"));
doTest(new MockIntroduceVariableHandler("str", false, false, false, CommonClassNames.JAVA_LANG_STRING));
}
public void testArrayFromVarargs() {